Value cannot be null. Parameter name: type
var method = serviceActivatorType
.GetMethod("GetService", serviceActivatorParams)
returns public T GetService<T>();
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
.
There is no parameter to be passed for 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:
var result = method.Invoke(new ServiceActivator());
Chuckles
Updated on June 04, 2022Comments
-
Chuckles almost 2 years
After having read and tried the answers on stackExchange I am still getting a null value for result in the below code:
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.
Note: this is the error message I recieve for the above code:
Line 156: .MakeGenericMethod(new[] { registrationInfo.interfaceType });
Line 157:
Line 158: var result = method.Invoke(new ServiceActivator(), null);
Line 159:
Line 160: return result;
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. 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); } }