You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Cannot get service inside Transient registration of service.
Project on Git with sample: DecorTransientDelegateBug
Class with Decor attribute:
public class ServiceBLL : IServiceBLL
{
private readonly IServiceDependencyBLL serviceDependencyBLL;
public ServiceBLL(IServiceDependencyBLL serviceDependencyBLL)
{
this.serviceDependencyBLL = serviceDependencyBLL;
}
[Decorate(typeof(CacheDecorator))]
public string GetValueB()
{
return "dependency" + this.serviceDependencyBLL.GetValueA();
}
}
Service registration:
builder.Services.AddControllers();
builder.Services.AddDecor();
builder.Services.AddSingleton<CacheDecorator>();
builder.Services.AddSingleton<IServiceDependencyBLL, ServiceDependencyBLL >().Decorated();
builder.Services.AddSingleton<ServiceBLL>().Decorated();
builder.Services.AddTransient<Func<string, IServiceBLL>>(serviceProvider => account => {
switch (account)
{
case "countryA":
return serviceProvider.GetService<ServiceBLL>(); // error
default:
throw new Exception("Country does not exist.");
}
});
When application calls serviceProvider.GetService<ServiceBLL>(); it get error:
Castle.DynamicProxy.InvalidProxyConstructorArgumentsException: 'Can not instantiate proxy of class: DecorTransientDelegateBug.BLL.ServiceBLL.
Could not find a parameterless constructor.'
I need to have constructor with parameter because I need to use other service that have to be injected in ServiceBLL class.
The text was updated successfully, but these errors were encountered:
Decorating concrete types requires them to have a parameterless constructor. This is a limitation of the Castle Core library (discussed here castleproject/Core#532).
As a pretty simple workaround you can register ServiceBLL as an interface, which will not require a parameterless constructor:
Cannot get service inside Transient registration of service.
Project on Git with sample: DecorTransientDelegateBug
Class with Decor attribute:
Service registration:
When application calls
serviceProvider.GetService<ServiceBLL>();
it get error:I need to have constructor with parameter because I need to use other service that have to be injected in ServiceBLL class.
The text was updated successfully, but these errors were encountered: