Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does ChannelFactory.CreateChannel() actually open connection?

May be this is very obious but after doing google alot, couldn't reach to any conclusion.

I want to know "does ChannelFactory.CreateChannel() actually open connection or it just return something and actual connection will be open the time of method call. How long this connection will be alive if I don;t close it."

like image 219
Abhash786 Avatar asked Aug 28 '15 06:08

Abhash786


People also ask

What is the use of ChannelFactory to invoke WCF service?

Introduction. A Channel Factory enables you to create a communication channel to the service without a proxy. A Channel Factory that creates and manages the various types of channels which are used by a client to send a message to various configured service endpoints.

What is channel in WCF service?

Channels provide a low-level programming model for sending and receiving messages. This programming model relies on several interfaces and other types collectively known as the WCF channel model.


3 Answers

Good question. When I wonder about something like that, I am just reading the source code of .Net at there.

CreateChannel method calls the Open method internally. If the CommunicationState is not equal to Opened then Open method executing with DefaultOpenTimeout.

DefaultOpenTimeout is configured by endpoint binding configuration.

You can see the source code.

like image 94
Uğur Aldanmaz Avatar answered Oct 30 '22 19:10

Uğur Aldanmaz


Connection is opened only when you call open() in the ChannelFactory. It is demonstrated in the Examples section here 'https://msdn.microsoft.com/en-us/library/ms575250(v=vs.110).aspx'

like image 44
Yash Avatar answered Oct 30 '22 19:10

Yash


It think you only need to create this :

// Sample service
public class Service : IService
{
   public void SendMessage(string message)
   {
      // do the processing....
   }
}

// Creating client connection using factory
// I can't remember the used of my asterisk here but this is use to identity the configuration name used for the endpoint.
var result = new ChannelFactory<IService>("*", new EndpointAddress(serviceAddress));
IService yourService = result.CreateChannel();

// This will automatically open a connection for you.
yourService.SendMessage("It works!");

// Close connection
result.Close();

Just a bit of my client configuration with multiple endpoints:

<client>
      <!--note that there is no address on the endpoints as it will be overridden by the app anyway-->
      <endpoint binding="wsHttpBinding" bindingConfiguration="wsHttpBinding" behaviorConfiguration="standardBehavior" contract="IService" name="Service"/>
       .
       .
</client>

I used this approach to my client to connect with 30+ services hosted in IIS. By the way, I just grab this code to my existing WCF services and the actual implementation of it was, ChannetFactory it wrapper to another method where I could just simply pass my Service as Generic Type and Service Addressess.

I used message pattern Request Reply and .Net 4.5 here.

like image 43
jtabuloc Avatar answered Oct 30 '22 21:10

jtabuloc