我通过界面进行了简单的Typed Hub设置。我可以成功连接到SignalR集线器。
namespace POC.WebSocket
{
public class ApiResultHub : Hub<IApiResultHubClient>
{
public async Task SendMessage(string user, string message)
{
await Clients.All.ReceiveMessage(user, message);
}
public override Task OnConnectedAsync()
{
Clients.Caller.ReceiveMessage("newUser", $"{Context.ConnectionId}");
return base.OnConnectedAsync();
}
}
}
这是我的其他课程文件: Startup.cs
namespace POC.WebSocket
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(o => o.AddPolicy("CorsPolicy", builder =>
{
builder
.AllowAnyMethod()
.AllowAnyHeader()
.WithOrigins("https://localhost:44324", "https://localhost:44326")
.AllowCredentials();
}));
services.AddSignalR();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
}
app.UseCors("CorsPolicy");
app.UseSignalR(routes =>
{
routes.MapHub<ApiResultHub>("/apiResult");
});
app.UseHttpsRedirection();
app.UseMvc();
}
}
}
IApiResultHubClient`T.cs
namespace POC.Common
{
public interface IApiResultHubClient
{
Task ReceiveMessage(string user, string message);
Task SendApiResponse<T>(ApiResult<T> data);
}
}
还有其他人看到过这个问题吗?我发现很少有类似的问题可以连接到集线器本身。但是,如上所述,我能够成功连接到集线器,只是无法解析Clients.All,Clients.Caller或Clients.Others。
今天更新了所有库:
.Net Core版本2.1.4 .Net SignalR Core版本1.0.3
修改: 找到该异常的详细信息:
方法“ SendApiResponse”的类型 'Microsoft.AspNetCore.SignalR.TypedClientBuilder.IApiResultHubClientImpl' 从程序集“ Microsoft.AspNetCore.SignalR.TypedClientBuilder”中, 版本= 0.0.0.0,文化=中性,PublicKeyToken =空'没有 一个实现。