参数构造错误 微信,api控制器有一个无参数的公共构造函数错误

I am using Unity to do my DI and I have encountered the following error:-

"An error occurred when trying to create a controller of type 'UploadController'. Make sure that the controller has a parameterless public constructor."

I have the following UnityResolver:-

public class UnityResolver : IDependencyResolver, IDependencyScope, System.Web.Http.Dependencies.IDependencyResolver

{

protected IUnityContainer container;

public UnityResolver(IUnityContainer container)

{

if (container == null)

{

throw new ArgumentNullException("container");

}

this.container = container;

}

public object GetService(Type serviceType)

{

try

{

return container.Resolve(serviceType);

}

catch (ResolutionFailedException)

{

return null;

}

}

public IEnumerable GetServices(Type serviceType)

{

try

{

return container.ResolveAll(serviceType);

}

catch (ResolutionFailedException)

{

return new List();

}

}

public IDependencyScope BeginScope()

{

var child = container.CreateChildContainer();

return new UnityResolver(child);

}

public void Dispose()

{

container.Dispose();

}

}

}

I have created a UnityConfig as follows:-

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();

BuildUnityContainer();

GlobalConfiguration.Configuration.DependencyResolver = new UnityDependencyResolver(container);

}

private static IUnityContainer BuildUnityContainer()

{

var container = new UnityContainer();

// register all your components with the container here

// you don't need to register controllers

container.RegisterType();

container.RegisterType();

container.RegisterType();

container.RegisterType();

container.RegisterType();

container.RegisterType();

container.RegisterType();

container.RegisterType();

container.RegisterType();

container.RegisterType>(new HierarchicalLifetimeManager());

container.RegisterType, UserStore>(new HierarchicalLifetimeManager());

return container;

}

}

}

Im initializing everyting in the WebApiConfig.cs

public static void Register(HttpConfiguration config)

{

// Web API configuration and services

// Configure Web API to use only bearer token authentication.

config.SuppressDefaultHostAuthentication();

config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));

// Web API configuration and services

var container = new UnityContainer();

UnityConfig.RegisterComponents();

config.DependencyResolver = new UnityResolver(container);

config.EnableCors();

// Web API routes

config.MapHttpAttributeRoutes();

config.Routes.MapHttpRoute(

name: "DefaultApi",

routeTemplate: "api/{controller}/{id}",

defaults: new { id = RouteParameter.Optional }

);

}

}

And this is my UploadController class:-

public class UploadController : ApiController

{

private readonly IFileHelpers _fileHelpers;

private readonly IDatabaseHelper _databaseHelper;

private readonly IPlayersHelpers _playersHelpers;

public UploadController(IFileHelpers fileHelpers, IDatabaseHelper databaseHelper, IPlayersHelpers playersHelpers)

{

_fileHelpers = fileHelpers;

_databaseHelper = databaseHelper;

_playersHelpers = playersHelpers;

}

public async Task Post()

{........................}

Can you please help me in determining what am I doing wrong. Do I need to declare something else for this to work?

Thanks for your help and time

解决方案

In your Register method, you have the following code:

// ...

// Web API configuration and services

var container = new UnityContainer();

UnityConfig.RegisterComponents();

config.DependencyResolver = new UnityResolver(container);

However, there is nothing registered to that container. There is a separate container (with registrations) being created in your static UnityConfig class, but that container is not being passed to the UnityResolver object...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值