Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

WCF Web Service Call to Another WCF Web Service returns no data

5.00/5 (4 votes)
8 Jan 2012CPOL2 min read 21.3K  
WCF Web Service Call to Another WCF Web Service returns no data

Problem Statement

I have a WCF IIS hosted web service which uses another WCF web service hosted in a different web application. The call to the other Web service always returns null or empty values on the data members of the data contract. If I call the other web service directly from a client application, the response of the service has no problem, and all the properties are populated correctly. I configured a trace on the request, and I can see that the second web service is handling the requests, but the data is not returned.

Solution

This problem is common when a WCF Web service acts as a client for another WCF Web service, and it can be addressed by adding a namespace to the Web service behavior and data contract that is used for the response. If you take a look at a trace file, you will be able to see that the actual data is coming back on XML nodes with a namespace that was created dynamically. Since the client does not understand this namespace, the data is placed on the ExtensionData field of the response (see inner Members collections). You can see this using the debugger. The actual data members are null or set to default values (defined in the class).

It is always recommended to set the service behavior and data contracts to a namespace to prevent unexpected behaviors. Take a look at the ServiceBehavior attribute on the service contract declaration and the DataContract attribute and assign a namespace as follows:

C#
[ServiceBehavior(Namespace = " urn:MyserviceNameSpace")]    
public class MyService: IMyService 

[DataContract(Name="MyResponse",Namespace="urn:MyserviceNameSpace"")]
public class MyResponse

Note how the Name attribute also matches the class name. After making these changes, the client proxy (for the service consuming the second web service) needs to be updated. This downloads the namespace declarations that would allow the binding to be accurate. If you debug this again, you will see that the ExtensionData Members collection is now null. This is because the binding is now correctly using the class properties.

I hope this helps.

og-bit.com

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)