【问题标题】:IDependencyResolver null exceptionIDependencyResolver 空异常
【发布时间】:2016-06-13 07:40:17
【问题描述】:

我正在尝试使用 IDependencyResolver 在我的控制器中注入一些依赖项。这是我的控制器。

public class MyController: ApiController
{
    private IMyService m_myService;

    public MyController()
    {

    }

    public MyController(IMyService myService)
    {
        m_myService = myService;
    }
   //Other code....
}

我正在使用一个自定义 serviceProvider,其中包含我所有的应用程序依赖项,它是提供给我的,并且是强制使用的。这是 IDependencyResolver 与我正在使用的 serviceProvider 的实现。

public class ServiceProviderResolver: IDependencyResolver
{
    private ServiceProvider m_serviceProvider;


    public ServiceProviderResolver(ServiceProvider serviceProvider)
    {
        m_serviceProvider = serviceProvider;
    }

    public void Dispose()
    {
        m_serviceProvider = null;
    }

    public IDependencyScope BeginScope()
    {
        return new ServiceProviderResolver(m_serviceProvider);
    }

    public object GetService(Type serviceType)
    {
        object result = null;
        try
        {
            MethodInfo serviceProviderGetMethod =
                m_serviceProvider.GetType().GetMethod("Get").MakeGenericMethod(new Type[] {serviceType});
            result = serviceProviderGetMethod.Invoke(m_serviceProvider, null);
        }
        catch (Exception)
        {

        }
        return result;
    }

    public IEnumerable<object> GetServices(Type serviceType)
    {
        //Code is the same because our container only have an object for each Type
        object result = null;
        try
        {
            MethodInfo serviceProviderGetMethod =
                m_serviceProvider.GetType().GetMethod("Get").MakeGenericMethod(new Type[] { serviceType });
            result = serviceProviderGetMethod.Invoke(m_serviceProvider, null);
        }
        catch (Exception)
        {

        }
        return (IEnumerable<object>) result;
    }
}

当我启动我的 webApi 时出现问题,我得到一个空异常并且它崩溃了。

Startup.cs

public class Startup
{
    //This property is being instantiated before calling configuration function
    public static ServiceProvider ServiceProvider { get; set; }

    public void Configuration(IAppBuilder application)
    {
        application.UseCors(CorsOptions.AllowAll);
        HttpConfiguration configuration = new HttpConfiguration();
        configuration.DependencyResolver = new ServiceProviderResolver(ServiceProvider);

        // Attribute routing.
        configuration.MapHttpAttributeRoutes();

        application.UseWebApi(configuration);

        configuration.EnsureInitialized();
    }
}

编辑: 当我在 Startup 类中调用 UseWebApi 方法时。该代码正在调用我的dependencyResolver 的GetService 方法,参数传递的类型是IHostBufferPolicySelector。

任何帮助将不胜感激。谢谢!

【问题讨论】:

  • 你在哪里分配ServiceProvider?还要发布您的异常详细信息(包括堆栈跟踪)。
  • 嗨@FedericoDipuma 我刚刚在 3 分钟前解决了它。感谢您的关注!

标签: c# asp.net-web-api dependency-injection


【解决方案1】:

我刚刚解决了。问题出在 GetServices(Type serviceType) 上。在某些情况下,我返回 null ,正确的方法是如果你有 null 返回一个新列表。下面的代码解决了它

public IEnumerable<object> GetServices(Type serviceType)
{
    //Code is the same because our container only have an object for each Type
    object result = null;
    try
    {
        MethodInfo serviceProviderGetMethod =
            m_serviceProvider.GetType().GetMethod("Get").MakeGenericMethod(new Type[] { serviceType });
        result = serviceProviderGetMethod.Invoke(m_serviceProvider, null);
    }
    catch (Exception)
    {

    }

    if(resul == null)
    {
       return new List<object>();
    }
    return (IEnumerable<object>) result;
}

【讨论】:

    猜你喜欢
    • 2023-04-11
    • 2021-10-25
    • 2014-04-26
    • 2014-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多