Wednesday, 5 December 2007

Pre-build events, client proxies, and common libraries

Previously I posted about WCF client-proxies and how pre-build events can be used to generate these every time your client project builds.

The post sets out that the client and service share a common library of domain/business objects, and svcutil is instructed as such using the /reference flag. This approach works fine, however, you may run into problems when the shared library changes. This happened to me repeatedly during a period where I had to modify the common library which contains the classes (marked up with the [DataContract] attribute).

The problem is that the pre-build event on the client project references a running version of the WCF service. When the service interface changes, or in particular when the DataContracts that the service consume or return change, there's a discrepancy between the library referenced by the library and the library referenced by the client.

Even if you ensure that you update the client's common library code prior to the build, the pre-build event may fail. Why? Because the call to svcutil that creates the client proxy runs before the changes to the common library (in the client) are compiled.

That particular aspect of this approach isn't great. However I found that if I make the changes to the common library through the client solution and then pull these across to the service things work just fine. With SVN this is easy. The common library is a SVN external to both the client and service SVN repositories, and I can easily commit changes made through the client solution and update the service solution (I typically work on the client in one instance of Visual Studio and the service in another).

Having said all this and gone through a great deal of trouble to get everything working with pre-build events in Visual Studio 2005, it appears that the solution to this particular problem really lies with Visual Studio 2008.

Happy coding!

No comments: