【问题标题】:Unity Dependency injection on the same project for web api and mvc针对 web api 和 mvc 在同一个项目上的 Unity 依赖注入
【发布时间】:2018-02-18 16:36:22
【问题描述】:

起初我使用 asp.net mvc 和 unity.mvc 为 DI 启动项目,然后想将 web api 添加到同一个项目并安装 unity.webapi 但现在统一依赖注入无法将服务实例注入到ApiControllers 但是控制器类正在工作。

UnityConfig.cs

public static class UnityConfig
    {
        private static Lazy<IUnityContainer> container =
          new Lazy<IUnityContainer>(() =>
          {
              var container = new UnityContainer();
              RegisterTypes(container);
              return container;
          });

        public static IUnityContainer Container => container.Value;

        public static void RegisterTypes(IUnityContainer container)
        {
            container.RegisterType<IEmployeeService, EmployeeService>();
            container.RegisterType<IRepository<Employee>, Repository<Employee>>();
        }
    }

ApiController.cs

public class EmployeeApiController : ApiController
    {
        private readonly IEmployeeService _employeeService;

        public EmployeeApiController(IEmployeeService employeeService)
        {
            _employeeService = employeeService;
        }

        public EmployeeApiController(){}



        // GET: api/EmployeeApi
        public IEnumerable<Employee> Get()
        {
            var a = _employeeService.GetAll();
            return a;
        }
}

Global.asax.cs

public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            GlobalConfiguration.Configure(WebApiConfig.Register);

            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
        }
    }

在 apicontroller 的 get 操作上,IService 通过一个空指针异常。

【问题讨论】:

  • 找到解决方案:在 UnityConfig.cs 中添加以下行: GlobalConfiguration.Configuration.DependencyResolver = new Unity.WebApi.UnityDependencyResolver(container);

标签: c# asp.net dependency-injection unity-container


【解决方案1】:

Web API 定义了以下接口来解决依赖关系:

public interface IDependencyResolver : IDependencyScope, IDisposable
{
    IDependencyScope BeginScope();
}

public interface IDependencyScope : IDisposable
{
    object GetService(Type serviceType);
    IEnumerable<object> GetServices(Type serviceType);
}

正如您在评论中指出的那样,Unity.WebApi nuget 包提供了此实现,您只需在应用程序启动中注册即可。

完整参考:

public static class UnityConfig
{
    public static void RegisterComponents()
    {
        var container = new UnityContainer();
        GlobalConfiguration.Configuration.DependencyResolver = new UnityDependencyResolver(container);
    }
}

public class WebApiApplication : System.Web.HttpApplication
{
  protected void Application_Start()
  {
    UnityConfig.RegisterComponents();
  }           
}  

【讨论】:

  • 感谢您提供详细解释的答案。
【解决方案2】:

找到解决方案: 在 UnityConfig.cs 中添加以下行:

public static void RegisterTypes(IUnityContainer container)
{
    //Add this line
     GlobalConfiguration.Configuration.DependencyResolver = new Unity.WebApi.UnityDependencyResolver(container);

       container.RegisterType<IEmployeeService, EmployeeService>();
       container.RegisterType<IRepository<Employee>, Repository<Employee>>();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-25
    相关资源
    最近更新 更多