Reputation: 136
We are trying to start our Signalr Hub with Dependency Injection using Autofac but with no result yet. MVC and Web Apis are working fine with DI.
Here the config files.
Startup.cs
[assembly: OwinStartupAttribute(typeof(PNAME.WebUI.Startup))]
namespace PNAME.WebUI
{
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
var config = new HttpConfiguration();
// Register Routes
WebApiConfig.Register(config);
// Register dependencies
//ConfigureDependencies(app, config);
var container = DependencyConfiguration.Configure(app);
SignalRConfiguration.Configure(app, container);
MvcConfiguration.Configure(app, container);
WebApiConfiguration.Configure(app, container, config);
// Configure Authentication middleware
ConfigureAuth(app, config);
// configure Web API
app.UseWebApi(config);
}
}
}
DependencyConfiguration.cs
public static class DependencyConfiguration
{
public static IContainer Configure(IAppBuilder app)
{
var builder = new ContainerBuilder();
//Register any other components required by your code....
builder.RegisterType<MainContext>().As<IApplicationDbContext>();
builder.RegisterType<OneSignalNotificationService>().As<IPushNotificationService>();
builder.RegisterType<LogService>().As<ILogger>();
//Register SignalR Hub
builder.RegisterHubs(Assembly.GetExecutingAssembly());
//builder.RegisterType<ChatHub>().ExternallyOwned();
//Register MVC Controllers
builder.RegisterControllers(Assembly.GetExecutingAssembly());
//Register WebApi Controllers
builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
builder.RegisterAssemblyModules(Assembly.GetExecutingAssembly());
var container = builder.Build();
app.UseAutofacMiddleware(container);
return container;
}
}
WebApiConfiguration.cs
public class WebApiConfiguration
{
public static void Configure(IAppBuilder app, IContainer container, HttpConfiguration config)
{
// Set the dependency resolver for Web API.
var webApiResolver = new AutofacWebApiDependencyResolver(container);
config.DependencyResolver = webApiResolver;
//Configure Action Injection
//config.InjectInterfacesIntoActions();
app.UseAutofacWebApi(config);
}
}
SignalRConfiguration.cs
public static class SignalRConfiguration
{
public static void Configure(IAppBuilder app, IContainer container)
{
//var config = new HubConfiguration();
//config.Resolver = new AutofacDependencyResolver(container);
//config.EnableDetailedErrors = true;
//app.MapSignalR("/signalr", config);
app.Map("/signalr", map =>
{
map.UseAutofacMiddleware(container);
var hubConfiguration = new HubConfiguration
{
Resolver = new AutofacDependencyResolver(container),
EnableDetailedErrors = true
};
map.RunSignalR(hubConfiguration);
});
}
}
MvcConfiguration.cs
public class MvcConfiguration
{
public static void Configure(IAppBuilder app, IContainer container)
{
var mvcResolver = new Autofac.Integration.Mvc.AutofacDependencyResolver(container);
DependencyResolver.SetResolver(mvcResolver);
app.UseAutofacMvc();
}
}
As for the ChatHub
its located in another project as class library with different namespace
as below
ChatHub.cs
namespace PNAME.BotServices.NotificationCenter.SignalR
{
[HubName("ChatHub")]
public class ChatHub : Hub
{
private ILogger _logger;
//private IChatMessageServices _chatMessageServices;
private readonly ILifetimeScope _hubLifetimeScope;
public ChatHub(ILifetimeScope lifetimeScope)
{
// Create a lifetime scope for the hub.
_hubLifetimeScope = lifetimeScope.BeginLifetimeScope("AutofacWebRequest");
// Resolve dependencies from the hub lifetime scope.
_logger = _hubLifetimeScope.Resolve<ILogger>();
// _chatMessageServices = _hubLifetimeScope.Resolve<IChatMessageServices>();
}
public bool SendMessage(INotificationMessage notificationMessage)
{
//Some code here
}
public override Task OnConnected()
{
//Some code here
}
public override Task OnReconnected()
{
//Some code here
}
public async Task UpdateStatus(int messageId, int ServerId, StatusType status)
{
//Some code here
}
protected override void Dispose(bool disposing)
{
// Dispose the hub lifetime scope when the hub is disposed.
if (disposing && _hubLifetimeScope != null)
{
_hubLifetimeScope.Dispose();
}
base.Dispose(disposing);
}
}
}
Thats all the configuration.
As I said in the MVC and API controllers are working well with DI only signalr not connecting from client and failing with the below error.
Microsoft.AspNet.SignalR.Client.HttpClientException: StatusCode: 500, ReasonPhrase: 'Internal Server Error', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
Transfer-Encoding: chunked
X-SourceFiles: =?UTF-8?B?RDpcUHJvamVjdHNcQk9UUmVwb1xCT1Rcc2lnbmFsclxuZWdvdGlhdGU=?=
Cache-Control: private
Date: Sat, 28 Dec 2019 20:43:45 GMT
Server: Microsoft-IIS/10.0
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Content-Type: text/html; charset=utf-8
}
at Microsoft.AspNet.SignalR.Client.Http.DefaultHttpClient.<>c__DisplayClass5_0.<Get>b__1(HttpResponseMessage responseMessage)
at Microsoft.AspNet.SignalR.TaskAsyncHelper.<>c__DisplayClass31_0`2.<Then>b__0(Task`1 t)
at Microsoft.AspNet.SignalR.TaskAsyncHelper.TaskRunners`2.<>c__DisplayClass3_0.<RunTask>b__0(Task`1 t)
don't know what did i miss in this configuration.
Upvotes: 0
Views: 355
Reputation: 136
The issue was with the Hub name after changing the Hub name to "MyChatHub" every thing worked just fine with dependency injection
Upvotes: 0