gpt4 book ai didi

asp.net-web-api - "The type IUnitOfWork does not have an accessible constructor"与 Umbraco 6.1、UmbracoApiController(Web API)和依赖注入(inject)(Unity)

转载 作者:行者123 更新时间:2023-12-01 13:20:34 24 4
gpt4 key购买 nike

我将 Umbraco 6.1 与 UmbracoApiController 一起使用,它的构造函数中注入(inject)了 IUnitOfWork。为了注入(inject)依赖项,我使用 Unity,就像我过去使用标准 Web API 项目一样。通常,我在 Global.asax.cs 中设置 unity。由于 Umbraco 没有这个,我创建了自己的 UmbracoEvents 处理程序,它继承自 IApplicationEventHandler,并具有以下方法:

  1. OnApplicationInitialized
  2. OnApplicationStarting
  3. OnApplicationStarted
  4. 配置API

在 OnApplicationStarted 方法中,我设置了我的 EF 数据库、数据库初始化程序等,并调用 ConfigureApi 来设置 Unity。我的 OnApplication Started 和 ConfigureApi 方法如下所示:

    public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
_applicationContext = applicationContext;
_umbracoApplication = umbracoApplication;
_contentService = ApplicationContext.Current.Services.ContentService;
this.ConfigureApi(GlobalConfiguration.Configuration);
Database.SetInitializer(null);
PropertySearchContext db = new PropertySearchContext();
db.Database.Initialize(true);
}

private void ConfigureApi(HttpConfiguration config)
{
var unity = new UnityContainer();
unity.RegisterType<PropertiesApiController>();
unity.RegisterType<IUnitOfWork, UnitOfWork>(new HierarchicalLifetimeManager());
config.DependencyResolver = new IoCContainer(unity);
}

我的 Controller 代码:

public class PropertiesApiController : UmbracoApiController
{
private readonly IUnitOfWork _unitOfWork;

public PropertiesApiController(IUnitOfWork unitOfWork)
{
if(null == unitOfWork)
throw new ArgumentNullException();
_unitOfWork = unitOfWork;
}

public IEnumerable GetAllProperties()
{
return new[] {"Table", "Chair", "Desk", "Computer", "Beer fridge"};
}
}

我的范围容器/IoC 容器代码:(根据 http://www.asp.net/web-api/overview/extensibility/using-the-web-api-dependency-resolver )

public class ScopeContainer : IDependencyScope
{
protected IUnityContainer container;

public ScopeContainer(IUnityContainer container)
{
if (container == null)
{
throw new ArgumentNullException("container");
}
this.container = container;
}

public object GetService(Type serviceType)
{
if (container.IsRegistered(serviceType))
{
return container.Resolve(serviceType);
}
else
{
return null;
}
}

public IEnumerable<object> GetServices(Type serviceType)
{
if (container.IsRegistered(serviceType))
{
return container.ResolveAll(serviceType);
}
else
{
return new List<object>();
}
}

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

public class IoCContainer : ScopeContainer, IDependencyResolver
{
public IoCContainer(IUnityContainer container)
: base(container)
{
}

public IDependencyScope BeginScope()
{
var child = this.container.CreateChildContainer();
return new ScopeContainer(child);
}
}

我的 IUnitOfWork 代码:

public interface IUnitOfWork : IDisposable
{
GenericRepository<Office> OfficeRepository { get; }
GenericRepository<Property> PropertyRepository { get; }
void Save();
void Dispose(bool disposing);
void Dispose();
}

我的 UnitOfWork 实现:

public class UnitOfWork : IUnitOfWork
{
private readonly PropertySearchContext _context = new PropertySearchContext();
private GenericRepository<Office> _officeRepository;
private GenericRepository<Property> _propertyRepository;

public GenericRepository<Office> OfficeRepository
{
get
{

if (this._officeRepository == null)
{
this._officeRepository = new GenericRepository<Office>(_context);
}
return _officeRepository;
}
}
public GenericRepository<Property> PropertyRepository
{
get
{

if (this._propertyRepository == null)
{
this._propertyRepository = new GenericRepository<Property>(_context);
}
return _propertyRepository;
}
}

public void Save()
{
_context.SaveChanges();
}

private bool disposed = false;

public virtual void Dispose(bool disposing)
{
if (!this.disposed)
{
if (disposing)
{
_context.Dispose();
}
}
this.disposed = true;
}

public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}

我之前多次将 unity/DI 与 MVC4/WebAPI Controller 和 UnitOfWork 的实现一起使用,没有出现问题,所以我认为它是 Umbraco 特定的。

我还调试了应用程序并确保它命中 OnApplicationStarted 并且它的参数不为空。

Controller 中的 GetAllProperties 方法只是一种测试方法,以确保它一切正常,但是,当我尝试访问此操作时,我收到错误:

“IUnitOfWork 类型没有可访问的构造函数”

有没有人有使用 Umbraco 6.1 的经验,它是具有依赖注入(inject)/Unity 的 UmbracoApiController?

此外,在不相关的说明中,有没有办法在操作中返回 JSON 而不是 XML?在 Web API 中,您只需在 WebApi.config 中定义格式化程序,但 Umbraco 中没有。

谢谢,贾斯汀

最佳答案

如果您还没有找到解决问题的方法?下载此 nuget package在构建统一容器之后:

GlobalConfiguration.Configuration.DependencyResolver =
new Unity.WebApi.UnityDependencyResolver(Bootstrapper.Container);

注意与 Unity.Mvc4.UnityDependencyResolver 不同的命名空间。

关于asp.net-web-api - "The type IUnitOfWork does not have an accessible constructor"与 Umbraco 6.1、UmbracoApiController(Web API)和依赖注入(inject)(Unity),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18250472/

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