I have a Web Api with controllers that have constructors with parameters, like this:
public class UserController : ApiController
{
private UserService userService;
public UserController(UserService userService)
{
this.userService = userService;
}
}
I'm using Dependency Injection. I followed this tutorial and this is how it looks now:
public static class UnityConfig
{
public static void RegisterComponents()
{
var container = new UnityContainer();
// register all your components with the container here
// it is NOT necessary to register your controllers
// e.g. container.RegisterType<ITestService, TestService>();
GlobalConfiguration.Configuration.DependencyResolver = new UnityDependencyResolver(container);
}
}
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
var container = new UnityContainer();
container.RegisterType<UserService>().RegisterType<IUnitOfWork, UnitOfWork>(new HierarchicalLifetimeManager());
container.RegisterType<ProviderService>().RegisterType<UserService>().RegisterType<IUnitOfWork, UnitOfWork>(new HierarchicalLifetimeManager());
container.RegisterType<TransactionService>().RegisterType<UserService>().RegisterType<IUnitOfWork, UnitOfWork>(new HierarchicalLifetimeManager());
config.DependencyResolver = new UnityResolver(container);
...
}
When I run it from Visual Studio and test it through Postman it works just fine. But when I publish the Api and try to run it from the published version I get this error:
ExceptionMessage: An error occurred when trying to create a controller of type 'UserController'. Make sure that the controller has a parameterless public constructor.
en System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)
en System.Web.Http.Controllers.HttpControllerDescriptor.CreateController(HttpRequestMessage request)
en System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()
System.ArgumentException: The type 'PaymentManagement.Web.Api.Controllers.UserController' doesn't have a predeterminate constructor
en System.Linq.Expressions.Expression.New(Type type)
en System.Web.Http.Internal.TypeActivator.Create[TBase](Type instanceType)
en System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage request, Type controllerType, Func`1& activator)
en System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)
This is how I do to publish it:
I used to get this same error when running it from Visual Studio before adding the unity container. But I don't know what I'm missing now.