我正在开发一个非常简单的通用主机解决方案,它将允许我们将程序集作为 Windows 服务(ala NServiceBus)托管。我遇到了以下异常(类似于 Dru博客文章中提到的评论)。我需要它来工作,这样我就可以在不同的 AppDomain 中托管服务。
“在程序集 'MyProject.WindowsServices.GenericHost, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' 中键入 'MyProject.WindowsServices.GenericHost.Program+<>c__DisplayClass5' 未标记为可序列化。”
我正在使用 topshelf 主页 (topshelf-project.com) 上的下载链接提供的 Topshelf 1.0 RC 二进制文件。我已经尝试了最新的构建(29/07/2010),并且可以从谷歌代码和 github 下载构建!我不能让他们中的任何一个为我工作!
这在具有旧版本 Topshelf 的 NServiceBus 库中工作(dll 版本为 0.8.0.96)。通过对以下内容进行一些小的代码更改(使用 CreateServiceLocator 代替 HowToBuildService),它适用于这些较旧的二进制文件,但我宁愿坚持使用最新的代码以利用任何计划的修复或增强功能。
这是我的代码。
static void Main(string[] args)
{
ArgumentParser arguments = new ArgumentParser(args);
string configFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
arguments.ServiceType.Assembly.ManifestModule.Name + ".config");
RunConfiguration cfg = RunnerConfigurator.New(x =>
{
x.SetServiceName(arguments.ServiceName);
x.SetDisplayName(arguments.DisplayName);
x.SetDescription(arguments.Description);
if (string.IsNullOrEmpty(arguments.UserName))
{
x.RunAsLocalSystem();
}
else
{
x.RunAs(arguments.UserName, arguments.Password);
}
x.ConfigureServiceInIsolation<GenericHost>(c =>
{
c.ConfigurationFile(configFile);
c.Named(arguments.ServiceType.AssemblyQualifiedName);
c.HowToBuildService(name => new GenericHost(arguments.ServiceType));
c.WhenStarted(tc => tc.Start());
c.WhenStopped(tc => tc.Stop());
});
});
Runner.Host(cfg, args);
}
另外值得注意的是,我的 GenericHost 类和 arguments.ServiceType 标识的类都实现了 MarshalByRefObject 并且我还使这些类 Serializable 以查看是否有帮助。虽然不是这些类导致了问题,但它似乎在抱怨 C# 编译器为我配置的一个或多个 lambda 生成的匿名类型。
有其他人使用 ConfigureServiceInIsolation() 看到这个问题吗?如果没有,有人知道我在这里缺少什么吗?如果您需要更多信息,请告诉我,例如堆栈跟踪或更多代码。