Friday, May 23, 2014

Step By Step How to solve the MVC error "Failed to generate code for the service reference" in Visual Studio 2012

         By Carmel Shvartzman

In this tutorial we'll learn How to take care of the MVC error "Failed to generate code for the service reference"  while generating the Proxy classes for a WCF Web Service in Visual Studio 2012
When adding a WCF Service Reference to an MVC application in Visual Studio 2012, there is a frequent issue that resolves in an error, causing the Visual Studio to fail in creating the Proxy classes that represent the WCF Web Service in the Client application. We'll learn how to deal with that error.

The error is produced after we add a service reference to our application, as follows:



The Web Service is found, and we add it to the app:


The error tells us that it was a failure while trying to generate the Proxy code for the Service :



A search at Microsoft Support explains that the bug was fixed in the new version of the .Net Framework 4.5:


The update fixes the case in which the code is not generated :


Indeed ' we can see after the project building, that the Proxy code is empty :


Also, the Web Config file has not been updated to reflect the new Web Service Client Endpoint :



To solve the problem without upgrading the Framework version with the new update available at the Support web site, we just open the Service reference Configuration :



In the Configuration dialog, look for the "Reuse types in referenced assemblies" checkbox :


And uncheck the "Reuse types in referenced assemblies" checkbox .
Build your project again.
Open the Reference.cs file to see the Proxy classes :



This time we got the classes and methods.
Open the web.config file:


As you can see , now the WCF endpoint is referenced.

Now you can use the Service Reference, and its methods :


The WCF Web Service is up and running, and you get the data :



So you can add a new View strongly-typed, using the Proxy classes :


Your View should appear as follows :


This is the composite type sent by the WCF Web Service to our MVC application.


That's All !!!! 
In this tutorial we've learned how to take care of the MVC error "Failed to generate code for the service reference" while generating the Proxy classes for a WCF Web Service  in Visual Studio 2012.  

Happy programming.....


עריכה:  כרמל שוורצמן


No comments:

Post a Comment