gpt4 book ai didi

c# - 使用 Autofac 将 SignalR IHubContext 注入(inject) Controller

转载 作者:行者123 更新时间:2023-12-03 20:53:39 27 4
gpt4 key购买 nike

我正在尝试注入(inject) SignalR IHubContext进入 ASP.NET MVC 5 应用程序框架 4.72(不是 .NET Core)中的 Web API 2.x Controller 。调用 Web API Controller 时抛出此异常 MyController :

An error occurred when trying to create a controller of type 'MyController'. Make sure that the controller has a parameterless public constructor



内部异常说:

None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'MyController' can be invoked with the available services and parameters: Cannot resolve parameter 'Microsoft.AspNet.SignalR.IHubContext[MyHub] context' of constructor 'Void .ctor(Microsoft.AspNet.SignalR.IHubContext [MyHub])'.



我不介意使用属性注入(inject)来做到这一点,但没有任何运气让它发挥作用。所以我正在注入(inject) Controller 的c'tor。

我已按照以下答案寻求帮助:
  • https://stackoverflow.com/a/37913821/177416 --> c'tor 注入(inject)
  • https://stackoverflow.com/a/29793864/177416 --> c'tor 注入(inject)
  • https://stackoverflow.com/a/26810399/177416 --> 属性注入(inject)
  • https://stackoverflow.com/a/15600493/177416 --> 属性注入(inject)

  • 这是 Web API Controller :
    public class MyController : WebApiController
    {
    public IHubContext<MyHub> Context { get; set; }

    public MyController(IHubContext<MyHub> context)
    {
    Context = context;
    }
    }

    这是 Startup.cs 的相关部分:
    public void Configuration(IAppBuilder app)
    {
    // Other code...

    var builder = new ContainerBuilder();
    var config = new HttpConfiguration();

    builder.RegisterHubs(Assembly.GetExecutingAssembly());
    builder.RegisterControllers(typeof(MvcApplication).Assembly)
    .InstancePerRequest();
    builder.RegisterApiControllers(Assembly.GetExecutingAssembly())
    .InstancePerRequest();
    builder.RegisterType<AutofacDependencyResolver>()
    .As<IDependencyResolver>()
    .SingleInstance();

    builder
    .Register(c => c.Resolve<IConnectionManager>().GetHubContext<MyHub>())
    .Named<IHubContext>("MyHub");
    builder.RegisterType<MyController>()
    .WithParameter(
    new ResolvedParameter(
    (pi, ctx) => pi.ParameterType == typeof(IHubContext),
    (pi, ctx) => ctx.ResolveNamed<IHubContext>("MyHub")
    )
    );

    var container = builder.Build();
    app.UseAutofacMiddleware(container);

    DependencyResolver.SetResolver(new Autofac.Integration.Mvc.AutofacDependencyResolver(container));
    config.DependencyResolver = new AutofacWebApiDependencyResolver((IContainer)container);

    app.Map("/signalr", map =>
    {
    var hubConfiguration = new HubConfiguration
    {
    Resolver = new AutofacDependencyResolver(container),
    };

    map.RunSignalR(hubConfiguration);
    });
    }

    我错过了什么?谢谢。

    最佳答案

    你的第一个问题是 typeof(IHubContext)typeof(IHubContext<MyHub>) 不一样.您可以使用以下方法解决此问题:
    pi.ParameterType == typeof(IHubContext).MakeGenericType(typeof(MyHub))
    但是,旧版本的 SignalR 不能很好地支持通用接口(interface),因此如果您保持比较不变并注入(inject) IHubContext 可能会更好。而不是 IHubContext<MyHub>MyController .

    关于c# - 使用 Autofac 将 SignalR IHubContext 注入(inject) Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61828792/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com