5

我通过设置一个普通的集线器(包括 JS 文件)成功地在我的 MVC4 项目中使用了 Signalr,/signalr/hubs它可以正常工作。

现在我正在尝试统一设置 DI:

Global.asax

UnityContainer = Bootstrapper.Initialise();
var unityDependencyResolver = new UnityDependencyResolver(UnityContainer);

// Used for MVC
DependencyResolver.SetResolver(unityDependencyResolver);

// Used for SignalR
GlobalHost.DependencyResolver = new SignalRUnityDependencyResolver(UnityContainer);

RouteTable.Routes.MapHubs();

Bootstrapper.cs

public static IUnityContainer Initialise()
{
    var unityContainer = new UnityContainer();

    unityContainer.RegisterType<IUsers, Users>();

    unityContainer.RegisterType<ChatHub>(new InjectionFactory(CreateMyHub));

    return unityContainer;
}

private static object CreateMyHub(IUnityContainer p)
{
    return new ChatHub(p.Resolve<IUsers>());
}

和集线器: public class UserHub : Hub { private readonly IUsers _users;

public ChatHub(IUsers users)
{
    _users = users;
}

public void Send(String message)
{
    Clients.All.addMessage(message);
}

}

我的 SignalRUnityDependencyResolver.cs

public class SignalRUnityDependencyResolver : DefaultDependencyResolver
{
    private readonly IUnityContainer _container;

    public SignalRUnityDependencyResolver(IUnityContainer container)
    {
        if (container == null)
        {
            throw new ArgumentNullException("container", "Containet cannot be null");
        }
        _container = container;
    }

    public override Object GetService(Type serviceType)
    {
        return _container.IsRegistered(serviceType) ? _container.Resolve(serviceType) : base.GetService(serviceType);
    }

    public override IEnumerable<Object> GetServices(Type serviceType)
    {
        return _container.IsRegistered(serviceType) ? _container.ResolveAll(serviceType) : base.GetServices(serviceType);
    }
}

这适用于非集线器,因为所有依赖项都已解决,但现在<script src="~/signalr/hubs"></script>不再生成 javascript 文件。

我该如何调试这一代的 JS,罪魁祸首在哪里?

4

1 回答 1

0

您可以在配置 EnableJavaScriptProxies = false 中使用它

像这样的代码:

        app.Map("/signalr", map =>
        {
            map.UseCors(CorsOptions.AllowAll);
            var hubConfiguration = new HubConfiguration
            {
                EnableJSONP = true,

                EnableJavaScriptProxies = false
            };

            map.RunSignalR(hubConfiguration);
        });
于 2015-12-12T01:47:43.500 回答