繁体   English   中英

AutoFac Singalr 依赖注入返回为此 object 错误定义的无参数构造函数

[英]AutoFac Singalr dependency injection returns so parameterless constructor defined for this object error

基于本教程

https://autofac.readthedocs.io/en/latest/integration/signalr.html

到目前为止我做了什么:

在 Global.asax.cs 中:

builder.RegisterType<NotificationHub>().As<INotificationHub>().ExternallyOwned();
var container = builder.Build();
GlobalHost.DependencyResolver = new Autofac.Integration.SignalR.AutofacDependencyResolver(container);
return container;

在我的 Startup.cs 中:

app.MapSignalR(new HubConfiguration { 
                EnableJSONP = true });

我的 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);
    }
}

最后是我的 Js 方法:

$(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);

            }
        });
    }
});

不,当我尝试从 JS 调用 TestMethod 时,它返回“没有为此 object 错误定义无参数构造函数”,如果我删除构造函数部分,它会正常工作。 我在 DotnetFramework 4.5 上使用普通的旧 MVC(没有 Owin 没有核心)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM