gpt4 book ai didi

asp.net-mvc - 将 Unity 与 Web Api 2 一起使用给出错误没有默认构造函数

转载 作者:行者123 更新时间:2023-12-02 09:26:51 25 4
gpt4 key购买 nike

我有 ASP.NET MVC5 Web 应用程序,并且在同一个应用程序中也有 Web API。我正在为 DI 使用 Unity(版本 4)。我在 APP 启动时配置 Unity 容器,如下所示

        public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
UnityConfiguration.Config();
}
}

public class UnityConfiguration()
{
public void Config()
{
UnityContainer container = new UnityContainer();
container.RegisterType<IMyService, Myservice>();
container.RegisterType<IGenericRepository, GenericRepository>();
container.RegisterType<DbContext, MyEntities>();
}
}

public class GenericRepository:IGenericRepository
{
private DbContext _dbcontext;
public GenericRepository(DbContext dbcontext)
{
_dbcontext = dbcontext;
}
}
public class MyService:IMyService
{
private IGenericRepository _repo;
publi void MyService(IGenericRepository repository)
{
_repo = repository;
}
}


public class MyApiController:ApiController
{
provate IMyService _service;
MyApiController(IMyService myservice)
{
_service = myservice;
}

public IEnumerable<MyModel> GetData()
{
var result = _service.GetData();
return result.ConvertToMyModel();
}
}

但是当我像这样调用 url 时

localhost://lookup/getdata

我得到错误

Type 'LookupController' does not have a default constructor

我该如何解决这个问题?我是否需要注册我使用 Unity 创建的每个 Controller ,或者 Unity 会自动注册所有 MVC Controller ?

最佳答案

我倾向于使用 Unity.Mvc 包。

您不需要注册 Controller ,但您需要使用 WebAPI 注册 Unity。

   public class UnityConfiguration()
{
public IUnityContainer Config()
{
IUnityContainer container = new UnityContainer();
container.RegisterType<IMyService, Myservice>();
container.RegisterType<IGenericRepository, GenericRepository>();
container.RegisterType<DbContext, MyEntities>();

// return the container so it can be used for the dependencyresolver.
return container;
}
}

public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Your routes...

// Register Unity with Web API.
var container = UnityConfiguration.Config();
config.DependencyResolver = new UnityResolver(container);

// Maybe some formatters?
}
}

您还需要一个 DependencyResolver:

public class UnityResolver : 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();
}
}

你也可以看看这个类似的问题,除了欧文部分。 Unity.WebApi | Make sure that the controller has a parameterless public constructor

关于asp.net-mvc - 将 Unity 与 Web Api 2 一起使用给出错误没有默认构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37097200/

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