在阅读和尝试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));
结果一直返回为空,并且当我使用 Object[] {null} 或 Object[]{} 或 Object[]{null,null,等等} 时,我得到的是参数不匹配的错误。
注意:这是上面代码的错误信息:
第156行: .MakeGenericMethod(new[] { registrationInfo.interfaceType });
第157行:
第158行: var result = method.Invoke(new ServiceActivator(), null);
第159行:
第160行: return result;
有人知道这里发生了什么以及如何修复吗????
{
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 Params中使用typeof(string)可以工作,但是我在稍后遇到了一个未处理的异常。这是新的错误:
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);
}
}
GetService
方法签名是什么?它的参数允许null
吗(例如值类型等)? - Khanh TOpublic T GetService();
public T GetService(string key);
} - Chuckles.GetMethod("GetService", serviceActivatorParams)
替换为.GetMethod("GetService", new Type[] { typeof(string) })
。 - Khanh TOnew Type[] { typeof(string) }
。如果您使用serviceActivatorParams
,则应为var serviceActivatorParams = new Type[] { typeof(string) }
。 - Khanh TO