Why doesn't IChannelFactory <T> define a CreateChannel parameter?

Why doesn't IChannelFactory <TChannel Interface> define the CreateChannel () parameter?

The ChannelFactory <class TChannel> , on the other hand, has the CreateChannel () parameter.

For testing / SoC capabilities, I want to go through the IChannelFactory interface, but it also forces me to bypass the EndpointAddress for use in CreateChannel(EndpointAddress) .

As a workaround, I created IChannelFactory2< IChannel> , which has a CreateChannel() parameter.

But in the end, I'm just wondering why it was designed this way (usually WCF has reasonable design options, but Im just lazy to work on this one!)

+7
source share
2 answers

The ChannelFactory <T> .CreateChannel () method is just a helper method, which was made possible thanks to the granularity of the ChannelFactory <T> class implementation .

If you look at the source code of ChannelFactory <T> , you will see the following:

 public TChannel CreateChannel() { return this.CreateChannel(this.CreateEndpointAddress(this.Endpoint), (Uri) null); } 

The CreateEndpointAddress method CreateEndpointAddress implemented inside the CreateEndpointAddress abstract class, which ChannelFactory <T> inherits from:

 internal EndpointAddress CreateEndpointAddress(ServiceEndpoint endpoint) { if (endpoint.Address == (EndpointAddress) null) throw DiagnosticUtility.ExceptionUtility.ThrowHelperError((Exception) new InvalidOperationException(System.ServiceModel.SR.GetString("SFxChannelFactoryEndpointAddressUri"))); else return endpoint.Address; } 

As you can see, endpoint information is already available through the ChannelFactory <T> .Endpoint property , which is initialized using various constructors of the ChannelFactory <T> class. These constructors , in essence, allow consumers to specify the details of the endpoint for the call .

Since interfaces cannot be determined by constructors, the most appropriate way to pass the required piece of information is through the arguments of the method that will use it, i.e. CreateChannel .

+4
source

I ran into the same problem.

My solution used factory pattern -

 public interface IFactory<out T> { T CreateInstance(); } public class WCFChannelFactory<TService> : IFactory<TService> { public ChannelFactory<TService> ChannelFactory { get; set; } public WCFChannelFactory(ChannelFactory<TService> channelFactory) { ChannelFactory = channelFactory; } public TService CreateInstance() { return ChannelFactory.CreateChannel(); } } 

Then in my application I just use IFactory .

+1
source

All Articles