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:
[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
CodeProject