asp.net-mvc – 该类型不能分配给服务autofac
发布时间:2020-12-16 09:13:38 所属栏目:asp.Net 来源:网络整理
导读:我有这样的界面: public interface ICategoryFacade{ IEnumerableCategory Get(); Category Get(int id); int Post(Category model); int Put(Category model); int Patch(Category model); int Delete(int id);} 我上课了: public class CategoryFacade :
我有这样的界面:
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接口.
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 如何在MVC_WebAPI项目中的APIController帮助页面添加Web测试
- asp.net – IIS 8中的URL中缺少.aspx
- asp.net-mvc – 验证asp.net MVC中的只读输入不显眼的验证
- asp.net-mvc – 使用Entity Framework 6和ViewModel从数据库
- Asp.net web api 知多少
- ASP.NET MVC Controller FileContent ActionResult通过AJAX
- asp.net – 如何绑定gridview中的下拉列表?
- asp.net-mvc – ApiController中的ASP.NET Web API路由
- asp.net – 使用SQL Server进行关键字搜索
- asp.net-web-api – Xamarin.Android上的StringContent为空
推荐文章
站长推荐
热点阅读