Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 是否可以在自定义OWIN中间件方法中注册依赖项?_C#_.net_Dependency Injection_Asp.net Web Api2_Owin - Fatal编程技术网

C# 是否可以在自定义OWIN中间件方法中注册依赖项?

C# 是否可以在自定义OWIN中间件方法中注册依赖项?,c#,.net,dependency-injection,asp.net-web-api2,owin,C#,.net,Dependency Injection,Asp.net Web Api2,Owin,有一个web-api2应用程序。共享库中有一些自定义属性(web api应用程序引用此库)。此外,这个共享库包含AppBuilderExtension(比如app.UseMyCustomAttribute(新的MySettings)) 该属性需要注入自定义BL服务: [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = false, AllowMultiple = true)] public c

有一个web-api2应用程序。共享库中有一些自定义属性(web api应用程序引用此库)。此外,这个共享库包含AppBuilderExtension(比如app.UseMyCustomAttribute(新的MySettings))

该属性需要注入自定义BL服务:

[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = false, AllowMultiple = true)]
public class MyAttribute : FilterAttribute, IAuthenticationFilter
{
    public async Task AuthenticateAsync(HttpAuthenticationContext context, CancellationToken cancellationToken)
    {
      var myService = context.Request
            .GetDependencyScope()
            .GetService(typeof(IMyService)) as IMyService;
      await _myService.DoSomething();
    }
问题是:是否可以在我的共享库中的UseMyCustomAttribute()扩展中注册IMyService?出于此目的,最好不要在共享库中引用任何IoC库(Autofac,Unity)。(换句话说,不要求共享库的使用者在每次需要MyAttribute时都进行istantiate和IMyService注入)类似于:

    public static IAppBuilder UseMyCustomAttribute(this IAppBuilder app, HttpConfiguration config, MySettings settings)
    {
        config.Services.Add(typeof(IMyService), new MyService(settings.Url));
        return app;
    }

此方法引发异常。(正如所解释的,服务是针对预定义的、众所周知的服务的。)如何将MyService添加到应用程序服务容器中,而不使用任何DI/IoC LIB(如Autofac、Unity等)。在共享库中实现UseMyCustomAttribute(…)方法的最佳解决方案是什么

已更新

我的问题不是:“如何在属性中注册依赖项?”(),而是如何为库属性注册依赖项?是否可以在库方法中执行此操作,例如owin.UseMyAttribute()?在上面的UseMCustomAttribute()方法中,我应该如何注册我的自定义IMyService属性

是否可以在我的共享库中的UseMyCustomAttribute()扩展中注册IMyService


是的,这是可能的,但你不应该。每个应用程序应该只有一个,组合根通常应该是。这意味着共享库不应具有任何DI引导逻辑,也不应依赖于DI库。

相关内容:@Steven我的问题不是关于“如何在属性内注册依赖关系?”(回答如下),而是如何注册库属性的依赖关系?是否可以在库方法中执行此操作,例如owin.UseMyAttribute()?在上面的UseMyCustomAttribute()方法中,我应该如何注册我的自定义IMyService for attribute?好的,因此根据这个想法,在任何sahred库中使用OWIN DependencyResolver也是不好的风格?在这种情况下,我的案例的正确解决方案是什么?在我的共享库(靠近属性类)中手动实现IMyService的普通旧单音的实例化?另外,您能否提供一个示例,说明我“不应该在我的共享库中的UseMyCustomAttribute()扩展中注册IMyService”,@n.piskunov:您目前正在使用
MyAttribute
作为一个谦逊的对象,并从
DependencyScope
解析
IMyService
,如果您只有其中的一些属性(如前所述),这是很好的。这意味着您应该在组合根目录中注册您的
MyService
(在您的情况下,可能是
Configuration
方法)。这应该是你要做的全部,或者你的问题中有什么我遗漏的吗?谢谢你的帮助。我更新了我的问题,以更清楚地解释我的问题。我的主要问题是-如何在库方法UseMyCustomAttribute(…)中正确注册MyService。一些代码示例将特别有用。我的答案不变:不要在扩展方法中注册。
    public static IAppBuilder UseMyCustomAttribute(this IAppBuilder app, HttpConfiguration config, MySettings settings)
    {
        config.Services.Add(typeof(IMyService), new MyService(settings.Url));
        return app;
    }