在Unity注册时注入未知值

时间:2016-02-11 06:23:16

标签: c# asp.net-web-api dependency-injection unity-container

这里是Unity注册

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

这里是webApi控制器

[MyFilter]
public class TestController : ApiController
{
    private ITestService _testService;
    public EmployeeController(ITestService testService)
    {
        _testService = testService;
    }
}

我的考试班

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]中知道,而不是在注册时。是否可以注入来自属性的值?

谢谢,

UPDATE1: 我使用的解决方法是在会话工作中保存值“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);
    }
}

2 个答案:

答案 0 :(得分:0)

在解决方法中注入值:

container.RegisterType<ITestService,TestService>();
...
var service = container.Resolve<ITestService>(new ParameterOverride(@"mystring", new InjectionParameter(typeof(string), "XXXXX")));

答案 1 :(得分:0)

扩展上面的评论,此示例显示了一种在属性中存储过滤器值并将其恢复以将其传递到onEventMainThread的方法:

这假设每个TestService将使用相同的过滤器(此处为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; } 的构造函数中设置过滤器:

TestController