Proxy Code Generation in Visual Studio
Since working on our RIA application that uses JBoss WebServices on the backend, we’ve had tremendous amount of problems when it came down to handling proxies generated by Visual Studio. The fact is Visual Studio proxy generator (as well as the command prompt tools) are so lame! Although they do the job, but they should only suffice basic scenarios where a client connects to a bunch of webservice with no shared contract across them.
Other than the problem creating all data and service contracts in one single file brings you in terms of change management and large file editing / viewing, in enterprise services you may have to use multiple services that have shared contracts and that’s when you find the real trouble.
Visual Studio even in 2010 version, generates proxy codes in a way that each service should have its own namespace. This does not seem problematic given there is an option in the generator dialog that gives you the option to "Reuse types" in specified assemblies, alas those assemblies are just referenced projects and system assemblies and VS will not check if the generating code is already there in the same project.
At this point you have a few options to choose from.
Not a good solution, but given that you have control over the service’s implementation and since VS only works well with a single service, you can create a new aggregate service that delegates calls to other services on your backend, and generate the code only for the so called aggregate service. The problem with this method, other than violating SRP, is that the generated source code (“Reference.cs”) will get large and hard to maintain.
You can create a tool to process the generated code, and merge the services namespaces and remove the duplicate data contracts. While searching for a solution, I found out there is an existing tool for solving this same probelm. The was tool originally created by Michael Giagnocavo and further enhanced by Raymond Roelands. Download and read more about it here.
This may sound hard, but it turns out that the API is already there so creating .NET code from WSDL is not hard at all. In short, you can do this with about 60 lines of code! Hard to believe, eh?
public void Generate()
While having an instance of CodeCompileUnit, you can customize it even more further before actually creating code from it. For our problem here, you can merge all services in one single namespace, remove duplicate or already generated types and you are done, but that is for another post.