ChannelFactory.Endpoint 上的地址 属性 为空。 ChannelFactory 的端点必须具有指定的有效地址

The Address property on ChannelFactory.Endpoint was null. The ChannelFactory's Endpoint must have a valid Address specified

我在 URL (http://192.168.2.131:8089/) 上通过 WCF 服务托管。现在,我正尝试从 Windows 表单调用 WCF 服务,但出现此错误:

The Address property on ChannelFactory.Endpoint was null. The ChannelFactory's Endpoint must have a valid Address specified.

这里是 win 窗体 C# 代码:

var binding = new WSDualHttpBinding();

var endpoint = new EndpointAddress("http://192.168.2.131:8089/");

InstanceContext context = new InstanceContext(new Form1());

var cFactory = new DuplexChannelFactory<IPubSubService>(context);

IPubSubService client = null;
client = cFactory.CreateChannel();
client.SendNotification();

client = cFactory.CreateChannel(); 正在抛出错误。

这是 WCF 服务和 Winforms system.serviceModel 配置 app.config:

<system.serviceModel>
   <services>
     <service name="PubSubServiceLib.PubSubService">
        <endpoint 
            address="" 
            binding="wsDualHttpBinding"
            contract="PubSubServiceLib.IPubSubService">
        </endpoint>
        <host>
           <baseAddresses>
              <add baseAddress="http://192.168.2.131:8089/"/>
           </baseAddresses>
        </host>
      </service>
   </services>
   <bindings>
      <wsDualHttpBinding>
         <binding receiveTimeout="00:30:00" sendTimeout="00:30:00" 
                  maxReceivedMessageSize="2147483647">
            <reliableSession inactivityTimeout="02:00:00"/>
         </binding>
      </wsDualHttpBinding>
   </bindings>
   <behaviors>
      <serviceBehaviors>
         <behavior>
            <serviceMetadata httpGetEnabled="True"/>
            <serviceDebug includeExceptionDetailInFaults="False"/>
         </behavior>
      </serviceBehaviors>
   </behaviors>
</system.serviceModel>

尝试使用带有绑定和端点的 DuplexChannelFactory(InstanceContext, Binding, EndpointAddress) 重载,这样工厂就会有所需的组件。

更新代码:

var binding = new WSDualHttpBinding();
var endpoint = new EndpointAddress("http://192.168.2.131:8089/");
InstanceContext context = new InstanceContext(new Form1());
var cFactory = new DuplexChannelFactory<IPubSubService>(context, binding, endpoint);

这会将绑定和终结点地址分配给 DuplexChannelFactory

还要确保您的 Form1 实施回调合同。