Base on this tutorial
https://autofac.readthedocs.io/en/latest/integration/signalr.html
what ive done so far :
In Global.asax.cs :
builder.RegisterType<NotificationHub>().As<INotificationHub>().ExternallyOwned();
var container = builder.Build();
GlobalHost.DependencyResolver = new Autofac.Integration.SignalR.AutofacDependencyResolver(container);
return container;
In My Startup.cs :
app.MapSignalR(new HubConfiguration {
EnableJSONP = true });
My NotificationHub.cs :
[HubName("NotificationHub")]
public class NotificationHub : Hub, INotificationHub
{
private readonly ILifetimeScope _hubLifetimeScope;
public static IQueryManagerContributor QueryManager;
public NotificationHub(ILifetimeScope lifetimeScope)
{
// Create a lifetime scope for the hub.
_hubLifetimeScope = lifetimeScope.BeginLifetimeScope("AutofacWebRequest");
// Resolve dependencies from the hub lifetime scope.
QueryManager = _hubLifetimeScope.Resolve<IQueryManagerContributor>();
}
public void TestMethod()
{
}
protected override void dispose(bool disposing)
{
// dipose the hub lifetime scope when the hub is disposed.
if (disposing && _hublifetimescope != null)
{
_hublifetimescope.dispose();
}
base.dispose(disposing);
}
}
And finaly my Js method :
$(document).ready(function () {
var connection = $.hubConnection("/signalr", { useDefaultPath: false });
connection.client = function () { };
var hub = connection.createHubProxy("NotificationHub");
hub.on("AddMessage", Method);
connection.start({ jsonp: true })
.done(function () {
console.log('connected');
hub.invoke('TestMethod');
})
.fail(function (a) {
console.log('not connected' + a);
});
connection.disconnected(function () {
setTimeout(function () {
connection.start({ jsonp: true })
.done(function () {
console.log('ReConnected');
hub.invoke('TestMethod');
})
.fail(function (a) {
console.log('not ReConnected' + a);
});;
}, 5000);
});
function Method(messageFromHub) {
Notification.requestPermission().then((result) => {
if (result === 'granted') {
const options = {
body: messageFromHub,
// icon: notifImg,
};
new Notification('TestMethod', options);
}
});
}
});
No when Im trying to invoke TestMethod from JS it returns " No parameterless constructor defined for this object error" and if i remove the constructor part it works correctly. Im using plain old MVC on DotnetFramework 4.5(No Owin No Core)