gpt4 book ai didi

c# - 自动部署,Global.asax : Could not load type Namespace. Global

转载 作者:行者123 更新时间:2023-11-30 18:38:42 35 4
gpt4 key购买 nike

这是我的第一篇 StackOverflow 帖子。提前致谢!我在这里找到了很多答案,但在网络上的任何地方都找不到有关我当前问题的任何信息。 =(

我有一个 C# 服务,我已经使用 Visual Studio 2010 Express 中在线找到的 WCFRestWebService 模板测试和部署了该服务。当我将项目“发布”到我的 Web 服务器时,服务会按预期运行。该服务需要三个文件才能在线运行,即 my web.config、global.asax 和 bin/WCFRestWebService1.dll。

然后我想,为什么我不能编写一个程序来生成这三个文件并将这些文件通过 FTP 传输到我的 Web 服务器上呢?所以我写了这个:

namespace Edifice
{
//code behind for ASP.Net page
public partial class SiteMaster : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e) { }
protected void Button1_Click(object sender, EventArgs e) {
//the first Write() method returns an array of strings
//containing .cs files for the project, in this particular
//case, 2 strings, one containing the service.cs and one
//containing global.asax.cs
string[] parameters = eConfig.midTier.Write(eConfig);
string webConfig = eConfig.midTier.WriteWebConfig(eConfig);
string globalAsax = eConfig.midTier.WriteGlobalAsax(eConfig);
Assembly assembly = ECompiler.BuildAssembly(eConfig.className, parameters);
EFTP.Upload(assembly, webConfig, globalAsax, eConfig);
}
}
public static class EFTP
{
//for this sample, FTP merely saves the file to the current file system for testing
public static void Upload(Assembly sourceFile, string webConfig, string globalAsax, EConfiguration eConfig)
{
string filepath = "C:\\EdificeTest\\dlls\\";
using (Stream oStream = new FileStream(filepath + "bin\\" + "WCFRESTService2.dll", FileMode.Create))
{
StreamWriter sWriter = new StreamWriter(oStream);
sWriter.Write(sourceFile);
sWriter.Close();
}
using (Stream oStream = new FileStream(filepath + "Web.config", FileMode.Create))
{
StreamWriter sWriter = new StreamWriter(oStream);
sWriter.Write(webConfig);
sWriter.Close();
}
using (Stream oStream = new FileStream(filepath + "Global.asax", FileMode.Create))
{
StreamWriter sWriter = new StreamWriter(oStream);
sWriter.Write(globalAsax);
sWriter.Close();
}
}
}
public static class ECompiler
{
public static Assembly BuildAssembly(string assemblyName, string[] sources)
{
List<string> WCFRestServiceAssemblies = new List<string>();
WCFRestServiceAssemblies.Add("Microsoft.CSharp.dll");
WCFRestServiceAssemblies.Add("System.dll");
WCFRestServiceAssemblies.Add("System.Configuration.dll");
WCFRestServiceAssemblies.Add("System.Core.dll");
WCFRestServiceAssemblies.Add("System.Data.dll");
WCFRestServiceAssemblies.Add("System.Drawing.dll");
WCFRestServiceAssemblies.Add("System.EnterpriseServices.dll");
WCFRestServiceAssemblies.Add("System.Runtime.Serialization.dll");
WCFRestServiceAssemblies.Add("System.ServiceModel.dll");
WCFRestServiceAssemblies.Add("System.ServiceModel.Activation.dll");
WCFRestServiceAssemblies.Add("System.ServiceModel.Web.dll");
WCFRestServiceAssemblies.Add("System.Web.dll");
WCFRestServiceAssemblies.Add("System.Web.ApplicationServices.dll");
WCFRestServiceAssemblies.Add("System.Web.DynamicData.dll");
WCFRestServiceAssemblies.Add("System.Web.Entity.dll");
WCFRestServiceAssemblies.Add("System.Web.Extensions.dll");
WCFRestServiceAssemblies.Add("System.Web.Services.dll");
WCFRestServiceAssemblies.Add("System.Xml.dll");
WCFRestServiceAssemblies.Add("System.Xml.Linq.dll");

Microsoft.CSharp.CSharpCodeProvider provider = new CSharpCodeProvider(new Dictionary<String, String> { { "CompilerVersion", "v4.0" } });
ICodeCompiler compiler = provider.CreateCompiler();
CompilerParameters compilerparams = new CompilerParameters(WCFRestServiceAssemblies.ToArray());
compilerparams.OutputAssembly = assemblyName + ".dll";
compilerparams.GenerateExecutable = false;
compilerparams.GenerateInMemory = true;
CompilerResults results = compiler.CompileAssemblyFromSourceBatch(compilerparams, sources);

if (results.Errors.HasErrors)
{
StringBuilder errors = new StringBuilder("Compiler Errors :\r\n");
foreach (CompilerError error in results.Errors)
{
errors.AppendFormat("Line {0},{1}\t: {2}\n",
error.Line, error.Column, error.ErrorText);
}
throw new Exception(errors.ToString());
}
else
{
return results.CompiledAssembly;
}
}
}
}

那么,问题是即使可以将完全相同的代码粘贴到 Visual Studio 项目中,并且我可以从 Visual Studio 编译和发布该服务(而且它可以工作!),当我尝试使用相同的文件时由这些方法生成,当我尝试调用该服务时出现错误:

[HttpException]: Could not load type 'EdificeTest.Global'.
at System.Web.UI.TemplateParser.GetType(String typeName, Boolean ignoreCase, Boolean throwOnError) at System.Web.UI.TemplateParser.ProcessInheritsAttribute(String baseTypeName, String codeFileBaseTypeName, String src, Assembly assembly) at System.Web.UI.TemplateParser.PostProcessMainDirectiveAttributes(IDictionary parseData)

'EdificeTest' 是 Web 服务的命名空间,并由 Global.asax 文件引用。根据我的分析,似乎对于“已发布”服务,Global.asax.cs 文件已编译并添加到程序集中,而 Global.asax 文件在程序集中引用其“代码隐藏”文件。当 visual studio 负责打包项目时,这似乎有效,但是我必须在我的自动部署中遗漏一个步骤,因为它在 Global.asax 中找不到 'Inherits=' 属性的类型并抛出错误.

请帮忙?

最佳答案

我不确定我是否完全理解您的问题。如果您不希望您的全局代码隐藏类在汇编中,您可以在 global.asax 中使用内联代码,而不是继承/代码隐藏:

<%@ Application Language="C#" %>
<script RunAt="server">
protected void Application_Start(object sender, EventArgs e)
{
}

protected void Session_Start(object sender, EventArgs e)
{

}

protected void Application_BeginRequest(object sender, EventArgs e)
{

}

protected void Application_AuthenticateRequest(object sender, EventArgs e)
{

}

protected void Application_Error(object sender, EventArgs e)
{

}

protected void Session_End(object sender, EventArgs e)
{

}

protected void Application_End(object sender, EventArgs e)
{

}
</script>

关于c# - 自动部署,Global.asax : Could not load type Namespace. Global,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10966416/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com