C# 使用Unity在注册时注入未知值

C# 使用Unity在注册时注入未知值,c#,asp.net-web-api,dependency-injection,unity-container,C#,Asp.net Web Api,Dependency Injection,Unity Container,这是统一注册 public static class UnityConfig { public static void RegisterComponents() { var container = new UnityContainer(); container.RegisterType<ITestService,TestService>(new InjectionConstructor("XXXXX")); Global

这是统一注册

public static class UnityConfig
{
    public static void RegisterComponents()
    {
        var container = new UnityContainer();
        container.RegisterType<ITestService,TestService>(new InjectionConstructor("XXXXX"));
        GlobalConfiguration.Configuration.DependencyResolver = new UnityDependencyResolver(container);
    }
}
我的测试班

public interface ITestService
{
    string GetText();
}
public class TestService : ITestService
{
    private string _mystring;
    public TestService(string mystring)
    {
        _mystring = mystring;
    }
    public string GetText()
    {
        return _mystring;
    }
}
问题:要注入构造函数的值(此处硬编码为“XXXXX”)仅在
[MyFilter]
中知道,而不是在注册时知道。是否可以注入来自属性的值

谢谢

更新1: 我使用的解决方法是在会话工作中保存值“XXXXX”,但不是很干净

public class MyFilter: AuthorizationFilterAttribute
{
        public override void OnAuthorization(HttpActionContext actionContext)
        {
            try
            {

                HttpContext.Current.Session["MyData"] = "XXXXX";
                    return;

                HandleUnauthorized(actionContext);
            }
            catch (Exception ex)
            {
            }
        }

    private void HandleUnauthorized(HttpActionContext actionContext)
    {
        actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Unauthorized);
    }
}

在解析方法中注入值:

container.RegisterType<ITestService,TestService>();
...
var service = container.Resolve<ITestService>(new ParameterOverride(@"mystring", new InjectionParameter(typeof(string), "XXXXX")));
container.RegisterType();
...
var service=container.Resolve(新参数override(@“mystring”,新注入参数(typeof(string),“XXXXX”));

扩展上面的注释,此示例显示了一种将筛选值存储在属性中并将其恢复以将其传递到
测试服务的方法:

这假设每个
TestController
将使用相同的过滤器(此处
abc
),并且每个
OtherTestController
将使用相同的其他过滤器(如
def
?)。也就是说,附加到类型而不是实例的筛选器

class Program
{
    static void Main( string[] args )
    {
        var unityContainer = new UnityContainer();

        unityContainer.RegisterType<ITestServiceFactory, TestServiceFactory>();

        var theController = unityContainer.Resolve<TestController>();

        // theController._testService.FilterString is "abc"
    }
}

[MyFilter("abc")]
public class TestController
{
    private ITestService _testService;
    public TestController( ITestServiceFactory testServiceFactory )
    {
        _testService = testServiceFactory.CreateTestService(this);
    }
}

public class MyFilterAttribute : Attribute
{
    public string FilterString
    {
        get;
    }

    public MyFilterAttribute( string filterString )
    {
        FilterString = filterString;
    }
}

public interface ITestServiceFactory
{
    ITestService CreateTestService( object owner );
}

public interface ITestService
{
    string GetText();
}

internal class TestServiceFactory : ITestServiceFactory
{
    public ITestService CreateTestService( object owner )
    {
        return new TestService( ((MyFilterAttribute)owner.GetType().GetCustomAttribute( typeof( MyFilterAttribute ) )).FilterString );
    }
}

internal class TestService : ITestService
{
    public TestService( string mystring )
    {
        _mystring = mystring;
    }
    public string GetText()
    {
        return _mystring;
    }

    private readonly string _mystring;
}

请发布
MyFilter
的代码。过滤器中配置的值
XXXXX
在哪里?例如,您可以使用自定义类型转换器并从配置中选择值。您可以在解析
ITestService
时插入
mystring
的值。您可以改为注册工厂,并在工厂创建实际的
ITestService
时传递该值。它还可以传入自己的类型,这样工厂就可以从该类型的自定义属性中获取值
class Program
{
    static void Main( string[] args )
    {
        var unityContainer = new UnityContainer();

        unityContainer.RegisterType<ITestServiceFactory, TestServiceFactory>();

        var theController = unityContainer.Resolve<TestController>();

        // theController._testService.FilterString is "abc"
    }
}

[MyFilter("abc")]
public class TestController
{
    private ITestService _testService;
    public TestController( ITestServiceFactory testServiceFactory )
    {
        _testService = testServiceFactory.CreateTestService(this);
    }
}

public class MyFilterAttribute : Attribute
{
    public string FilterString
    {
        get;
    }

    public MyFilterAttribute( string filterString )
    {
        FilterString = filterString;
    }
}

public interface ITestServiceFactory
{
    ITestService CreateTestService( object owner );
}

public interface ITestService
{
    string GetText();
}

internal class TestServiceFactory : ITestServiceFactory
{
    public ITestService CreateTestService( object owner )
    {
        return new TestService( ((MyFilterAttribute)owner.GetType().GetCustomAttribute( typeof( MyFilterAttribute ) )).FilterString );
    }
}

internal class TestService : ITestService
{
    public TestService( string mystring )
    {
        _mystring = mystring;
    }
    public string GetText()
    {
        return _mystring;
    }

    private readonly string _mystring;
}
    public TestController( ITestServiceFactory testServiceFactory )
    {
        _testService = testServiceFactory.CreateTestService("abc");
    }