IDependencyResolver null异常

时间:2016-06-13 07:40:17

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

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

public class MyController: ApiController
{
    private IMyService m_myService;

    public MyController()
    {

    }

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

我正在使用一个自定义的serviceProvider,它包含我的所有应用程序依赖项,并且提供给我并且必须使用它。以下是我正在使用的serviceProvider的IDependencyResolver的实现。

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时出现问题我得到一个Null Exception并且它崩溃了。

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方法,而通过参数传递的Type是IHostBufferPolicySelector。

任何帮助都会非常感激。谢谢!

1 个答案:

答案 0 :(得分:0)

我刚解决了。问题出在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;
}