ServiceStack APPHOST是一个单? [英] ServiceStack AppHost Is a Singleton?

查看:250
本文介绍了ServiceStack APPHOST是一个单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在评估ServiceStack,到目前为止,我已经几乎售 - 但我有我的认为的将是一个交易断路器的要求

I have been evaluating ServiceStack and so far, I've been pretty much sold - but I have a requirement that I think is going to be a deal-breaker.

基本上,我需要多APPHOST衍生实例。第一个很好的旋转起来,但其余的失败,因为AppHostBase.Instance已设定。

I basically need multiple AppHost-derived instances. The first one spins up nicely, but the rest fail because "AppHostBase.Instance has already been set".

这是否意味着,如果我想多台主机(在不同的URI) ,是在不同的可执行文件?如果是这样,这是相当令人失望的,因为这个库是完美的在每一个其他的方式,除了这个限制。

Does this mean if I want multiple hosts (on different Uris), to be in different executables? If so this is rather disappointing as this library is perfect in every single other way, other than this limitation.

如果我错了,任何人都可以指向我一个解决方案吗?

If I am wrong, can anyone point me to a solution?

请允许我阐述了一点。我们为在网络许多,许多服务计划。我的本意是,它们被设计为独立的服务,然后在一个托管班主办的。托管类本身就是为了管理而服务,并启动和停止服务的能力。它们是在整个网络发现。我们可以旋转起来在不同的时间降速在不同机器上的服务,或随意。

Allow me to elaborate a little. We have plans for many, many services across the network. My intention is that they are designed as individual services and then "hosted" in a hosting class. The hosting class itself is a service for administrative purposes and has the ability to start and stop services. They are "discoverable" across the network. We can spin up and spin down services on different machines at different times, or at will.

我的计划是使用服务栈对于这些事情,有创造能力和在任何机器上,在任何的主机破坏一个特设的方式服务。我在跌跌撞撞的唯一一件事就是APPHOST只被初始化一次。

My plan was to use service-stack for each of these things and have the ability to create and destroy the services in an ad-hoc manner on any machine and in any "host". The only thing I am stumbling on is the AppHost only being initialised once.

很显然,我做错了什么,所以如何在同一个可执行承载多个服务上的任何信息将是巨大的:)

Obviously I am doing something wrong, so any information on how to host multiple services in the same executable would be great :)

推荐答案

在回答这个问题我已经添加了新的模块化服务体育项目结构 wiki页面突出结构的不同方式,模块化ServiceStack服务,我会重复听到曝光率:

In response to this question I've added new Modularizing services and Physical project structure wiki pages to highlight the different ways of structuring and modularizing ServiceStack services which I'll repeat hear for discoverability:

正如您所发现的,ServiceStack有单一应用程序主机为每个应用程序域。正如你可能能够从名字推断,在主机项目的作用是对于结合所有服务混凝土的依赖,插件,过滤器和其他所有的服务需求的渠道。一切都在你的 AppHost.Configure()方法初始化后,你的服务的配置应该是一成不变的。该体育项目结构的wiki页面维基显示了典型的解决方案建议的物理项目的结构。

As you've discovered, ServiceStack has a single App Host for each App Domain. As you might be able to infer from the name, the role of the Host project is to be the conduit for binding all your services concrete dependencies, plugins, filters and everything else your service needs. The configuration of your service should be immutable after everything is initialized in your AppHost.Configure() method. The Physical project structure wiki page wiki shows the recommended physical project structure for typical solutions.

虽然你只能有1 APPHOST,服务可以在多个组件传播通过提供模块化服务在AppHostBase构造组件,例如:

Whilst you can only have 1 AppHost, services can be spread across multiple assemblies by providing the Assemblies in the AppHostBase constructor, e.g:

public class AppHost : AppHostBase
{
    //Tell Service Stack the name of your application and which assemblies to find your web services
    public AppHost() : base("Hello ServiceStack!", 
       typeof(ServicesFromDll1).Assembly, ServicesFromDll2).Assembly /*, etc */) { }

    public override void Configure(Container container) {}
}

您也可以提供自己的战略,发现和解决服务类型ServiceStack应该通过重写自动线 CreateServiceManager ,例如:

You can also provide your own strategy for discovering and resolving the service types that ServiceStack should auto-wire by overriding CreateServiceManager, e.g:

public class AppHost : AppHostBase
{
    public AppHost() : base("Hello ServiceStack!", typeof(ServicesFromDll1).Assembly) { }
    public override void Configure(Container container) {}

    //Provide Alternative way to inject IOC Container + Service Resolver strategy
    protected virtual ServiceManager CreateServiceManager(params Assembly[] assembliesWithServices)
    {       
        return new ServiceManager(new Container(),
            new ServiceController(() => assembliesWithServices.ToList().SelectMany(x => x.GetTypes())));
    }
}



封装内的插件服务



模块化服务的一种方法是将它们封装内 插件,它允许你手动注册服务,自定义路线,过滤器,内容类型,允许定制和其他任何你的模块的需求。

Encapsulating Services inside Plugins

One way of modularizing services is to encapsulate them inside Plugins which allows you to manually register services, custom routes, filters, content types, allow customization and anything else your module needs.

要说明这一点,我们将展示什么是基本的验证功能例子可能是这样的:

To illustrate this point, we'll show what a Basic Auth Feature example might look like:

public class BasicAuthFeature : IPlugin 
{
    public string HtmlRedirect { get; set; }   //User-defined configuration

    public void Register(IAppHost appHost)
    {
        //Register Services exposed by this module
        appHost.RegisterService<AuthService>("/auth", "/auth/{provider}");
        appHost.RegisterService<AssignRolesService>("/assignroles");
        appHost.RegisterService<UnAssignRolesService>("/unassignroles");

        //Load dependent plugins
        appHost.LoadPlugin(new SessionFeature());
    }
}

通过一切都封装在一个插件内,用户可以很容易地使他们在与APPHOST:

With everything encapsulated inside a plugin, your users can easily enable them in your AppHost with:

Plugins.Add(new BasicAuthFeature { HtmlRedirect = "~/login" });



物理项目结构



请参阅此早些时候回答上推荐的方法以物理布局项目。

Physical Project Structure

See this earlier answer on the recommended way to physically layout your project.

这篇关于ServiceStack APPHOST是一个单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆