Service Reference Error: Failed to generate code for the service reference

125,257

Solution 1

Have to uncheck the Reuse types in all referenced assemblies from Configure service reference option

Check this for details

Solution 2

Right click on your service reference and choose Configure Service Reference...

Configure Service Reference

Then uncheck Reuse types in referenced assemblies

Reuse Types

Click OK, clean and rebuild your solution.

Solution 3

I also encountered a similar error when trying to generate the client for a web service from an ASP .Net MVC 4.0 project using Visual Studio 2012.

The root of the problem seems to be that fact that the project from where I was trying to generate the client was referencing an assembly which in turn was dependent on another assembly that was not being referenced as well.

When "Reuse types in referenced assemblies" is enabled in the service configuration, the service generator is probably inspecting all the referenced assemblies to get a list of types that can be reused. The fact that one of the referenced assemblies is referencing another assembly which is not available is probably causing the generator to fail.

Unchecking "Reuse types in referenced assemblies" from the service configurations will solve the above problem, but there is a side effect to it. The reuse types option is there for a reason and in some cases it avoids unnecessary casting in the code consuming the service.

For example, if the service itself is built using WCF and some methods parameters inside it are of type System.Guid, they will be translated to strings in the generated client if the reuse types option is disabled.

An alternative that I prefer to disabling reusing types is to add the service reference from Class Library project specifically created for that purpose. The one thing to keep in mind is to copy all the service related configurations from the class library's app.config to the configuration file of the startup project.

If there are types defined in local assemblies that need to be reused in the service client, those assemblies simply need to be referenced from the above mentioned class library project, along with all their dependencies.

Solution 4

http://uliasz.com/2011/06/wcf-custom-tool-error-failed-to-generate-code-for-the-service-reference/#comment-1647

Thanks to the article above.

In my case, i have this issue with my WPF project in VS.Net 2008. After going through this article, i was realizing that the assembly used in the web service is different version of assembly used on client.

It works just fine after updating the assembly on the client.

Solution 5

It would be extremely difficult to guess the problem since it is due to a an error in the WSDL and without examining the WSDL, I cannot comment much more. So if you can share your WSDL, please do so.

All I can say is that there seems to be a missing schema in the WSDL (with the target namespace 'http://service.ebms.edi.cecid.hku.hk/'). I know about issues and different handling of the schema when include instructions are ignored.

Generally I have found Microsoft's implementation of web services pretty good so I think the web service is sending back dodgy WSDL.

Share:
125,257
user2119701
Author by

user2119701

Updated on July 24, 2020

Comments

  • user2119701
    user2119701 almost 4 years

    I have a Windows Service Solution and am trying to add a service reference to a Hermes(Opensource ebms message server) Web Service in VS2010.

    I can find the Web Service using it's URL, but when I try and populate the Service reference I get the following errors in Visual Studio:

    Error 8 Custom tool error: Failed to generate code for the service reference 'testService'.  Please check other error and warning messages for details. C:\Users\Admin\documents\visual studio 2010\Projects\MyProject\MyProject.MessageHandler\Service References\testService\Reference.svcmap 1 1 MyProject.MessageHandler
    
    
    Warning 6 Custom tool warning: Cannot import wsdl:binding
    Detail: There was an error importing a wsdl:portType that the wsdl:binding is dependent on.
    XPath to wsdl:portType: //wsdl:definitions[@targetNamespace='http://service.ebms.edi.cecid.hku.hk/']/wsdl:portType[@name='EbmsStatusQuery']
    XPath to Error Source: //wsdl:definitions[@targetNamespace='http://service.ebms.edi.cecid.hku.hk/']/wsdl:binding[@name='EbmsSoapHttpStatusQuery'] C:\Users\Admin\documents\visual studio 2010\Projects\MyProject\MyProject.MessageHandler\Service References\testService\Reference.svcmap 1 1 MyProject.MessageHandler
    
    Warning 7 Custom tool warning: Cannot import wsdl:port
    Detail: There was an error importing a wsdl:binding that the wsdl:port is dependent on.
    XPath to wsdl:binding: //wsdl:definitions[@targetNamespace='http://service.ebms.edi.cecid.hku.hk/']/wsdl:binding[@name='EbmsSoapHttpStatusQuery']
    XPath to Error Source: //wsdl:definitions[@targetNamespace='http://service.ebms.edi.cecid.hku.hk/']/wsdl:service[@name='EbmsMessageStatusQuery']/wsdl:port[@name='EbmsStatusQuery'] C:\Users\Admin\documents\visual studio 2010\Projects\MyProject\MyProject.MessageHandler\Service References\testService\Reference.svcmap 1 1 MyProject.MessageHandler
    
    Warning 5 Custom tool warning: Cannot import wsdl:portType
    Detail: An exception was thrown while running a WSDL import extension: System.ServiceModel.Description.XmlSerializerMessageContractImporter
    Error: Schema with target namespace 'http://service.ebms.edi.cecid.hku.hk/' could not be found.
    XPath to Error Source: //wsdl:definitions[@targetNamespace='http://service.ebms.edi.cecid.hku.hk/']/wsdl:portType[@name='EbmsStatusQuery'] C:\Users\Admin\documents\visual studio 2010\Projects\MyProject\MyProject.MessageHandler\Service References\testService\Reference.svcmap 1 1 MyProject.MessageHandler
    

    Some investigation seemed to suggest it is due to svcutil.exe not been able to build the proxys due to not having permissions to a directory (possibly c:\windows\temp). I have tried assigning various access permissions, but I am not really sure which user needs the permission, or if it is just a red herring.

    Any ideas would be greatly appreciated.

    Thanks

  • user2119701
    user2119701 over 13 years
    Thanks Aliostad, I will try and share the WSDL when I get in from work.
  • Florin Dumitrescu
    Florin Dumitrescu about 11 years
    Alternatively, when "Reuse types in referenced assemblies" is actually needed, you can add the service reference from a Class Library project specifically created for that purpose. Check my reply for additional info.
  • citronas
    citronas almost 10 years
    Weird. I changed my binding from HttpBinding to NetNamedPipeBinding (also added a mex endpoint) and encoutered this in my client assembly A. I created a new class libary B which referenced my WCF service. Then I added B as a reference to A and magically everything works again. Very strange. Thanks for the solution
  • Ricardo Appleton
    Ricardo Appleton over 7 years
    What if I need to Reuse the referenced assemblies? I have a project and that checkbox is checked. Update works in my machine, but on a workmate's machine it doesn't... any clues?
  • Shahin Dohan
    Shahin Dohan about 7 years
    I wanted a solution without unchecking "reuse types", and I managed to find one that worked, see my answer here.
  • Mike Flynn
    Mike Flynn almost 7 years
    This happened when I upgraded from v4.6.1 to v4.7 with my project. This fixed it.
  • Oscar Guillamon
    Oscar Guillamon over 4 years
    Instead of doing something like this, use the assemblies that the WCF client depends on instead of using all of the referenced ones blindly, It can be tricky and more timeconsuming than unchecking the checkbox in question, but I find this solution more suitable than the one marked as answer.
  • arihanth jain
    arihanth jain over 4 years
    Also make sure your Project is not Read Only..This was my problem..