简体   繁体   English

使用ConfigureServiceInIsolation - Generic Host运行Topshelf服务

[英]Running Topshelf service using ConfigureServiceInIsolation - Generic Host

I am developing a very simple Generic Host solution that will allow us to host assemblies as windows services (ala NServiceBus). 我正在开发一个非常简单的通用主机解决方案,它允许我们将程序集作为Windows服务托管(ala NServiceBus)。 I'm coming across the following exception (similar to the comments mentioned on Dru's blog post ). 我遇到了以下异常(类似于Dru的博客文章中提到的评论)。 I need this to work so I can host services in different AppDomains. 我需要这个工作,所以我可以在不同的AppDomain中托管服务。

"Type 'MyProject.WindowsServices.GenericHost.Program+<>c__DisplayClass5' in Assembly 'MyProject.WindowsServices.GenericHost, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable." “在程序集'MyProject.WindowsServices.GenericHost中输入'MyProject.WindowsServices.GenericHost.Program + <> c__DisplayClass5',Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null'未标记为可序列化。”

I'm using the Topshelf 1.0 RC binaries available from the download link on the topshelf homepage (topshelf-project.com). 我正在使用Topshelf主页(topshelf-project.com)上的下载链接提供的Topshelf 1.0 RC二进制文件。 I've tried the latest build (29/07/2010), and the builds avialable for download from google code and github! 我已经尝试了最新版本(29/07/2010),以及可从谷歌代码和github下载的版本! I can't get any of them to work for me! 我不能让他们中的任何一个为我工作!

This is working in the NServiceBus library with an older version of Topshelf (the dll is versioned 0.8.0.96). 这是在NServiceBus库中使用旧版本的Topshelf(dll版本为0.8.0.96)。 With some minor code changes to what I have below (use CreateServiceLocator in place of HowToBuildService) it works for me with these older binaries, but I'd rather stick to the latest code to take advantage of any planned fixes or enhancements. 通过对我下面的代码进行一些小的代码更改(使用CreateServiceLocator代替HowToBuildService)它对我来说对这些旧的二进制文件有用,但我宁愿坚持使用最新的代码来利用任何计划的修复或增强功能。

Here is my code. 这是我的代码。

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);
}

Also of note is that my GenericHost class and the class identified by arguments.ServiceType both implement MarshalByRefObject and I have also made these classes Serializable to see if that would help. 另外值得注意的是,我的GenericHost类和arguments.ServiceType标识的类都实现了MarshalByRefObject,我还使这些类Serializable看看是否有用。 It's not these classes that are causing the problem though, it appears to be complaining about an anonymous type generated by the C# compiler for one or more of the lambda's I have configured. 不是这些类引起了问题,它似乎抱怨C#编译器为我配置的一个或多个lambda生成的匿名类型。

Is anyone else seeing this problem with using ConfigureServiceInIsolation()? 是否有其他人使用ConfigureServiceInIsolation()看到此问题? If not, does anyone know what I'm missing here? 如果没有,有谁知道我在这里缺少什么? Let me know if you need more info, eg stack trace or more code. 如果您需要更多信息,请告诉我,例如堆栈跟踪或更多代码。

If you're only using one service inside the host, I would remove the "InIsolation". 如果您只在主机内使用一项服务,我会删除“InIsolation”。 It doesn't work right but in a future version of TopShelf (we are currently working on it) I think we have a better answer for this issue. 它不能正常工作,但在TopShelf的未来版本中(我们正在研究它)我认为我们有更好的答案来解决这个问题。 On top of the ability to just drop files in a host and have that spin up your service in a new AppDomain automagically. 除了能够在主机中删除文件并在新的AppDomain中自动启动服务之外。

I would say this falls under a know issue and unless there's a compelling reason to use the InIsolation avoid it for the moment. 我会说这属于一个已知问题,除非有令人信服的理由使用InIsolation暂时避开它。 You can't marshal lambda expressions across app domain barriers, hence the issue you're seeing. 您无法跨应用程序域障碍编组lambda表达式,因此您遇到的问题。 If the InIsolation issue is important enough, I can look into the effort to fix that vs. the timeline before we plan on releasing the newest version. 如果InIsolation问题非常重要,我可以在计划发布最新版本之前研究解决这个问题与时间表的关系。 [You can grab the latest dev. [你可以抓住最新的开发者。 bits from here: http://github.com/legomaster/Topshelf -- warning, we're still under active development but I think all the major bugs are now squashed]. 来自这里的位: http//github.com/legomaster/Topshelf-警告,我们仍在积极开发中,但我认为所有主要的错误现在都被压扁了]。

If you want to discuss this further, it might be easiest to post on the MassTransit list where all the developers are watching: http://groups.google.com/group/masstransit-discuss 如果您想进一步讨论这个问题,最简单的方法是在所有开发人员都在观看的MassTransit列表中发帖: http ://groups.google.com/group/masstransit-discuss

I hope this helps! 我希望这有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM