gpt4 book ai didi

c# - 在 Unity 中使用 DbContextScopeFactory

转载 作者:行者123 更新时间:2023-11-30 17:42:31 27 4
gpt4 key购买 nike

我正在尝试使用 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());

但我收到服务器错误:“类型 IDbContextFactory 没有可访问的构造函数。”

我也试过:

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, 类型controllerType) +110

[InvalidOperationException:尝试创建“Portal.Controllers.VendorController”类型的 Controller 时发生错误。确保 Controller 有一个无参数的公共(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 step, Boolean& completedSynchronously) +155

我有一个构造函数

public class VendorController : Controller
{
IVendorService _VendorService;

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

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

最佳答案

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

Web API 尝试使用您没有的默认构造函数来实例化 Controller (因为您期望将依赖项注入(inject)到构造函数中)。您需要将 Unity 设置为依赖项解析器以连接 Controller 依赖项。您可以添加 Unity bootstrapper for ASP.NET Web API来自 NuGet。这将为您注册一个 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 将在网络请求结束时自动处理 IDisposable 对象。

关于c# - 在 Unity 中使用 DbContextScopeFactory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31930179/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com