In my API I have to wrap a WCF service, I added the reference to the project and I added this to IServiceCollection
services.AddScoped<IServiceClient,ServiceClient>(x =>
new ServiceClient($"{configuration.GetValue<string>(Constant.Url)}")
);
Class ServiceClient
public class ServiceClient : IServiceClient
{
private string urlService;
private WCFServiceReference.WCFServiceServiceClient _WCFServiceServiceClient; //WCF Service to wrapp
public ServiceClient(string urlService)
{
this.urlService = urlService;
BasicHttpBinding basicHttpBinding = new BasicHttpBinding();
this._WCFServiceServiceClient = new WCFServiceReference.WCFServiceServiceClient(basicHttpBinding, new EndpointAddress(this.urlService));
}
public Task<CalculateResponse> CalculateAsync(Request input)
{
return CalculateAsync(input, CancellationToken.None);
}
public Task<GetProductListResponse> GetProductListAsync(ProductRequest input)
{
return GetElencoProdottiAsync(input, CancellationToken.None);
}
public Task<GetVariableOfProductResponse> GetVariableOfProductAsync(GetVariableRequest input)
{
return GetVariableOfProductAsync(input, CancellationToken.None);
}
private Task<GetProductListResponse> GetProductListAsync(ProductRequest input, CancellationToken none)
{
return this._WCFServiceServiceClient.GetProductListAsync(input);
}
private Task<GetVariableOfProductResponse> GetVariableOfProductAsync(GetVariableRequest input, CancellationToken none)
{
return this._WCFServiceServiceClient.GetVariableOfProductAsync(input);
}
private Task<CalculateResponse> CalculateAsync(Request input, CancellationToken none)
{
return this._WCFServiceServiceClient.CalculateAsync(input);
}
}
When I start my application, this call CreateHostBuilder(args).Build().Run();
in Main rises the following Excpetion:
Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: Namespace.IProductService Lifetime: Scoped ImplementationType: Namespace.ProductService': Unable to resolve service for type 'Namespace.ServiceClient' while attempting to activate 'Namespace.ProductService'.)
CodePudding user response:
What does your ProductService Service Lifetime?
Singleton can't resolve Scope service
CodePudding user response:
Maybe you can try this, you can look at this post for specifics.
public class ServiceClient : IServiceClient
{
...
public ServiceClient(IServiceClient ServiceClient)
{
...
}
}
CodePudding user response:
I follow this post And so I modified my wrapper:
public class ServiceClient : ClientBase<WCFServiceReference.IWCFServiceServiceClient>, WCFServiceReference.IWCFServiceServiceClient, IServiceClient
{
private string urlService;
private WCFServiceReference.WCFServiceServiceClient _WCFServiceServiceClient; //WCF Service to wrapp
public ServiceClient(IOptions<ServiceClientOptions> options) : base()
{
if (options == null)
{
throw new ArgumentNullException(nameof(options));
}
this.Endpoint.Address = new EndpointAddress(options.Value.BaseUrl);
}
public Task<CalculateResponse> CalculateAsync(Request input)
{
return CalculateAsync(input, CancellationToken.None);
}
public Task<GetProductListResponse> GetProductListAsync(ProductRequest input)
{
return GetElencoProdottiAsync(input, CancellationToken.None);
}
public Task<GetVariableOfProductResponse> GetVariableOfProductAsync(GetVariableRequest input)
{
return GetVariableOfProductAsync(input, CancellationToken.None);
}
private Task<GetProductListResponse> GetProductListAsync(ProductRequest input, CancellationToken none)
{
return this.Channel.GetProductListAsync(input);
}
private Task<GetVariableOfProductResponse> GetVariableOfProductAsync(GetVariableRequest input, CancellationToken none)
{
return this.Channel.GetVariableOfProductAsync(input);
}
private Task<CalculateResponse> CalculateAsync(Request input, CancellationToken none)
{
return this.Channel.CalculateAsync(input);
}
}
And this configuration:
services.AddScoped<IServiceClient>(provider => {
var options = new ServiceClientOptions
{
BaseUrl = configuration.GetValue<string>(ApplicationConstant.UrlWCFExample)
};
var client = new ServiceClient((Microsoft.Extensions.Options.IOptions<QuickServiceClientOptions>)options);
return client;
});
But I get the same error