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

  • Protecting business operations means shifting the priorities around availability from disaster recovery to business continuity. Enterprises are shifting their focus from recovery from a disaster to preventing the disaster in the first place. With this change in mindset, disaster recovery is no longer the first line of defense; the organizations with a smarter business continuity practice are less impacted when disasters strike. This SmartSelect will provide insight to help guide your enterprise toward better …

  • Where the business performance of their mobile app portfolios are concerned, most companies are flying blind. While traditional application portfolios are held to all kinds of ROI measure, the investment plan for mobile apps -- increasingly the more crucial bet -- is made by guesswork and dart-throwing. This interactive e-book investigates how mobile is driving the need for app and portfolio measures unlike any we saw in the days of web. Good mobile analytics must deliver leading indicators of user experience …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds