3

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

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

于是我想,为什么我不能编写一个程序来生成这三个文件并将这些文件通过 FTP 传输到我的网络服务器。所以我写了这个:

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]:无法加载类型“EdificeTest.Global”。
在 System.Web.UI.TemplateParser.GetType(String typeName, Boolean ignoreCase, Boolean throwOnError) 在 System.Web.UI.TemplateParser.ProcessInheritsAttribute(String baseTypeName, String codeFileBaseTypeName, String src, Assembly assembly) 在 System.Web.UI。 TemplateParser.PostProcessMainDirectiveAttributes(IDictionary parseData)

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

请帮忙?

4

2 回答 2

0

请拖动并drop your Global.asax.cs归档到App_Code folder. 它将解决您与 Global.asax.cs 文件相关的所有无聊问题。

干杯,Jignesh Jinjariya

于 2013-03-16T05:53:33.283 回答
0

我不确定我是否完全理解您的问题。如果您不希望您的全局代码隐藏类在汇编中,您可以在 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>
于 2012-08-23T04:29:55.453 回答