в настоящее время возникает следующая ошибка
Конструктор типа ChurchController содержит параметр с именем 'churchService' и незарегистрированным типом ICurchService. Убедитесь, что ICurchService зарегистрирован, или измените конструктор ChurchController.
я использую Simple Injector, но я новичок в IOC, не уверен, что означает сообщение об ошибке или что я сделал неправильно, я включил мой код ниже
SimpleInjectorWebApiInitializer.cs
public static class SimpleInjectorWebApiInitializer
{
public static void Initialize()
{
var container = new Container();
container.Options.DefaultScopedLifestyle = new AsyncScopedLifestyle();
InitializeContainer(container);
container.RegisterWebApiControllers(GlobalConfiguration.Configuration);
container.Verify();
GlobalConfiguration.Configuration.DependencyResolver =
new SimpleInjectorWebApiDependencyResolver(container);
}
private static void InitializeContainer(Container container)
{
// For instance:
container.Register(typeof(IRepository<>), typeof(EFRepository<>));
}
}
public interface IRepository<T> where T : class
{
//To query using LINQ
IQueryable<T> GetAll();
//Returning Movie or Review by id
T GetById(int id);
//Adding Movie or Review
void Add(T entity);
//Updating Movie or Review
void Update(T entity);
//Deleting Moovie or Review
void Delete(T entity);
//Deleting Movie or Review by id
void Delete(int id);
}
public class EFRepository<T> : IRepository<T> where T : class
{
public EFRepository(DbContext dbContext)
{
if (dbContext == null)
throw new ArgumentNullException("dbContext");
DbContext = dbContext;
DbSet = DbContext.Set<T>();
}
protected DbContext DbContext { get; set; }
protected DbSet<T> DbSet { get; set; }
public virtual IQueryable<T> GetAll()
{
return DbSet;
}
public virtual T GetById(int id)
{
return DbSet.Find(id);
}
public virtual void Add(T entity)
{
DbEntityEntry dbEntityEntry = DbContext.Entry(entity);
if (dbEntityEntry.State != EntityState.Detached)
{
dbEntityEntry.State = EntityState.Added;
}
else
{
DbSet.Add(entity);
}
}
public virtual void Update(T entity)
{
DbEntityEntry dbEntityEntry = DbContext.Entry(entity);
if (dbEntityEntry.State != EntityState.Detached)
{
DbSet.Attach(entity);
}
dbEntityEntry.State = EntityState.Modified;
}
public void Delete(T entity)
{
DbEntityEntry dbEntityEntry = DbContext.Entry(entity);
if (dbEntityEntry.State != EntityState.Deleted)
{
dbEntityEntry.State = EntityState.Deleted;
}
else
{
DbSet.Attach(entity);
DbSet.Remove(entity);
}
}
public void Delete(int id)
{
var entity = GetById(id);
if (entity == null) return;
Delete(entity);
}
}
моя модель
public class Church
{
public int Id { get; set; }
public string ChurchName { get; set; }
public virtual ICollection<User> Users { get; set; }
}
настроить мой контроллер для вызова моей службы
public class ChurchController : ApiController
{
private readonly IChurchService _churchService;
public ChurchController(IChurchService churchService)
{
_churchService = churchService;
}
public IEnumerable<Church> Get()
{
var result = _churchService.GetAll();
return result;
}
}
IChurchService
в контейнере? Ваш код этого не показывает. ПосколькуIChurchService
является абстракцией, Simple Injector не может волшебным образом угадать тип реализации за вас: вам придется указать Simple Injector, какой тип использовать. - person Steven   schedule 28.06.2017