这是我的第一篇StackOverflow帖子。提前致谢!我在这里找到了很多答案,但无法在网络上的任何地方找到关于我当前问题的任何信息。 =(
我有一个C#服务我已经使用在Visual Studio 2010 Express中在线找到的WCFRestWebService模板进行了测试和部署。当我发布'项目到我的Web服务器,服务按需要运行。该服务需要三个文件才能在线运行,即我的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]:无法加载类型&#39; EdificeTest.Global&#39;。
在System.Web.UI.TemplateParser.GetType(String typeName,Boolean ignoreCase,Boolean throwOnError)at System.Web.UI.TemplateParser.ProcessInheritsAttribute(字符串 baseTypeName,String codeFileBaseTypeName,String src,Assembly 汇编) System.Web.UI.TemplateParser.PostProcessMainDirectiveAttributes(IDictionary的 parseData)
&#39; EdificeTest&#39;是Web服务的命名空间,Global.asax文件正在引用它。根据我的分析,似乎对于已发布的&#39; service,Global.asax.cs文件被编译并添加到程序集中,Global.asax文件引用其代码隐藏&#39;文件在程序集中。当visual studio负责打包项目时,这似乎有效,但是我必须错过自动部署中的一个步骤,因为它无法找到“继承=”的类型。 Global.asax中的属性,它会抛出错误。
请帮帮忙?
答案 0 :(得分:0)
我不确定我是否完全理解你的问题。 如果您不希望全局代码隐藏类处于程序集中,则可以在global.asax中使用内联代码,而不是继承/ codebehind:
<%@ 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>
答案 1 :(得分:0)
请将drop your Global.asax.cs
文件拖至App_Code folder
。
它将修复与Global.asax.cs文件相关的所有无聊问题。
干杯, Jignesh Jinjariya