Does ChannelFactory.CreateChannel() actually open connection?
Asked Answered
P

3

6

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."

Prater answered 28/8, 2015 at 6:35 Comment(0)
B
9

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.

Buckles answered 28/8, 2015 at 10:18 Comment(0)
J
1

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'

Juanitajuanne answered 28/8, 2015 at 6:45 Comment(3)
I tried the example... but how can I call my service from this channel (it is IRequestChannel type).Prater
Here is code sample var binding = new CustomBinding("CustomNetTcpBinding"); var factory = binding.BuildChannelFactory<IRequestChannel>(binding, myEndpoint); factory.Open(); var channel = factory.CreateChannel(myEndpoint); var data = GenerateParameters(); channel.Open();Prater
I don't want to generate message dynamically.Prater
A
1

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.

Asp answered 28/8, 2015 at 10:14 Comment(2)
Thanks for your response. Just a question, If I call the service the way you mentioned.. Which service method will be called. I couldn't see any way to mention method name. Thanks in advance.Prater
I called public void SendMessage(string message) through yourService.SendMessage("It works!");Asp

© 2022 - 2024 — McMap. All rights reserved.