问题
I have tried several variations over the past several weeks and so far all I can do is pass a message from the UI to the server and back. I am unable to pass a message from my controller or other method outside the hub to the UI. It seems like I am not getting an instance of the HubContext. I've created a basic demo app from scratch and uploaded here:
https://github.com/cdecinkoKnight/SignalRDemo-help
I have a basic hub (MessageHub):
[HubName("messages")]
public class MessageHub : Hub, IMessageBroker
{
public override Task OnConnected()
{
Debug.WriteLine("Connected: " + Context.ConnectionId);
return base.OnConnected();
}
public void ShowNewMessage(string message)
{
Debug.WriteLine("Show New Message: " + message);
Clients.All.showMessageOnPage("Show New Message: " + message);
}
}
And a basic controller:
public class ValuesController : ApiController
{
private readonly ISampleService _sampleService;
private IConnectionManager _connectionManager;
public ValuesController(ISampleService sampleService, IConnectionManager connectionManager)
{
_sampleService = sampleService;
_connectionManager = connectionManager;
}
// GET api/values
[HttpGet]
public string Get()
{
var context = _connectionManager.GetHubContext<MessageHub>();
context.Clients.All.showMessageOnPage("From controller");
return _sampleService.GetDummyValue();
}
}
I know the issue is in my Startup but so far nothing has worked.
public class Startup
{
public void Configuration(IAppBuilder app)
{
var builder = new ContainerBuilder();
builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
builder.RegisterHubs(Assembly.GetExecutingAssembly());
builder.RegisterType<AutofacDependencyResolver>().As<IDependencyResolver>().SingleInstance();
builder.RegisterType<ConnectionManager>().As<IConnectionManager>().SingleInstance();
builder.RegisterType<SampleService>().As<ISampleService>().InstancePerLifetimeScope();
builder.RegisterType<MessageBroker>().As<IMessageBroker>();
var container = builder.Build();
GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container);
app.UseAutofacMiddleware(container);
app.UseCors(CorsOptions.AllowAll);
app.Map("/signalr", map =>
{
map.UseCors(SignalrCorsOptions.Value);
map.UseAutofacMiddleware(container);
var hubConfiguration = new HubConfiguration
{
Resolver = new AutofacDependencyResolver(container),
};
hubConfiguration.EnableDetailedErrors = true;
hubConfiguration.EnableJavaScriptProxies = true;
map.RunSignalR(hubConfiguration);
});
}
private static readonly Lazy<CorsOptions> SignalrCorsOptions = new Lazy<CorsOptions>(() =>
{
return new CorsOptions
{
PolicyProvider = new CorsPolicyProvider
{
PolicyResolver = context =>
{
var policy = new CorsPolicy();
policy.AllowAnyOrigin = true;
policy.Origins.Add("http://localhost:44386");
policy.AllowAnyMethod = true;
policy.AllowAnyHeader = true;
policy.SupportsCredentials = true;
return Task.FromResult(policy);
}
}
};
});
}
I'm hoping someone can take a look and help me get this working.
来源:https://stackoverflow.com/questions/65331998/signalr-2-autofac-owin-webapi-will-not-pass-message-from-server-to-hub