在 Unity 中使用 DbContextScopeFactory

Using DbContextScopeFactory with Unity

我正在尝试使用 DBContextScopeFactory 和 运行 成一个问题。

public class VendorRepository : IVendorRepository
{
    private readonly IDbContextScopeFactory _dbContextScopeFactory;

    public VendorRepository(IDbContextScopeFactory dbContextScopeFactory)
    {
        if (dbContextScopeFactory == null)
        {
            throw new ArgumentNullException("dbContextScopeFactory");
        }
        _dbContextScopeFactory = dbContextScopeFactory;
    }
    public IList<MyVendor> GetMyVendors(int requestorId)
    {
        IList<MyVendor> vndrsList = null;
        using (var dbContextScope = _dbContextScopeFactory.CreateReadOnly())
    {
            var dbContext = dbContextScope.DbContexts.Get<VendorMgmtContext>();
            var vndrs = (from a in dbContext.sp_GetVendorByRequestor(requestorId)
                     select new MyVendor
                     {
                         VendorId = a.VendorId,
                         VendorName = a.VendorName,
                         CreatedByUserId = a.CreatedByUserId,
                         CreatedDate = a.CreatedDate
                         });
            vndrsList = vndrs.ToList();
        }
        return vndrsList;
    }

}

单元测试效果很好,因为它类似于演示应用程序项目。 (换句话说,我没有在 UnityConfig.cs 文件中注册它。

[TestMethod]
public void GetMyVendors()
{
    // Arrange
    var dbContextScope = new DbContextScopeFactory();
    var db = new VendorMgmtContext();
    VendorRepository repository = new VendorRepository(dbContextScope);
    IEnumerable<MyVendor> myVendor = null;

    // Act
    myVendor = repository.GetMyVendors(3);

    // Assert
    Assert.IsNotNull(myVendor);
}

所以现在我不太确定如何在 UnityConfig.cs 文件中注册它 我试过了:

container.RegisterType<IAmbientDbContextLocator, AmbientDbContextLocator>();
container.RegisterType<IDbContextScopeFactory, DbContextScopeFactory>();
container.RegisterType<VendorMgmtContext, VendorMgmtContext>(new PerResolveLifetimeManager(), new InjectionConstructor());
container.RegisterType<IVendorRepository, VendorRepository>(new PerResolveLifetimeManager());
container.RegisterType<IVendorService, VendorService>(new PerResolveLifetimeManager());

但是我收到一个服务器错误:"The type IDbContextFactory does not have an accessible constructor. "

我也试过:

container.RegisterType<IAmbientDbContextLocator, AmbientDbContextLocator>();
container.RegisterType<IDbContextScopeFactory, DbContextScopeFactory>(new InjectionFactory((a) => new DbContextScopeFactory()));

并且还收到错误。我很确定这是我注册这个新实现的方式。预先感谢您的帮助。

更新:注册错误:

container.RegisterType<IDbContextScopeFactory, DbContextScopeFactory>(new InjectionFactory((a) => new DbContextScopeFactory()));

Message: "An error occurred when trying to create a controller of type 'VendorController'. Make sure that the controller has a parameterless public constructor"
ExceptionType: System.InvalidOperationException
StackTrace:
at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)
at System.Web.Http.Controllers.HttpControllerDescriptor.CreateController(HttpRequestMessage request)
at System.Web.Http.Dispatcher.HttpControllerDispatcher.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__0.MoveNext()
InnerException:
Message: "Type 'Portal.Controllers.Api.VendorController' does not have a default constructor"
ExceptionType: System.ArgumentException
StackTrace:
at System.Linq.Expressions.Expression.New(Type type)
at System.Web.Http.Internal.TypeActivator.Create[TBase](Type instanceType)
at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage request, Type controllerType, Func`1& activator)
at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)

更新 #2 - 更新了 'Unity.AspNet.WebApi 3.5.1404.0' 并将 Unity 从 'Unity 3.0.1304.1' 更新为 'Unity 3.5.1404.0' 我还将所有注册从 WebApiConfig.cs 移到了 UnityConfig.cs文件。最后,我从

修改了 Global.asax.cs
 UnityConfig.RegisterComponents();

 UnityWebApiActivator.Start();

我的 webAPI 终于可以运行了。然而,我仍然在 MVC 端遇到相同的异常。

[MissingMethodException: 没有为此对象定义无参数构造函数。] System.RuntimeTypeHandle.CreateInstance(RuntimeType 类型, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck) +0 System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) +119 System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) +232 System.Activator.CreateInstance(Type类型,Boolean nonPublic) +83 System.Activator.CreateInstance(类型类型)+11 System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +110

[InvalidOperationException:尝试创建 'Portal.Controllers.VendorController' 类型的控制器时出错。确保控制器具有无参数 public 构造函数。] System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +247 System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType) +437 System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName) +257 System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory) +326 System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback 回调, 对象状态) +157 System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback 回调, 对象状态) +88 System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext 上下文,AsyncCallback cb,对象 extraData)+50 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +103 System.Web.HttpApplication.ExecuteStep(IExecutionStep 步骤,布尔型& completedSynchronously)+155

我有构造函数

public class VendorController : Controller
{
    IVendorService _VendorService;

    public VendorController(IVendorService VendorSvc)
    {
        _VendorService = VendorSvc;
    }

    // GET: Vendor
    public ActionResult Index()
    {
        return View();
    }
}

错误消息清楚地说明了问题所在。您没有正确设置 IControllerFactoryIDependencyResolver 以使用 Unity 解析实例。与您的注册无关。

ControllerBuilder.Current.SetControllerFactory(new UnityControllerFactory(container));

DependencyResolver.SetResolver(new UnityDependencyResolver(container));

请注意,您可以自己构建其中一个,也可以使用来自某处的现有实现。但还要注意 using IControllerFactory is considered the "correct" approach to DI.

根据您发布的堆栈跟踪,您似乎正在尝试调用 Web API 控制器而不是 ASP.NET Mvc 控制器(正如问题的标签所暗示的那样)。此外,错误不是在注册期间发生,而是在控制器实例化期间发生。

Web API 尝试使用您没有的默认构造函数来实例化控制器(因为您期望将依赖项注入构造函数)。您需要将 Unity 设置为依赖项解析器以连接控制器依赖项。您可以从 NuGet 添加 Unity bootstrapper for ASP.NET Web API。这将为您注册一个 UnityDependencyResolver(在 UnityWebApiActivator.cs 中):

var resolver = new Microsoft.Practices.Unity.WebApi.UnityDependencyResolver(UnityConfig.GetConfiguredContainer());

GlobalConfiguration.Configuration.DependencyResolver = resolver;

您还可以将所有注册码移至 UnityConfig.cs。

我看到您正在使用 PerResolveLifetimeManager -- 您可能需要考虑更改为使用与 Unity bootstrapper for ASP.NET MVC 一起安装的 PerRequestLifetimeManager。 PerRequestLifetimeManager 将在 Web 请求结束时自动处理 IDisposable 对象。