Reputation: 901
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."
Upvotes: 6
Views: 7228
Reputation: 1038
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.
Upvotes: 9
Reputation: 2535
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.
Upvotes: 1
Reputation: 314
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'
Upvotes: 1