Connect WCF using anonymous methods

In our project, we make WCF calls using the following code.

// In generated Proxy we have..
public static ICustomer Customer
{
 get
  {
    ChannelFactory<ICustomer> factory = new ChannelFactory<ICustomer>("Customer");
    factory.Endpoint.Behaviors.Add((System.ServiceModel.Description.IEndpointBehavior)new ClientMessageInjector());
    ICustomer channel = factory.CreateChannel();
    return channel;
  }
}

and we have a Service Proxy class that has methods such as

public static Datatable GetCustomerDetails(int id)
{
  return Services.Customer.GetCustomerDetails(id);
} 

public static void .SaveCustomerDetails (int id)
{
  Services.Customer.SaveCustomerDetails(id) ;
}

etc. that we use to make business calls.

We recently found out that we need to “close” the wcf connection, and we are trying to figure it out without asking our developers to change their code too much.

Please provide us with some suggestions that will help us achieve this goal.

+3
source share
6 answers

The accepted “best practice” for this case will be as follows:

// create your client
ICustomer channel = CreateCustomerClient();

try
{
   // use it
   channel.GetCustomerDetails() ....

   (more calls)

   // close it
   channel.Close();
}
catch(CommunicationException commEx)
{
   // a CommunicationException probably indicates something went wrong 
   // when closing the channel --> abort it
   channel.Abort();
}

, "IDisposable",

using(ICustomer channel = CreateCustomerChannel()) 
{
   // use it
}

block - , , , .Close() ( ).

, , ( ( ()) , .

+6

ChannelFactory<T> . . CreateChannel ChannelFactory<T>, factory, , . , CreateChannel(string endpointConfigurationName) , ChannelFactory<T>. ( .)

, "" , (SessionMode = SessionMode.Required), "IsInitiating = true, IsTerminating = true" [OperationContract]. , - ICustomer. , .

, , , () . WCF - , .

+1

- . , .

0

.

0

, -, , . , - "" ( ).

0

factory , . !

0

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


All Articles