views:

1552

answers:

5

When I create a new instance of a ChannelFactory:

var factory = new ChannelFactory<IMyService>();

and that I create a new channel, I have an exception saying that the address of the Endpoint is null.

My configuration inside my web.config is as mentioned and everything is as it is supposed to be (especially the address of the endpoint).

If I create a new MyServiceClientBase, it loads all the configuration from my channel factory:

var factoryWithClientBase = new MyServiceClientBase().ChannelFactory;
Console.WriteLine(factoryWithClientBase.Endpoint.Address); //output the configuration inside the web.config


var factoryWithChannelFactory = new ChannelFactory<IMyService>();
Console.WriteLine(factoryWithChannelFactory.Endpoint.Address); //output nothing (null)

Why?

A: 

Is the channelfactory in the code as the service? Can it see the web.config?

Preet Sangha
+3  A: 

Will it work if you provide the endpoint with a name like this in Web.Config:

<endpoint address="http://localhost:2000/MyService/" binding="wsHttpBinding"
    behaviorConfiguration="wsHttpBehaviour" contract="IService"
    name="MyWsHttpEndpoint" />

And create the channel using ChannelFactory like this:

var factory = new ChannelFactory<IMyService>("MyWsHttpEndpoint");
AnAngel
I didn't try that. :) Worth trying.
Maxim
+3  A: 

I finally found the answer. ChannelFactory doesn't read information from your Web.Config/app.config. ClientBase (which then work with a ChannelFactory) does.

All this costed me a few hours of work but I finally found a valid reason.

:)

Maxim
A: 

I do think it can be placed in web/app.config file, but I really don't know how to do...

+1  A: 

ChannelFactory instantiated by the new operator DOES read from web.config. AnAngel answer above is correct. You just have to make sure that you provide your endpoint with a name. And make sure that the endpoint contract is set correctly (very important)

I have an experience before where my web.config was generated by VS and the endpoint contract is set to the contract in the ServiceReference (as a result of adding Service Reference) which is not the one I wanted. Although it has similar name to my original contract (but reside in different namespaces)

<endpoint address="http://localhost:2000/MyService/" binding="wsHttpBinding"
behaviorConfiguration="wsHttpBehaviour" contract="TheCorrectNamespace.IService"
name="MyWsHttpEndpoint" />

The asker didn't post his web.config so I am assuming that you most likely made the mistake I did (specifying the wrong contract for endpoints)

suparthawijaya