简体   繁体   中英

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?

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.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM