【问题标题】:How to consume WCF Service with net tcp binding and without mex binding如何使用带有 net tcp 绑定和没有 mex 绑定的 WCF 服务
【发布时间】:2013-10-03 15:06:25
【问题描述】:

我安装了一个 Windows 应用程序,它使用 WCF 服务,我只是通过以下配置通过 Windows 服务中托管的 net tcp 绑定查看 WCF 服务的配置文件,我想知道客户端如何使用这个 WCF服务。应用程序使用此服务在 UI 中填充数据,并且它可以工作。当我尝试使用它时,我无法通过 WCF 测试客户端这样做。那么应用如何消费这个服务呢?

       <system.serviceModel>
    <bindings>
        <netTcpBinding>

            <binding name="NetTcpBinding_FirstBindingServiceContract" closeTimeout="00:10:00"
               openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:10:00"
               transactionFlow="false" transferMode="Buffered" transactionProtocol="OleTransactions"
               hostNameComparisonMode="StrongWildcard" listenBacklog="10"
               maxBufferPoolSize="999999999" maxBufferSize="999999999" maxConnections="10"
               maxReceivedMessageSize="999999999">
                <readerQuotas maxDepth="999999999"
                                          maxStringContentLength="999999999"
                                          maxArrayLength="999999999"
                                          maxBytesPerRead="999999999"
                                          maxNameTableCharCount="999999999" />
                <reliableSession ordered="true" inactivityTimeout="00:10:00" enabled="false" />
                <security mode="Transport">
                    <transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" />
                    <message clientCredentialType="Windows" />
                </security>
            </binding>
        </netTcpBinding>
    </bindings>
    <behaviors>
        <serviceBehaviors>
            <behavior name="MyServiceBehaviors">
                <serviceMetadata />
            </behavior>
        </serviceBehaviors>
    </behaviors>

    <services>
        <service name="MyService.DataAccessService"  behaviorConfiguration="MyServiceBehaviors">

            <endpoint bindingConfiguration="NetTcpBinding_FirstBindingServiceContract"
                      name="firstBinding" address="net.tcp://localhost:25488/MyDataAccessService/MyFirstBindingAddress"
                    binding="netTcpBinding"
                    contract="MyDataService.IMyDataAccessService">
            </endpoint>
        </service>
    </services>
</system.serviceModel>

【问题讨论】:

  • 如果调用应用程序知道所有参数(地址、绑定、合约),那么它可以轻松调用该服务......
  • 由于您使用的是 net.tcp,我们可以假设它是 .Net 到 .Net 的通信(即不可互操作)。 WCF 允许您“使用共享合同”来生成您的代理。 I.o.w.您可以设置对服务器用来实现服务的接口库的引用。不要设置对具有具体类型的库的引用,而只能设置对接口的引用。

标签: wcf nettcpbinding


【解决方案1】:

调用 WCF 服务需要知道三件事:

  • 地址 - 打电话到哪里 - 在你的情况下net.tcp://localhost:25488/MyDataAccessService/MyFirstBindingAddress
  • 绑定 - 使用什么协议和参数(在您的情况下:netTcpBinding
  • 合同 - 服务合同(public interface IMyDataAccessService),用于定义所需的服务方法和参数

一旦你有了这些东西,你就可以轻松地在代码中设置一个客户端:

NetTcpBinding binding = new NetTcpBinding(NetTcpBinding.None);
EndpointAddress address = new EndpointAddress("net.tcp://localhost:25488/MyDataAccessService/MyFirstBindingAddress");

ChannelFactory<IMyDataAccessService> channelFactory = new ChannelFactory<IMyDataAccessService>(binding, address);
IMyDataAccessService _clientProxy = channelFactory.CreateChannel();

现在您的_clientProxy 可以轻松调用服务器上的方法、传递参数等。

当然,要实现这一点,您必须拥有合同!例如。您必须有权访问定义服务合同(可能还有数据合同 - 来回发送的数据类型)的文件。由于您使用的是netTcpBinding,因此我假设电线的两端都是使用 .NET 构建的。

这些项目可以很容易地包含在一个单独的类库项目中,服务开发人员和客户端开发人员都可以共享和使用。

【讨论】:

  • 如果我需要调用远程服务怎么办...地址是这样的... {net.tcp://RemoteServer:25488/MyDataAccessService/MyFirstBindingAddress} 是否有任何安全设置需要吗?我收到错误消息,因为“通信对象 System.ServiceModel.Channels.ServiceChannel 无法用于通信,因为它处于故障状态。System.Exception {System.ServiceModel.CommunicationObjectFaultedException
  • @marc_s 是否可以自动交换合同信息?就像点击“添加服务引用”让Visual Studio自动生成代理一样?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-02-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多