1

ServiceStack 使用 Funq 方言(不支持元数据),而 Kephas 使用 MEF/Autofac 之一(需要元数据支持)。我的问题有两个部分:

  • 如果可能的话,如何让 ServiceStack 和 Kephas 使用一个 DI 容器?

  • 根据上面的答案:如何使IClientCacheKephas 组件可以使用 ServiceStack 服务(如 ),知道这些服务可能不会用 注释[AppServiceContract]

4

2 回答 2

2

我以前从未听说过 Kephas,但如果你指的是GitHub 上的这个 Kephas Framework,它会说它使用 ASP.NET Core,在这种情况下,最好让它们都使用 ASP.NET Core 的 IOC,你可以这样做通过ConfigureServices在应用程序的启动中注册您的依赖项:

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        //...
    }
}

或者,在 ServiceStack 的最新 v5.6 版本的模块化启动中,将您的 ASP.NET Core Startup 类更改为继承自ModularStartup,例如:

public class Startup : ModularStartup
{
    public Startup(IConfiguration configuration) : base(configuration){}

    public new void ConfigureServices(IServiceCollection services)
    {
        //...
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        //...
    }
}

在这种情况下,您将能够通过在 AppHost中注册它们来在AppHost 中注册 ASP.NET Core 依赖项Configure(IServiceCollection),在那里它们可以通过 ASP.NET Core 的 IOC + ServiceStack 的 IOC 来解决,例如:

public class AppHost : AppHostBase
{
    public override void Configure(IServiceCollection services)
    {
        services.AddSingleton<IRedisClientsManager>(
            new RedisManagerPool(Configuration.GetConnectionString("redis")));
    }

    public override void Configure(Container container)
    {
        var redisManager = container.Resolve<IRedisClientsManager>();
        //...
    }
}
于 2019-09-02T14:43:41.980 回答
2

您可以通过选择使用 Autofac 使 ASP.NET 和 Kephas 使用一个容器。但是,正如@mythz 指出的那样,您需要将AutofacIoC 适配器提供给ServiceStack. 我认为这样做不会对 ASP.NET 有任何问题,这Autofac是 ASP.NET Core 团队的第一个建议。

对于 ASP.NET Core,Kephas.AspNetCore如果需要全部设置,请引用包并从 StartupBase 类继承。但是,如果您需要控制,请查看https://github.com/kephas-software/kephas/blob/master/src/Kephas.AspNetCore/StartupBase.cs并编写您自己的 Startup 类。您可能会发现另一个有用的资源是Kephas.ServiceStack集成包。

然后,除了注释服务契约和服务实现之外,Kephas 还允许您通过实现IAppServiceInfoProvider接口来提供服务定义。这些类是自动发现的,所以这几乎是您必须做的所有事情。

public class ServiceStackAppServiceInfoProvider : IAppServiceInfoProvider
{
    public IEnumerable<(Type contractType, IAppServiceInfo appServiceInfo)> GetAppServiceInfos(IList<Type> candidateTypes, ICompositionRegistrationContext registrationContext)
    {
        yield return (typeof(IUserAuthRepository),
                         new AppServiceInfo(
                             typeof(IUserAuthRepository),
                             AppServiceLifetime.Singleton));

        yield return (typeof(ICacheClient),
                         new AppServiceInfo(
                             typeof(ICacheClient),
                             ctx => new MemoryCacheClient(),
                             AppServiceLifetime.Singleton));
    }
}

请注意,在上面的示例中,IUserAuthRepository没有提供任何实现。这表明 Kephas 会自动发现为组合注册的类型中的实现。或者,如果您需要确定性,请随意在注册中使用实例或工厂。

于 2019-09-06T20:27:20.593 回答