Custom tool error: Failed to generate code for the service reference …

Within a Windows Phone 7 Silverlight client, when updating a service reference I frequently receive the following error message: “Custom tool error: Failed to generate code for the service reference ”. Please check other error and warning messages for details.” When I get this error, the code generator fails to generate the Reference.cs file. This has the knock on effect of generating a number of errors as my service reference is no longer recognised by VS2010.

Completely removing the service references, deleting the bin, obj folders and service reference folders, and removing the service reference pointers in the project file and readding the service references has no effect at all. The custom tool still fails to generate the Reference.cs file. That means that the problem has to lie with the WCF Service.

I will now investigate the WCF Service, and see if this yields any clue as to what is going on.

It is indeed the service, there is something wrong with my deployment. My initial thinking is that there are some missing dependencies that my service relies on. And I am right. There were some missing DLLs, and data files that the service depends upon.

Another problem has arisen, in that the Exception policy for the Enterprise Framework as set in the Web.config file is being set more than once. This generates an error, and yet it is only located in one place! The simple fix is to simply comment the policy field out in the config file.

Testing the deployed service now works in the WCF Test Client, after modifying the client config to increase the timeouts, maximum buffer size, and maximum message received size.

Now that have I my deployed service working, surely I can create my service reference and have the custom tool generate the source code for the Reference.cs file right? No. Hmmm, this is rather frustrating. But never mind, there is a solution. I just have to rack my brains a little harder.

I have tried to delete the service reference and recreate it with a different filename as some suggest on WCF forums, but this hasn’t worked for me. Time for a cup of tea and some thinking time.


I have found the cause of the problem. Team Foundation Server was intefering with the regeneration of the service reference. To solve this issue, I deleted the bin folder, the obj folder, the service reference client config file, the service references, and the service reference folder. I then saved the solution and checked in the changes to Team Foundation Server, and confirmed the pending deletions. Then I recreated the services, and lo and behold the Reference.cs file was created with the generated source code.

In summary, when your client fails to create a service reference the first thing to check is that the service is working correctly using the WCF Test Client. Once you have established that the service is working, update your service reference. If the source code for the Reference.cs file has not been generated by the custom tool, delete the bin folder, obj folder, service reference folder with all its contents, and the client service config file. Save the project and check in the project confirming all deletions. Then proceed to add the service references. At this point you should have the fully generated service references with source code and the basic client service config file. The final stage is to modify the bindings in the config file to include the timeouts set to “00:10:00”, the MaxBufferSize set to 2147483647 and set the MaxReceivedMessageSize to 2147483647.

A very painful excersise, but one with a happy ending:)

Update: 2011-05-27

This issue raised its ugly head again. I followed the steps above to fix the issue but they didn’t work in my project. So I created a new project that was not in TFS and added the service reference and it worked!

I had a look at the ServiceReferences.ClientConfig and noticed that the basicHttpBinding and the client endpoint were missing for the reference I was trying to add. So I added these entries to the ServiceReferences.ClientConfig file and then added the service reference, and sure enough the References.cs file was generated correctly.