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."
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.
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.
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.
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'
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With