gpt4 book ai didi

c# - Api Controller 有一个无参数的公共(public)构造函数错误

转载 作者:太空狗 更新时间:2023-10-30 01:01:24 26 4
gpt4 key购买 nike

我正在使用 Unity 进行 DI,但遇到了以下错误:-

“尝试创建‘UploadController’类型的 Controller 时发生错误。确保 Controller 具有无参数公共(public)构造函数。”

我有以下 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<object> GetServices(Type serviceType)
{
try
{
return container.ResolveAll(serviceType);
}
catch (ResolutionFailedException)
{
return new List<object>();
}
}

public IDependencyScope BeginScope()
{
var child = container.CreateChildContainer();
return new UnityResolver(child);
}

public void Dispose()
{
container.Dispose();
}
}

我创建了一个 UnityConfig 如下:-

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

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<IFileHelpers, FileHelpers>();
container.RegisterType<IDatabaseHelper, DatabaseHelper>();
container.RegisterType<IPlayersHelpers, PlayersHelpers>();
container.RegisterType<IBaseRepository, BaseRepository>();
container.RegisterType<IAttribsRepository, AttribsRepository>();
container.RegisterType<IPlayerRepository, PlayerRepository>();
container.RegisterType<IFileRepository, FileRepository>();
container.RegisterType<IPlayerAttribsRepository, PlayerAttribsRepository>();

container.RegisterType<FMContext>();
container.RegisterType<UserManager<ApplicationUser>>(new HierarchicalLifetimeManager());
container.RegisterType<IUserStore<ApplicationUser>, UserStore<ApplicationUser>>(new HierarchicalLifetimeManager());

return container;
}
}

我正在初始化 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 }
);
}
}

这是我的 UploadController 类:-

    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<HttpResponseMessage> Post()
{........................}

你能帮我确定我做错了什么吗?我是否需要声明其他内容才能正常工作?

感谢您的帮助和时间

最佳答案

在您的 Register 方法中,您有以下代码:

// ...
// Web API configuration and services
var container = new UnityContainer();
UnityConfig.RegisterComponents();
config.DependencyResolver = new UnityResolver(container);

但是,该容器中没有注册任何内容。在您的静态 UnityConfig 类中创建了一个单独的容器(带有注册),但该容器未传递给 UnityResolver 对象...

关于c# - Api Controller 有一个无参数的公共(public)构造函数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39300528/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com