BSSchwarzkopf
BSSchwarzkopf

Reputation: 458

How to inject single instance of WCF service client in ASP.Net core using the code generated by dotnet-svcutil?

Context:

We migrated our application to ASP.Net Core 2.1 and got everything "working" but ran into performance issues under heavy load. After doing much googling, I found this article and then this one which advocated for using a single instance of HttpClient. Since we connect to several WCF services and we're injecting them using AddScoped I figured that maybe we need to do the same for our WCF clients.

This is how we're injecting the WCF service client in the ConfigureServices method:

services.AddScoped<IWCFService>(s => 
  new WCFServiceClient(
    binding, 
    new EndpointAddress(Configuration[$"ServiceEndPoints:WCFUrl"])
));

The other solutions, that I found, of injecting WCF client proxies also use AddScoped so I might be going down a rabbit hole with this.

We also copy/pasted the generated client proxies because VisualStudio was a bit buggy in the "ConnectedServices" area but I've since got that working. I don't think that that was my performance issue but I suppose it could be.

Question(s):

Is the dotnet-svcutil generated "client" the same as the HttpClient? I don't think it is but maybe it's susceptible to the same resource bottlenecks and should be treated the same? And if that is the case, how does one inject a single instance of the "client" into the generated proxy code? The available constructors don't take anything like that.

Example constructors:

    public WCFServiceClient() : 
            base(WCFServiceClient.GetDefaultBinding(), WCFServiceClient.GetDefaultEndpointAddress())
    {
        this.Endpoint.Name = EndpointConfiguration.CustomBinding_IWCFService.ToString();
        ConfigureEndpoint(this.Endpoint, this.ClientCredentials);
    }

    public WCFServiceClient(EndpointConfiguration endpointConfiguration) : 
            base(WCFServiceClient.GetBindingForEndpoint(endpointConfiguration), WCFServiceClient.GetEndpointAddress(endpointConfiguration))
    {
        this.Endpoint.Name = endpointConfiguration.ToString();
        ConfigureEndpoint(this.Endpoint, this.ClientCredentials);
    }

    public WCFServiceClient(EndpointConfiguration endpointConfiguration, string remoteAddress) : 
            base(WCFServiceClient.GetBindingForEndpoint(endpointConfiguration), new System.ServiceModel.EndpointAddress(remoteAddress))
    {
        this.Endpoint.Name = endpointConfiguration.ToString();
        ConfigureEndpoint(this.Endpoint, this.ClientCredentials);
    }

    public WCFServiceClient(EndpointConfiguration endpointConfiguration, System.ServiceModel.EndpointAddress remoteAddress) : 
            base(WCFServiceClient.GetBindingForEndpoint(endpointConfiguration), remoteAddress)
    {
        this.Endpoint.Name = endpointConfiguration.ToString();
        ConfigureEndpoint(this.Endpoint, this.ClientCredentials);
    }

    public WCFServiceClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) : 
            base(binding, remoteAddress)
    {
    }

Upvotes: 2

Views: 1844

Answers (0)

Related Questions