加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

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接口.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读