Shift Nibble

Happy Coding ...

Problem in Consuming WCF Service from WebApi

I was trying to consume SecureNet (Payment Gateway) wcf service within web api today. I added a reference in my web api project but was not able to access the service through proxy class. I found many warnings, one of them is below

Warning    1    Custom tool warning: Cannot import wsdl:portType Detail: An exception was thrown while running a WSDL import extension: System.ServiceModel.Description.DataContractSerializerMessageContractImporter Error: Type 'Newtonsoft.Json.Linq.JToken' is a recursive collection data contract which is not supported. Consider modifying the definition of collection 'Newtonsoft.Json.Linq.JToken' to remove references to itself. XPath to Error Source: //wsdl:definitions[@targetNamespace='']/wsdl:portType[@name='IGateway']    c:\Project\Service References\SecureNet\Reference.svcmap    1    1    Project

I found that the problem was due to the reuse of assemblies in referenced services. You should either un-check this option when adding reference of service (in advance menu) or select only the assemblies which are required to reuse.

New Reference

If you are adding new reference then click "Advanced..." button and un-check "Reuse types in referenced assemblies" (see below image).


Fix Existing Reference

If you have already added a reference and finding solution to fix it then right click on service reference (Solution Explorer > Service References > YourServiceReference) and select "Configure Service Reference..." option. You will see a configuration window (see above image) where you can un-check the "Reuse types in referenced assemblies" option.