SQL Server knowledge center

everything about SQL Server
See also: Other Geeks@INDC

Connect to WCF Service Without Proxy

Connect to WCF Service Without Proxy

By : Kasim Wirama, MCDBA, MVP SQL Server

 

In several WCF articles, I always attach generated proxy to WCF client, so that WCF client could do remote call to WCF service. But you can consume remote WCF service from WCF client without proxy at client side.

This is possible by attaching service contract interface and member contract to WCF client. And create instance of channel through generic class ChannelFactory with service contract type with CreateChannel method.

What is channel actually? Channel is an entity used by proxy to connect to remote WCF service, it comprises of communication binding configuration (TCP/HTTP/MSMQ, etc that you will find when you configure by WCF configuration editor, on Bindings folder, create new binding, then you can see list of available binding types), endpoint address,  and communication binding type itself).

You can configure security in communication binding configuration, and make sure channel information at client should be same with channel information at WCF service, otherwise client couldn’t communicate with server.

For example here, I configure channel communication with TCP between WCF client and WCF server.

WCF server

Make sure you have TCP binding, with configured security tab at web.config. for example here Mode is Message, Algorithm suite is Basic 128 and MessageClientCredentialType is windows.

Make sure the binding is bound to TCP service endpoints at item in Endpoints folder.

WCF client

Attach contract interface and data member (if applicable) from WCF service to WCF client. You don’t need app.config, and declare namespace System.ServiceModel.Security at class where Main method resides.

Below is complete code for Main method.

NetTcpBinding netTCPBinding = new NetTcpBinding(SecurityMode.Message);

 NetTcpSecurity netTCPSecurity = netTCPBinding.Security;

netTCPSecurity.Message.AlgorithmSuite = SecurityAlgorithmSuite.Basic128;

netTCPSecurity.Message.ClientCredentialType = MessageCredentialType.Windows;

 

EndpointAddress address = new EndpointAddress("net.tcp://localhost:9002/Service/Service.svc");

WCFLib.IOperation proxy = ChannelFactory<WCFLib.IOperation>.CreateChannel(netTCPBinding, address);

 

int result = proxy.Add(5, 6);

Console.WriteLine("result : " + result);

 

proxy = null;   //close proxy

Console.ReadLine();

 

From this code above, you need to write more code to define communication type binding configuration, endpoint, communication type binding and contract, in this case WCFLib.IOperation. basically these information is also supplied by generated proxy. To create proxy, you can call CreateChannel method.

Run WCF client, this time at client’s console application will display 11 as addition between 5 and 6.

You don’t have to remember all steps to create proxy if you play around editing app.config with Service Configuraton Editor

Share this post: | | | |

Comments

De_Joker said:

Interesting articles about WCF :)

I want to learn more deep about WCF, can you point me a good book to start ?

Thanks

# December 14, 2007 7:49 AM

Kasim.Wirama said:

Many books relevant to WCF are good start to begin exploration, here my source come from Microsoft web site. You can search books from Wrox, usually it is developer-centric books.

# December 16, 2007 4:55 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 
Are you human?:  


Enter the numbers above: