6

我安装了一个 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>
4

1 回答 1

13

调用 WCF 服务需要了解三件事:

  • 地址- 打电话到哪里 - 在你的情况下net.tcp://localhost:25488/MyDataAccessService/MyFirstBindingAddress
  • 绑定- 使用什么协议和参数(在你的情况下netTcpBinding:)
  • Contract - 服务合同 (the 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 构建的。

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

于 2013-10-03T15:15:34.470 回答