The type "***" is not assigned to the service "***" in Autofac

Now I am making a dynamic query in my project using System.Linq.Dynamic. I use Autofac as my default IOC container. But now I have a problem with registering common components, here is my code:

interface:

public interface IDynamicQuery { IQueryable<T> CreateDynamicQuery<T>(string propertyName, string propertyValue, Expression<Func<T, bool>> where) where T:class; } 

class:

 public class DynamicQuery :IDynamicQuery { public DynamicQuery(IUnitOfWork unitOfWork) { this.unitOfWork = unitOfWork; } private readonly IUnitOfWork unitOfWork; public IQueryable<T> CreateDynamicQuery<T>(string propertyName, string propertyValue, Expression<Func<T, bool>> where) where T:class { var appRepository = unitOfWork.Repository<T>(); IQueryable<T> queryResult = null; if (propertyName.Contains('$')) propertyName = propertyName.Replace('$', '.'); queryResult = appRepository.GetMany(where).Where("" + propertyName + ".Contains(\"" + propertyValue + "\")"); return queryResult; } } 

Then I will register them in the application launch entry:

 builder.RegisterType<IDynamicQuery>().As<DynamicQuery>().InstancePerHttpRequest(); 

But when I run my project based on MVC 4, it throws me an exception like:

  The type 'TinyFrame.Framework.Query.IDynamicQuery' is not assignable to service 'TinyFrame.Framework.Query.DynamicQuery'. 

An exception was var container = builder.Build(); : var container = builder.Build();

I know how to register a generic class in autofac, but I don’t know how to register above a class that I raised, can anyone help me with this? I am new to autofac, thanks in advice.

+7
c # autofac
source share
5 answers

@CharlieShi I had the same problem and I noticed that I forgot to add inheritance to my class. But in your case, try the following:

 builder.RegisterType<DynamicQuery>().As<IDynamicQuery>().InstancePerHttpRequest(); 

reverse interface with class!

+12
source share

In my case, I changed the order in which the interface and classes should be mentioned:

 builder.RegisterType<CONCRETE_CLASS>().As<INTERFACE>(); 
+2
source share

You can register the complete assembly using the autofac container as follows:

  builder.RegisterAssemblyTypes( typeof(YourClass).Assembly) .AsImplementedInterfaces() .InstancePerLifetimeScope(); 

In your case, if you want to register only the perticular type , just use the following:

 builder.RegisterType<DynamicQuery>() .As<IDynamicQuery>() .InstancePerLifetimeScope(); 

You can set the life according to your business requirements.

0
source share

Your problem is that you are trying to map an interface to an implementation, just replace IDynamicQuery and DynamicQuery. And do not forget to make a restructuring.

It should look like this:

 builder.RegisterType<DynamicQuery>().As<IDynamicQuery>().InstancePerHttpRequest(); 
0
source share

The same problem was detected, but in my case the reason was different. I have not seen myself registering twice. I find this error message to be misleading.

0
source share

All Articles