我创建了一个用作 WCF 客户端的控制台应用程序。app.config 包含以下内容:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<bindings>
<customBinding>
<binding name="WebHttpBinding_IWebContentService">
<textMessageEncoding maxReadPoolSize="64" maxWritePoolSize="16"
messageVersion="Soap12" writeEncoding="utf-8">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
</textMessageEncoding>
</binding>
</customBinding>
</bindings>
<client>
<endpoint address="http://localhost:1252/SomeService.svc" binding="customBinding" bindingConfiguration="WebHttpBinding_IWebContentService"
contract="WebContentClient.IWebContentService" name="WebHttpBinding_IWebContentService" />
</client>
</system.serviceModel>
</configuration>
当我运行它时,出现以下错误:
The address property on ChannelFactory.Endpoint was null
端点元素有一个地址属性,但我不确定我应该分配该属性。我正在本地主机上运行我的网络服务。
更新 1:
我添加了地址并更新了原始代码。但是我得到错误,说没有端点侦听地址="http://localhost:1252/SomeService.svc"。如果我访问 wcf 网址,我可以看到 Web 服务。
自定义绑定的配置不正确。您应该指定一个传输元素,如 httptransport。
下面是一个有效配置的示例:
<customBinding>
<binding name="myCustomHttpBinding">
<textMessageEncoding />
<httpTransport / >
</binding >
</customBinding >
为什么使用自定义绑定?根据服务的不同,有许多绑定(basicHttpBinding,webHttpBinding等)更易于使用。
注意:由于异常,我认为您的客户端代码也不正确。
您需要将其分配给 Web 服务的地址。 如果它是本地主机上的 WCF Web 服务,则其形式可能为:
http://localhost[:port]/<path>/SomeService.svc