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>
2
  • If the calling application knows all the parameters (address, binding, contract), then it can easily call that service....
    – marc_s
    Oct 3, 2013 at 15:10
  • 1
    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. Oct 3, 2013 at 15:11

1 Answer 1

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.

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.CommunicationObjectFaultedException Jan 28, 2014 at 17:38
  • @marc_s Is it possible to exchange the contract info automatically? Just like clicking "Add Service Reference" and let Visual Studio automatically generate the proxies? Aug 23, 2014 at 13:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.