這是我第一次的StackOverflow後未能加載類型。提前致謝!我在這裏找到了很多答案,但在網絡上的任何地方都找不到我當前的問題。 =(自動部署,Global.asax中:Namespace.Global
我有一個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(字符串的typeName,布爾 IGNORECASE,布爾throwOnError)在 System.Web.UI.TemplateParser.ProcessInheritsAttribute(字符串 baseTypeName,字符串codeFileBaseTypeName,字符串SRC,裝配 組件)在 System.Web.UI.TemplateParser.PostProcessMainDirectiveAttributes(IDictionary的 parseData)
'EdificeTest' 是Web服務的命名空間,並正在由Global.asax文件中引用。從我的分析看來,對於'發佈'服務,Global.asax.cs文件被編譯並添加到程序集中,並且Global.asax文件在程序集中引用其'代碼隱藏'文件。這似乎在Visual Studio負責打包項目時起作用,但是我必須錯過自動部署中的一個步驟,因爲它無法在Global.asax中找到'Inherits ='屬性的類型,並且會引發錯誤。
請幫助?
爲什麼你要找重塑部署時,MS Web部署工作並可以通過MSBuild的,PowerShell的,等很容易實現自動化? –
看來,如果我不編譯Global.asax.cs,而改爲使用CodeFile =「」而不是Codebehind =「」的Global.asax源,那麼我可以推遲編譯Global.asax。 cs直到服務被執行。所以我會有四個文件上傳而不是三個。看起來很愚蠢,僅僅編譯和鏈接Global.asax.cs文件是非常困難的,但是,看起來這個修復實際上解決了這個問題。我仍然好奇我是否可以預編譯全局類並使用codebehind =「」? –
我沒有考慮過MSBuild,Powershell等,大多是出於無知。我會閱讀這些選項,看看它們將如何適用於這種情況。在我匆忙中,我選擇將這個自定義代碼拼湊起來,而不是學習新的東西。 –