【问题标题】:Web Api OWIN Host with Unity带有 Unity 的 Web Api OWIN 主机
【发布时间】:2017-03-29 13:13:02
【问题描述】:

我正在尝试在我的 WebApi2 应用程序上实现 UNITY。 问题是我正在使用现有的 SqlConnection,这取决于在资源 URL 中找到的标识符。 所以我需要请求 uri 中提供的标识符来创建我的上下文。 是否可以从请求 URI 中获取 {dbIdentifier},并将其解析到 MyRepo 的构造函数中?

请求用户界面如下所示:/api/{dbIdentifier}/{controller}/{id}

结构看起来...

Request POST /api/myDbIdentifier/my/ + PAYLOAD data

Controller:
public class MyController : ApiController
{
    private readonly IRepo _repo;

    public MyController(IRepo repo)
    {
        _repo = repo;
    }
}

Repo:
public class MyRepo : IRepo
{
    private readonly MyContext _context;

    public  MyRepo(string dbIdentifier)
    {
        _context = new MyContext(GetConnection(dbIdentifier));
    }

    public void Insert(string s)
    {
        //Inserting string in context and save changes
    }

    private DbConnection(string id)
    {
        //psudo find connecion from pool, and return instance of DbConnection...
    }
}

public interface IRepo
{
    void Insert(string s);
}

Context:
public class MyContext : DbContext
{
    public MyContext(DbConnection exitingConnection) : base(existingConnection, true)
    { }
}

顺便说一句,这是我第一次玩 WebApi 和 Unity,所以请原谅我的无知。

更新了我的代码的 Unity 部分...

UnityResolver(取自https://docs.microsoft.com/en-us/aspnet/web-api/overview/advanced/dependency-injection):

public class UnityResolver : IDependencyResolver
{
    protected IUnityContainer Container;

    public UnityResolver(IUnityContainer container)
    {
        if (container == null)
        {
            throw new ArgumentNullException(nameof(container), "Please provider an IUnityContainer.");
        }
        Container = container;
    }

    public void Dispose()
    {
        Container.Dispose();
    }

    public object GetService(Type serviceType)
    {
        try
        {
            return Container.Resolve(serviceType);
        }
        catch (ResolutionFailedException)
        {
            return null;
        }
    }

    public IEnumerable<object> GetServices(Type serviceType)
    {
        try
        {
            return Container.ResolveAll(serviceType);
        }
        catch (Exception)
        {
            return new List<object>();
        }
    }

    public IDependencyScope BeginScope()
    {
        return new UnityResolver(Container.CreateChildContainer());
    }
}

我的启动中的 Unity 注册部分:

public static void Register(HttpConfiguration config)
{
    // Configuring DI Container fo IoC (Invert of Control)
    var container = new UnityContainer();
    container.RegisterType<IRepo, MyRepo>(new HierarchicalLifetimeManager());
    config.DependencyResolver = new UnityResolver(container);
}

【问题讨论】:

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


    【解决方案1】:

    您可以尝试以下方法:

    1)创建一个DelegatingHandler,您可以在其中访问HtppRequestMessage.RequestUri

    2)从 Uri 中提取 dbIdentifier

    3)用一个类(例如 DbIdentifier)包装 dbIdentifier 并使用 HierarchicalLifetimeManager 将其统一注册

    4)记得在owin中注册handler:

    httpConfiguration.MessageHandlers.Add(new DbIdentifierHandler());
    

    编辑。 您可以查看这篇文章以找到一些灵感: How to pass Owin context to a Repo being injected into Api controller

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-05
      • 2016-10-24
      • 2015-11-11
      • 2015-11-01
      • 2016-01-28
      • 2014-10-05
      • 2018-02-02
      • 1970-01-01
      相关资源
      最近更新 更多