1

I'm trying to add a WebAPI to an existing ASP.Net MVC project and my question is regarding Unity:

Before with UnityMVC alone, the project had in the App_Start folder, the class UnityConfig with the following code:

container.RegisterType<IUnitOfWork, UnitOfWork>(new PerRequestLifetimeManager());
container.RegisterType<IDataContext, tfe_schemaContext>(new PerRequestLifetimeManager());

container.RegisterType<IPrintJobService, PrintJobService>(new PerRequestLifetimeManager());
container.RegisterType<IRepository<print_job>, Repository<print_job>>(new PerRequestLifetimeManager());

To use a WebAPI, I added UnityWebAPI via NuGet and this action added UnityMvcActivator class in the App_Start folder.

The following error displayed when I execute the project: "The type Unity.WebApi.UnityDependencyResolver does not appear to implement Microsoft.Practices.ServiceLocation.IServiceLocator. Parameter name: commonServiceLocator".

I found different examples of code using a Bootstrapper class but without using the UnityConfig class or UnityMvcActivator.

In the Bootrstrapper class, the registration of the types is like this:

container.RegisterType<IProductServices, ProductServices>().RegisterType<UnitOfWork>(new HierarchicalLifetimeManager());

I'm a little bit lost with all this, please help me understand and fix this error.

1

2 Answers 2

7

It seems you are using wrong or outdated NuGet packages. Try use Unity's official packages instead. Therefore remove any packages related to Unity and add following packages in Package Manager Console:

Install-Package Unity.Mvc
Install-Package Unity.AspNet.WebApi

Now you have one Unity container in your project which works well with MVC and WebAPI.

Sign up to request clarification or add additional context in comments.

5 Comments

You are right, I was using unity and not unity.mvc, I would not have found this alone, Thank you very much! After deleting and reinstalling what you recommended, the system displayed an error regarding ServiceLocation, I installed the Microsoft.Practices.ServiceLocation via NuGet and it's working fine now.
Hi Sam, from NuGet I see there are 3 packages: Unity.Mvc, Unity.Mvc4, Unity.Mvc5. For the same question as Sonouk (original poster), my question is why do we use Unity.Mvc instead of Unity.Mvc5 or Unity.Mvc4 package? Is Unity.Mvc package more generalized than the other 2 ones?
@Thomas.Benz Packages like Unity.MVC4 or Unity.MVC5 are unofficial packages. Therefore they are not maintained by Microsoft. Most of them are outdated or incompatible with each other. If you look at last public date of Unity.MVC5 for example you could easily see this package is not updated about 2 years. But official packages are being continuously updating.
Sam, thank you for your answer. I am a newbie to Unity DI. And I would like to learn more about it, especially how to implement the 2 IoC an Asp.net project which includes Unity.Mvc and Unity.AspNet.WebApi; in other words, that project supports MVC and Web Api functions. If possible, can you please show me some resources or codes to implement step by step the 2 Unity IoC (for MVC and asp.net WebApi) in such an asp.net project?
@Thomas.Benz you don't need to implement separate container for WebAPI and MVC. You could use one shared container. If you have further question please ask as a separate question with proper details. I will try to answer it if you inform me about that question.
0

You can also try by separating the projects.

One project four you MVC code and another project for your WebAPI code. That way you will install Unity.MVC in your MVC project and Unity.WebAPI in your WebAPI project.

After that, you can consume the services from your MVC Application without any problem.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.