Design time WCF Client Proxy Generation

In my job I work on a large application consisting of several components, many of which communicate via Windows Communication Foundation (WCF). When developing an application where both the WCF service and client are in the same solution, the “Add Service Reference” command in Visual Studio is not ideal to use since it requires a running service. Implementing the client manually is tedious to say the least, especially keeping it updated when the service contract changes. There are various solution that dynamically emit code, and generates proxies at runtime, but this is not ideal either, I’d much prefer to have the code there so I can see it and make changes to it if necessary.

In addition, when defining the service you usually have either an async or sync version of each operation contract. In the client however, you may want both. This may mean that you need a different interface for the client proxy as well.

Then there is the problem of error handling when using client proxies deriving from ClientBase or DuplexClientBase. You should avoid using the using statement, because the Dispose method may actually throw exceptions, which would hide any original exception that was generated.

I have used T4 for parsing the FileCodeModel and generating proxies before, but frankly I do not like writing complex generators in T4. It is very hard to maintain, and the old EnvDTE API:s leave much to be desired when it comes to interpreting the code. Now with the advent of Roslyn… ehrm, I mean the “Microsoft .NET Compiler Platform”… or maybe I’ll stick with “Roslyn” after all. 😉 … Anyway, Roslyn makes parsing source code so nice, and so I started a project in part to learn using Roslyn, and in part because I really needed a better way to generate these proxies.

The WCF Client Proxy Generator is a Visual Studio Extension providing a Custom Tool that can be used for generating WCF Client proxy interfaces and classes based on service contract interfaces defined in code in the same solution. The first version is available now. Should be considered a preview for now, since it has not been extensively tested.

See GitHub for more information, and download the extension from Visual Studio Gallery

1 Comment

Comments are closed.