ChannelFactory在端点上没有地址,为什么?

Max*_*ler 3 .net configuration wcf c#-3.0

当我创建ChannelFactory的新实例时:

var factory = new ChannelFactory<IMyService>();
Run Code Online (Sandbox Code Playgroud)

并且我创建了一个新的通道,我有一个例外,说Endpoint的地址为null.

我在web.config中的配置如上所述,一切都是应该的(特别是端点的地址).

如果我创建一个新的MyServiceClientBase,它会从我的通道工厂加载所有配置:

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)
Run Code Online (Sandbox Code Playgroud)

为什么?

小智 9

如果在Web.Config中为端点提供如下名称,它是否有效:

<endpoint address="http://localhost:2000/MyService/" binding="wsHttpBinding"
    behaviorConfiguration="wsHttpBehaviour" contract="IService"
    name="MyWsHttpEndpoint" />
Run Code Online (Sandbox Code Playgroud)

并使用ChannelFactory创建频道,如下所示:

var factory = new ChannelFactory<IMyService>("MyWsHttpEndpoint");
Run Code Online (Sandbox Code Playgroud)


Max*_*ler 5

我终于找到了答案.ChannelFactory不会从您的Web.Config/app.config中读取信息.ClientBase(然后使用ChannelFactory)可以.

所有这些都花了我几个小时的工作,但我终于找到了一个合理的理由.

:)