Null Reference
Null Reference

Reputation: 11340

WCF ChannelFactory.CreateChannel()

Why is it I'm able to do this:

var _channel = new ChannelFactory<T>("bindingname").CreateChannel();

But not this

var _channelFactory = new ChannelFactory<T>("bindingname");
var _channel = _channelFactory.CreateChannel();

The 2nd snippet complains that I need to pass in an EndPointAddress in CreateChannel() whereas the 1st doesn't.

Aren't the 2 snippets essentially the same?

Upvotes: 3

Views: 4232

Answers (2)

Harry Biscuit
Harry Biscuit

Reputation: 186

It might be because var _channelFactory = new ChannelFactory<T>("bindingname"); is resolving _channelFactory to IChannelFactory<T> instead of ChannelFactory<T>.

Try

ChannelFactory<T> _channelFactory = new ChannelFactory<T>("bindingname");
var _channel = _channelFactory.CreateChannel();

ChannelFactory<T> has a CreateChannel() method that takes no parameters. IChannelFactory<T> does not have a CreateChannel() method that takes no parameters.

Upvotes: 9

Nick Ryan
Nick Ryan

Reputation: 2670

Looking at the signature for the constructors for ChannelFactory, the only one that takes a single string as an input is asking for an endpointConfigurationName - not a binding name. Could this be the root of your problem? You may be constructing the ChannelFactory incorrectly? See below:-

    public ChannelFactory();
    public ChannelFactory(Binding binding);
    public ChannelFactory(ServiceEndpoint endpoint);
    public ChannelFactory(string endpointConfigurationName);
    protected ChannelFactory(Type channelType);
    public ChannelFactory(Binding binding, EndpointAddress remoteAddress);
    public ChannelFactory(Binding binding, string remoteAddress);
    public ChannelFactory(string endpointConfigurationName, EndpointAddress remoteAddress);

Upvotes: 0

Related Questions