How to consume WCF Service with net tcp binding and without mex binding
Asked Answered
V

1

6

I installed a windows application, it uses a WCF service, I just go through the config file for WCF service with net tcp binding hosted in Windows service with the following configuration, I am wondering how the clients are able to consume this WCF service. Application consumes this service to populate data in the UI, and it works. When I try to consume this I am not able to do so through WCF test client. Then how the application consumes this service?

       <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>
Vino answered 3/10, 2013 at 15:6 Comment(2)
If the calling application knows all the parameters (address, binding, contract), then it can easily call that service....Ultranationalism
Since you're using net.tcp we can assume it's .Net to .Net communication (i.e. non interoperable). WCF allows you to "use the shared contracts" to generate your proxies. I.o.w. you can set a reference to the interface library which was used by the server to iomplement the service. Do not set a reference to the library with concrete types, but only to the interfaces.Glabrate
U
13

You need to know three things to call a WCF service:

  • Address - where to call to - in your case net.tcp://localhost:25488/MyDataAccessService/MyFirstBindingAddress
  • Binding - what protocol and parameters to use (in your case: netTcpBinding)
  • Contract - the service contract (the public interface IMyDataAccessService) to define the service methods and parameters needed

Once you have these things, you can easily set up a client in code:

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();

and now your _clientProxy can easily call up the methods on the server, passing in the parameters etc.

Of course, for this to work, you must have the contract! E.g. you must have access to the file that define the service contract (and possibly the data contracts, too - the data types being sent back and forth). Since you're using the netTcpBinding, I'm assuming both ends of the wire are built using .NET here.

Those items can easily be included into a separate class library project that both the service developers as well as the client-side developers can share and use.

Ultranationalism answered 3/10, 2013 at 15:15 Comment(2)
What if I need to call a remote service...where the address is something like this... {net.tcp://RemoteServer:25488/MyDataAccessService/MyFirstBindingAddress} Is there any security settings needed ? I am getting an error as "The communication object, System.ServiceModel.Channels.ServiceChannel, cannot be used for communication because it is in the Faulted state. System.Exception {System.ServiceModel.CommunicationObjectFaultedExceptionKylie
@Ultranationalism Is it possible to exchange the contract info automatically? Just like clicking "Add Service Reference" and let Visual Studio automatically generate the proxies?Uncertain

© 2022 - 2024 — McMap. All rights reserved.