Reputation:
When starting a selfhost SignalR server, I can broadcast messages if I don't use a HubConfiguration
for example, broadcasting a message works fine using the following
// SelfHost
public class SelfHostStartUp
{
public void Configuration(IAppBuilder app)
{
app.UseCors(CorsOptions.AllowAll);
app.MapSignalR();
}
}
but stops working when using this
// SelfHost
public class SelfHostStartUp
{
public void Configuration(IAppBuilder app)
{
var config = new HubConfiguration
{ EnableJSONP = false,
EnableDetailedErrors = true,
Resolver = new DefaultDependencyResolver()
};
app.UseCors(CorsOptions.AllowAll);
app.MapSignalR(config);
}
}
Server Side Event (outside of SignalRHub):
private void button1_Click(object sender, EventArgs e)
{
var hubContext = GlobalHost.ConnectionManager.GetHubContext<SignalRHub>();
hubContext.Clients.All.SendMessageTest("Hello");
}
Client Side:
hubConnection = new HubConnection(ipAddressAndPort);
hubConnection.StateChanged += HubConnection_StateChanged;
hubConnection.TraceLevel = TraceLevels.All;
hubConnection.TraceWriter = new EventLogTraceWriter(eventLog1);
hubProxy = hubConnection.CreateHubProxy("SignalRHub");
hubProxy.On<string>("SendMessageTest", message =>
Debug.WriteLine($"{message}")
);
There are no errors raised and server appears to send the message but client is not receiving anything. I need to be able to set the HubConfiguration, so can anyone help me determine why this is not working?
Upvotes: 1
Views: 154
Reputation:
Found a working solution, the default resolver in GlobalHost also needs to be set/reset
either of the following is working okay now
public class SelfHostStartUp
{
public void Configuration(IAppBuilder app)
{
var defaultDependencyResolver = new DefaultDependencyResolver();
GlobalHost.DependencyResolver = defaultDependencyResolver;
var config = new HubConfiguration { EnableJSONP = false, EnableDetailedErrors = true, Resolver = defaultDependencyResolver };
app.UseCors(CorsOptions.AllowAll);
app.MapSignalR(config);
}
}
or
public class SelfHostStartUp
{
public void Configuration(IAppBuilder app)
{
GlobalHost.DependencyResolver = new DefaultDependencyResolver();
app.UseCors(CorsOptions.AllowAll);
app.MapSignalR();
}
}
Upvotes: 1