值不能为空。参数名称:类型

时间:2016-02-10 12:44:13

标签: c# entity-framework linq asp.net-web-api owin

在阅读并尝试了stackExchange上的答案之后,我仍然在下面的代码中获得结果的空值:

    typeOfIMydServiceBase.Assembly
            .GetExportedTypes()
            .Where(exportedType => typeOfMydServiceBase != exportedType && typeOfMydServiceBase.IsAssignableFrom(exportedType))
            .Select(mydServiceType => new
            {
                serviceType = mydServiceType,
                interfaceType = mydServiceType.GetInterfaces().First(@interface => @interface != typeOfIMydServiceBase && typeOfIMydServiceBase.IsAssignableFrom(@interface))
            })
            .ToList()
            .ForEach(registrationInfo => container.Register(registrationInfo.interfaceType, () =>
            {
                var method = serviceActivatorType
                    .GetMethod("GetService", serviceActivatorParams)
                    .MakeGenericMethod(new[] { registrationInfo.interfaceType });
                var result = method.Invoke(new ServiceActivator(), null);

                return result;
            }, WebApiRequestLifestyleWithDisposal));

结果保持为null,当我使用Object [] {null}或Object [] {}或Object [] {null,null等}时,我得到参数不匹配。

注意:这是我收到的上述代码的错误消息:

  

第156行:.MakeGenericMethod(new [] {registrationInfo.interfaceType});

     

第157行:

     

第158行:var result = method.Invoke(new ServiceActivator(),null);

     

第159行:

     

第160行:返回结果;

有谁知道这里发生了什么以及如何解决它????

{
public class ServiceActivator
{
    public ServiceActivator();
    public ServiceActivator(IServiceConfigurationReader configurationReader);

    public T GetService<T>();
    public T GetService<T>(string key);
}

}

与上述内容相关的还有所使用服务的注册。见下文:

//Scan and register business servics
        var typeOfIMydServiceBase = typeof(IMydServiceBase);
        var typeOfMydServiceBase = typeof(MydServiceBase);
        var serviceActivatorType = typeof(ServiceActivator);
        var serviceActivatorParams = new Type[0];

更新:在serviceActivator上使用typeof(字符串)Params工作但是我得到了一个未处理的异常。这是新错误:

Line 166:                    p.ServiceContainer = container;
Line 167:                });
Line 168:
Line 169:                container.RegisterWebApiControllers(config);
Line 170:            }

属于以下代码:

var typeOfIMydServiceBase = typeof(IMydServiceBase);
        var typeOfMydServiceBase = typeof(MydServiceBase);
        var serviceActivatorType = typeof(ServiceActivator);
        var serviceActivatorParams = new Type[] { typeof(string) };

        typeOfIMydServiceBase.Assembly
            .GetExportedTypes()
            .Where(exportedType => typeOfMydServiceBase != exportedType && typeOfMydServiceBase.IsAssignableFrom(exportedType))
            .Select(mydServiceType => new
            {
                serviceType = mydServiceType,
                interfaceType = mydServiceType.GetInterfaces().First(@interface => @interface != typeOfIMydServiceBase && typeOfIMydServiceBase.IsAssignableFrom(@interface))
            })
            .ToList()
            .ForEach(registrationInfo => container.Register(registrationInfo.interfaceType, () =>
            {

                var method = serviceActivatorType
                    .GetMethod("GetService", serviceActivatorParams)
                    .MakeGenericMethod(new[] { registrationInfo.interfaceType });

                var result = method.Invoke(new ServiceActivator(), null);

                return result;
            }, WebApiRequestLifestyleWithDisposal));
        {

            container.RegisterInitializer<IMydServiceBase>(p =>
            {
                p.ServiceContainer = container;
            });

            container.RegisterWebApiControllers(config);
        }
    }

1 个答案:

答案 0 :(得分:2)

var method = serviceActivatorType .GetMethod("GetService", serviceActivatorParams)

使用public T GetService<T>();返回 var serviceActivatorParams = new Type[0]; (来自问题中的评论讨论)

但是这一行:

var result = method.Invoke(new ServiceActivator(), null);

将调用此方法:

<强> public T GetService<T>(string key);

传递null作为string key的值。

没有为public T GetService<T>();传递参数,这就是它抛出异常的原因。

要解决您的问题,请尝试:

var serviceActivatorParams = new Type[] { typeof(string) };

或在没有null参数的情况下调用您的方法:

var result = method.Invoke(new ServiceActivator());