Umbraco 6.1과 UmbracoApiController를 사용하고 있는데, IUnitOfWork가 생성자에 삽입되어 있습니다. 종속성을 주입하려면 과거의 표준 웹 API 프로젝트와 마찬가지로 Unity를 사용하고 있습니다. 일반적으로 Global.asax.cs에서 단결을 설정합니다. Umbraco이되어 있지 않기 때문에 나는 IApplicationEventHandler에서 상속 내 자신의 UmbracoEvents 핸들러를 생성하고, 방법이있다 :Umbraco 6.1, UmbracoApiController (웹 API) 및 종속성 삽입 (Unity)이있는 IUnitOfWork 유형에 액세스 가능한 생성자가 없습니다.
- 는
- OnApplicationStarting
- 가에서 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);
}
내 컨트롤러 코드 :
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에 구현 : 나는 통일/MVC4/WebAPI 컨트롤러와 DI 및 이전 문제없이 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);
}
}
은 Umbraco의 특정입니다.
또한 응용 프로그램을 디버깅하고 OnApplicationStarted에 도달하고 해당 매개 변수가 null이 아닌지 확인했습니다.
"유형의 IUnitOfWork가 없습니다 : 내가 노력하고 내가 오류가이 작업에 액세스 할 때
컨트롤러의 GetAllProperties 방법은, 그러나, 모든 잘 작동하는지 확인하기 위해 단지 시험 방법 접근 가능한 생성자 "
누구나 Umbraco 6.1을 사용한 경험이 있고 UmbracoApiController와 종속성 주입/Unity가 있습니까?
또한 관련없는 메모에서 작업 대신 XML 대신 JSON을 반환 할 수 있습니까? 웹 API에서는 WebApi.config 파일에 형식기를 정의하지만 Umbraco에는 아무 것도 없습니다.
덕분에, 저스틴
경우
제안 1 : 기본 생성자를 'UnitOfWork'에 추가하면 어떻게됩니까? – Halvard
제안 2 :이 질문을 본 적이 있습니까? http://stackoverflow.com/questions/17898433/issue-resolving-dependencies-with-unity – Halvard
예, 나는 그것을 보았습니다. 내 ConfigureApi 메서드에서 IUnitOfWork/UnitOfWork 형식을 HierarchicalLifetimeManager (항상 그래도 상관 없음)로 등록합니다. 또한 인스턴스가 아닌 유형을 등록합니다. 아마 컨트롤러 대신 인스턴스를 등록해야합니다. 내가 실제로 그것을 사용했을 때 컴파일 타임에 그것을 버렸다면 나는 생각대로 명시 적으로 빈 생성자를 선언하려하지 않았다? 나는 어쨌든 그것을 시도하고 다시보고 할 것이다 :) – JustinMoser