是否可以运行在Windows服务中托管的Orleans

时间:2016-04-28 21:43:36

标签: c# windows-services topshelf orleans

如果有任何直接的方法来运行在Windows服务中托管的Orleans,请指出。如果没有直接的方法,那么有没有间接的方法呢?

提前谢谢

1 个答案:

答案 0 :(得分:8)

注意:这适用于v1.x Orleans。 2.x配置发生了很大变化

这是一个基于topshelf的样本。 引用https://github.com/migrap/Topshelf.Orleans

static void Main()
{
    HostFactory.Run(c => {
        c.Service<OrleansService>(s => {
            s.ConstructUsing(sc => {
                sc.ConfigFileName("OrleansConfiguration.xml");

                //do some config at runtime if you want
                //sc.DeploymentId("blachblahc");

            });

            s.WhenStarted((service, control) => service.Start());
            s.WhenStopped((service, control) => service.Stop());
        });

        c.RunAsLocalSystem();
        c.UseAssemblyInfoForServiceInfo();
        c.SetServiceName("OrleansSiloHostService");
        c.StartAutomaticallyDelayed();
    });
}

public class OrleansService
{
    private readonly SiloHost host;
    private Task startup;

    internal OrleansService(SiloHost silohost)
    { host = silohost; }

    public bool Start()
    {
        host.LoadOrleansConfig();
        host.InitializeOrleansSilo();
        startup = Task.Factory.StartNew(() =>
        {
            return host.StartOrleansSilo();
        });
        return true;
    }

    public bool Stop()
    {
        if (startup.Status == TaskStatus.RanToCompletion)
        { host.ShutdownOrleansSilo(); }
        return true;
    }
}