TIP: Using Proxies and ChannelFactory in WCF

Proxies vs ChannelFactory

If you've used WCF services before, you know that you can add a reference to the service either by using svcutil.exe or letting Visual Studio do it for you (which in turn uses svcutil.exe).

Although using the proxy classes is the most common way, it's not always the best way.

Remember that not all WCF services are created the same. In some cases, you may have a service that is tightly bound to the client application—in other words, you may have a service that's only going to be used by a few applications, ever.

In such a case, it makes sense to reference the Interface DLL directly and use ChannelFactory to call your methods using that. One significant advantage of the ChannelFactory route is that it gives you access to methods that wouldn't otherwise be available if you used svcutil.exe.

However, if you have a service that you know is going to be used by several applications or is generic enough to be used in several places, you'll want to continue using the generated proxy classes.

It's worth keeping in mind that, when you generate a proxy class using svcutil.exe, it's doing almost the same thing as ChannelFactory behind the scenes, with the difference that the Interface is inferred from the metadata from your WCF service.

Using ChannelFactory

Start by referencing the Interface assembly. By default, your WCF service will have the interfaces in the same assembly as the service code. You should move the interface code out to another assembly so that it's reusable by the service and your client. Even if you're not going to use ChannelFactory, it's good practice to keep your Interface in a separate assembly.

Next, create the ChannelFactory like this:

ChannelFactory<IService1> channel =
   new ChannelFactory<IService1>("bindingName");
IService1 client = channel.CreateChannel();

And finally, call your method.

client.DoWork();


About the Author

SM Altaf

Mendhak is a web developer and a Microsoft MVP who works with ASP.NET and PHP among the usual array[] of web technologies. He is also rumored to be a three eyed frog, but the evidence is lacking. He can be contacted via his website, www.mendhak.com.


Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: November 20, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Are you wanting to target two or more platforms such as iOS, Android, and/or Windows? You are not alone. 90% of enterprises today are targeting two or more platforms. Attend this eSeminar to discover how mobile app developers can rely on one IDE to create applications across platforms and approaches (web, native, and/or hybrid), saving time, money, and effort and introducing apps to market faster. You'll learn the trade-offs for gaining long …

  • Live Event Date: November 18, 2014 @ 11:00 a.m. EST As you embrace the hybrid world of on-premise and cloud applications, often accessed via mobile devices, you now have to be concerned that cybercriminals have yet another vehicle to attack your business. In fact, the average cost of cybercrime has increased over 10% in the last year, and this applies to businesses of all sizes. Attend this webinar to hear David Monahan, Security Research Director at EMA, and Dana Epp, recognized security luminary from …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds