SignalR.Orleans在app.UseSignalR中导致错误

时间:2019-03-10 03:27:48

标签: signalr orleans

我正在尝试使用此库https://github.com/OrleansContrib/SignalR.Orleans将SignalR与MS Orleans背板一起使用
当我像自述文件中的指令一样将.AddOrleans()添加到AddSignalR()时,但是在Configure中的UseSignalR()代码中导致此错误:

  

尝试激活“ SignalR.Orleans.DefaultClusterClientProvider”时,无法解析类型为“ Orleans.IClusterClient”的服务。”

ConfigureServices:

public void ConfigureServices(IServiceCollection services)
    {            
        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
        services.AddSignalR().AddOrleans();
    }

配置:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Error");
            // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
            app.UseHsts();
        }
        app.UseAuthentication();
        app.UseHttpsRedirection();
        app.UseStaticFiles();
        app.UseCookiePolicy();
        app.UseSignalR(routes =>
        {
            routes.MapHub<Hubs.MainHub>("/main");
        });// this code work if I remove AddOrleans in ConfigureServices
        app.UseMvc();
    }

检查库代码后,AddOrleans函数代码是这样的:

public static ISignalRBuilder AddOrleans(this ISignalRBuilder builder, IClusterClientProvider clientProvider = null)
    {
        if (clientProvider != null)
            builder.Services.AddSingleton(clientProvider);
        else
            builder.Services.TryAddSingleton<IClusterClientProvider, DefaultClusterClientProvider>();

        builder.Services.AddSingleton(typeof(HubLifetimeManager<>), typeof(OrleansHubLifetimeManager<>));
        return builder;
    }

我仍然不知道为什么它不起作用,因为依赖注入对我来说是一种新事物。

1 个答案:

答案 0 :(得分:0)

DefaultClusterClientProvider构造函数期望一个IClusterClient实例。您收到的异常指示IClusterClient实例未在DI容器中注册。

您发布的示例代码似乎没有实例化集群客户端。您可能需要添加如下内容:

public void ConfigureServices(IServiceCollection services)
{            
    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
    services.AddSignalR().AddOrleans();

    var client = new ClientBuilder()
        ... // other client configuration options here
        .UseSignalR()
        .Build();
    services.AddSingleton<IClusterClient>(client);
}

除了其他对象(例如ASP.NET Core控制器)之外,单例注册还应使DefaultClusterClientProvider构造函数可以使用群集客户端。