Object instances in static classes

I am developing a web application with several WCF service links. Currently, every time we need to call the service, we do the following (as an example):

Service.ServiceClient ServiceClient = new Service.ServiceClient(); ServiceClient.SomeMethod(); 

Would it be better to have a static class with static references to each Service and call this class instead, thereby avoiding creating a new instance of the ServiceClient object every time we want to call it?

For instance:

 public static class Services { private static Service.ServiceClient _ServiceClient = new Service.ServiceClient(); public Service.ServiceClient ServiceClient { get { return _ServiceClient; } } } 

And, if you do so, will there be a line

 private static Service.ServiceClient _ServiceClient = new Service.ServiceClient(); 

causes the creation of a new object every time we try to call this object, or will it be the same instance of this object every time we call it?

+4
source share
4 answers

You may have a class that will have all the functions exposed by your data contract. All of these methods will be static. Now inside this function you can do the following:

 public class ServiceManager{ public static CalculatedData SomeMethod() { var client = GetClient(); try { return client.SomeMethod(); } catch(Exception ex) { //Handle Error } finally { if(client.State == System.ServiceModel.CommunicationState.Opened) client.Close(); } } private static SomeClient GetClient() { return new ServiceClient(); } } 

Consumer will consume it as

 var calculatedData = ServiceManager.SomeMethod(); 
+2
source

if you want to do this create

Singleton Service

The singleton service is the ultimate service available. When a service is configured as a single-point service, all clients connect to the same known instance independently, regardless of the service endpoint to which they connect. The singleton service lives forever and only leaves as soon as the host disconnects. Singleton is created exactly once when the host is created.

You configure the single-user service by setting the InstanceContextMode property to InstanceContextMode.Single:

 [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)] class MySingleton : ... {...} 
+2
source

It will be created only once, but when you create it, you will not have any control. The usual way to handle this is to either create a separate static method (like init) where you instantiate or create it the first time you call it. To do this, you should check out the singleton design template .

+1
source

You can use the helper as shown below:

 private delegate void ServiceAction(Service.ServiceClient client); private static void PerformServiceAction(ServiceAction serviceAction) { using (var client = new Service.ServiceClient()) { serviceAction(client); } } 

which can then be called as follows:

 Helper.PerformServiceAction(client => client.SomeMethod()); 

It still creates a proxy for each call or sequence of calls, but at least your calling code is easier.

(Keep in mind that using “use” with a wcf proxy is not a good idea, because dispose can throw an exception, so it's better to catch the exceptions and close the proxy gracefully manually).

0
source

Source: https://habr.com/ru/post/1392009/


All Articles