asp.net-mvc类型的服务在autofac中分配时为何会出现该类型不能分配的异常问题?
- 内容介绍
- 文章标签
- 相关推荐
本文共计329个文字,预计阅读时间需要2分钟。
我有一个这样的接口:`public interface ICategoryFacade { IEnumerable Get(); Category Get(int id); int Post(Category model); int Put(Category model); int Patch(Category model); int Delete(int id); }` 上课学习了:`public class Cate`
我有这样的界面:public interface ICategoryFacade { IEnumerable<Category> Get(); Category Get(int id); int Post(Category model); int Put(Category model); int Patch(Category model); int Delete(int id); }
我上课了:
public class CategoryFacade : ICategoryFacade { MyContext _dbContext = new MyContext (); public IEnumerable<Category> Get() { return _dbContext.Categories; } public Category Get(int id) { return _dbContext.Categories.FirstOrDefault(m => m.CategoryID == id); } public int Post(Category model) { return 0; } public int Put(Category model) { return 0; } public int Patch(Category model) { return 0; } public int Delete(int id) { return 0; } }
现在我想将IOC与我的ODATA控制器一起使用.所以为了做到这一点,我在Global.asax文件中使用以下代码行(Application_Start事件):
var builder = new ContainerBuilder(); builder.RegisterApiControllers(Assembly.GetExecutingAssembly()); builder.RegisterInstance(new CategoryFacade()).As<ICategoryFacade>(); builder.RegisterInstance(new TVideoFacade()).As<ITVideoFacade>(); builder.RegisterType<CategoryController>(); //builder.RegisterType<CategoryFacade>().As<ICategoryFacade>(); //builder.RegisterType<TVideoFacade>().As<ITVideoFacade>(); //builder.Register(c => new CategoryFacade()).As<ICategoryFacade>().InstancePerRequest(); //builder.Register(c => new TVideoFacade()).As<ITVideoFacade>().InstancePerRequest(); var container = builder.Build(); var resolver = new AutofacWebApiDependencyResolver(container); GlobalConfiguration.Configuration.DependencyResolver = resolver;
但它显示以下异常:
Additional information: The type 'ABC.ABCDatabase.Facades.TVideoFacade' is not assignable to service 'ABC.ABCDatabase.Abstractions.ICategoryFacade'.
你能告诉我在OData控制器上使用autofac的正确方法吗?我是autofac的新手.
我认为你还没有在TVideoFacade类中实现ITVideoFacade接口. 只需确保您已在TVideoFacade类中实现了ITVideoFacade接口.本文共计329个文字,预计阅读时间需要2分钟。
我有一个这样的接口:`public interface ICategoryFacade { IEnumerable Get(); Category Get(int id); int Post(Category model); int Put(Category model); int Patch(Category model); int Delete(int id); }` 上课学习了:`public class Cate`
我有这样的界面:public interface ICategoryFacade { IEnumerable<Category> Get(); Category Get(int id); int Post(Category model); int Put(Category model); int Patch(Category model); int Delete(int id); }
我上课了:
public class CategoryFacade : ICategoryFacade { MyContext _dbContext = new MyContext (); public IEnumerable<Category> Get() { return _dbContext.Categories; } public Category Get(int id) { return _dbContext.Categories.FirstOrDefault(m => m.CategoryID == id); } public int Post(Category model) { return 0; } public int Put(Category model) { return 0; } public int Patch(Category model) { return 0; } public int Delete(int id) { return 0; } }
现在我想将IOC与我的ODATA控制器一起使用.所以为了做到这一点,我在Global.asax文件中使用以下代码行(Application_Start事件):
var builder = new ContainerBuilder(); builder.RegisterApiControllers(Assembly.GetExecutingAssembly()); builder.RegisterInstance(new CategoryFacade()).As<ICategoryFacade>(); builder.RegisterInstance(new TVideoFacade()).As<ITVideoFacade>(); builder.RegisterType<CategoryController>(); //builder.RegisterType<CategoryFacade>().As<ICategoryFacade>(); //builder.RegisterType<TVideoFacade>().As<ITVideoFacade>(); //builder.Register(c => new CategoryFacade()).As<ICategoryFacade>().InstancePerRequest(); //builder.Register(c => new TVideoFacade()).As<ITVideoFacade>().InstancePerRequest(); var container = builder.Build(); var resolver = new AutofacWebApiDependencyResolver(container); GlobalConfiguration.Configuration.DependencyResolver = resolver;
但它显示以下异常:
Additional information: The type 'ABC.ABCDatabase.Facades.TVideoFacade' is not assignable to service 'ABC.ABCDatabase.Abstractions.ICategoryFacade'.
你能告诉我在OData控制器上使用autofac的正确方法吗?我是autofac的新手.
我认为你还没有在TVideoFacade类中实现ITVideoFacade接口. 只需确保您已在TVideoFacade类中实现了ITVideoFacade接口.
