如何使用net tcp绑定和没有mex绑定来使用WCF服务

Dev*_*Dev 5 wcf nettcpbinding

我安装了一个Windows应用程序,它使用了WCF服务,我只是通过以下配置在Windows服务中托管网络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>
Run Code Online (Sandbox Code Playgroud)

mar*_*c_s 12

您需要知道三件事来调用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();
Run Code Online (Sandbox Code Playgroud)

现在您_clientProxy可以轻松调用服务器上的方法,传入参数等.

当然,为了这个工作,你必须有合同!例如,您必须有权访问定义服务合同的文件(也可能是数据合同 - 来回发送的数据类型).既然你正在使用它netTcpBinding,我假设线的两端都是用.NET构建的.

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