[英]Value cannot be null. Parameter name: type
After having read and tried the answers on stackExchange I am still getting a null value for result in the below code: 在阅读并尝试了stackExchange上的答案之后,我仍然在下面的代码中获得结果的null值:
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));
result keeps coming back as null and when I use an Object[] {null} or Object[]{} or Object[]{null,null,etc} I get parameter mismatches. 结果保持为null,当我使用Object [] {null}或Object [] {}或Object [] {null,null等}时,我得到参数不匹配。
Note: this is the error message I recieve for the above code: 注意:这是我收到的上述代码的错误消息:
Line 156: .MakeGenericMethod(new[] { registrationInfo.interfaceType }); 第156行:.MakeGenericMethod(new [] {registrationInfo.interfaceType});
Line 157: 第157行:
Line 158: var result = method.Invoke(new ServiceActivator(), null); 第158行:var result = method.Invoke(new ServiceActivator(),null);
Line 159: 第159行:
Line 160: return result; 第160行:返回结果;
Does anyone know what's happening here and how I can fix it???? 有谁知道这里发生了什么以及如何解决它????
{
public class ServiceActivator
{
public ServiceActivator();
public ServiceActivator(IServiceConfigurationReader configurationReader);
public T GetService<T>();
public T GetService<T>(string key);
}
} }
Also pertinent to the above is the registration of the services used. 与上述内容相关的还有所使用服务的注册。 See below: 见下文:
//Scan and register business servics
var typeOfIMydServiceBase = typeof(IMydServiceBase);
var typeOfMydServiceBase = typeof(MydServiceBase);
var serviceActivatorType = typeof(ServiceActivator);
var serviceActivatorParams = new Type[0];
Update: using typeof(string) at serviceActivator Params works however I am getting an unhandled exception futher down. 更新:在serviceActivator上使用typeof(字符串)Params工作但是我得到一个未处理的异常。 Here is the new error: 这是新错误:
Line 166: p.ServiceContainer = container;
Line 167: });
Line 168:
Line 169: container.RegisterWebApiControllers(config);
Line 170: }
which pertains to the following code: 它属于以下代码:
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);
}
}
var method = serviceActivatorType .GetMethod("GetService", serviceActivatorParams)
returns public T GetService<T>();
返回public T GetService<T>();
with var serviceActivatorParams = new Type[0];
with var serviceActivatorParams = new Type[0];
(from comment discussion in the question) (来自问题中的评论讨论)
BUT this line: 但这一行:
var result = method.Invoke(new ServiceActivator(), null);
is going to invoke this method: 将要调用此方法:
public T GetService<T>(string key);
passing null
as the value for string key
. 传递null
作为string key
的值。
There is no parameter to be passed for public T GetService<T>();
public T GetService<T>();
没有传递参数public T GetService<T>();
, that's why it throws an exception. ,这就是它抛出异常的原因。
To fix your problem, try: 要解决您的问题,请尝试:
var serviceActivatorParams = new Type[] { typeof(string) };
or invoke your method without the null
parameter: 或者在没有null
参数的情况下调用您的方法:
var result = method.Invoke(new ServiceActivator());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.