Reputation: 1792
I'm doing some wsdl- and client-first development in C# with WCF (the wsdl and client already exist, I'm building the server-side,) and I'm having an odd issue. I used wsdl.exe to generate a contract from my .wsdl, and I'm able to build it and host the WCF service as a Windows service.
However, the wsdl I get from http://localhost/Service?wsdl exposes private fields instead of the public properties (e.g.: instead of OsType
I get m_OsTypeField
, which is the private variable associated with the public OsType
property.)
Here are the attributes for one of the classes having this issue:
[System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://xxxxxxx.com/")]
I'm completely stumped, as the .NET XML serializer is supposed to ignore any private members. Any ideas about why this might be happening?
Upvotes: 2
Views: 4485
Reputation: 390
Your datacontracts are using the XmlSerializer engine but your OperationContract is using DataContractSerializer.
Apply the XmlSerializerFormatAttribute at the operation contract
From MSDN http://msdn.microsoft.com/en-us/library/ms732038(v=VS.90).aspx
Occasionally, the DataContractSerializer is not adequate to serialize your types. WCF supports an alternative serialization engine, the XmlSerializer, which you can also use to serialize parameters. The XmlSerializer allows you to use more control over the resultant XML using attributes such as the XmlAttributeAttribute. To switch to using the XmlSerializer for a particular operation or for the entire service, apply the XmlSerializerFormatAttribute attribute to an operation or a service. For example:
[ServiceContract]
public interface IAirfareQuoteService
{
[OperationContract]
[XmlSerializerFormat]
float GetAirfare(Itinerary itinerary, DateTime date);
}
For more information, see Using the XmlSerializer Class. Remember that manually switching to the XmlSerializer as shown here is not recommended unless you have specific reasons to do so as detailed in that topic.
Upvotes: 6
Reputation: 754993
If you're using WCF, you shouldn't be using wsdl.exe
but svcutil.exe
instead.
Also, the standard WCF DataContract serializer will happily serialize anything you've marked with a [DataMember] attribute - the .NET visibility setting has no bearing on the SOA view of your data, really.
However, from your code sample it would appear as if you're using the Xml Serializer and not the DataContractSerializer - probably because you used wsdl.exe instead of svcutil.exe.
Can you try to create the server side stubs using svcutil.exe ? Do you still see the same problem?
Marc
Upvotes: 2