4

I am consuming a WCF service in my .net core API project and I used the following steps:
The binding in WCF service (for testing) <add binding="basicHttpsBinding" scheme="https" />

  1. Created a proxy class using https://learn.microsoft.com/en-us/dotnet/core/additional-tools/dotnet-svcutil-guide?tabs=dotnetsvcutil2x
    (This created a proxy which has an interface IService1 and calls Service1Client).

  2. In the startup class I have bound the interface with the class services.AddSingleton<IService1, Service1Client>();

The method endpoint in the WCF service gets the data form the database according to the parameter passed to consume the WCF service.

I am not sure which one I should use, services.AddSingleton or services.AddTransient, because I am not sure what the proxy class is using to call the method.

If I create a single instance, will it be locked?

I have done a Jmeter test with 1000 rows in the database and 1000 rows from csv as a parameter to consume the API, but did not find any lock and all were successful under 3 minutes.

2
  • @DingPeng I am not sure about the WCF client working I came to know that it's thread safe so single instance is enough to handle all the request. I am not sure about DNS cashing and in case of not availability (for a second) of WCF service will it retry etc. Commented Oct 31, 2020 at 2:52
  • services.AddSingleton<IService1, Service1Client>(); is working for me since long. And also observed if there was some issue at service end when it is resolved. It started working without recycling pool. Commented Dec 10, 2021 at 3:54

2 Answers 2

1

You can view the servicereference.cs file to see which method in the wcf service is called by the proxy class.

enter image description here

Then we need to instantiate the proxy class and call the WCF service through the instantiated proxy class:

ServiceReference.Service1Client service1 = new Service1Client(); 

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your support. I know I can call method from service1 object. How do you inject this class into your core api is my question.
You can refer to this link, which contains how to use dependency injection in WCF: stackoverflow.com/questions/61723357/…
1

By this documentation in a web api a single httpclient should be instantiated: https://learn.microsoft.com/en-us/aspnet/web-api/overview/advanced/calling-a-web-api-from-a-net-client#create-and-initialize-httpclient

Of course if you have multiple target services, with different endpoints, an HttpClient per service is needed (you set the base address in the HttpClient)

In your case your generated client should use an HttpClient under the covers. If you can determine if the whole genrated client is thread safe then you can inject the client as a Singleton, if not you should add it as scoped

2 Comments

Can you provide some reference in support of this statement generated client should use an HttpClient under the covers
Not really, I am supposing it because of the https binding, you should inspect the code of the generated client, as well it's thread safety, wich actually is the main point. Regardles the technology underneath, your decision of injecting as scoped or as singleton depends on the thread safety of the client

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.