tags:

views:

834

answers:

2

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?

+2  A: 

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

marc_s
The interface generated using SvcUtil.exe with the DataContractSerializer contains void, parameter-less methods (the methods should accept a request type and return a response type.) In addition to this, no data types are generated. Am I missing something with SvcUtil?Also, not sure if this matters: if I set the /ser option to "auto", the XmlSerializer is used.
oltman
Update: I got the code generated by SvcUtil.exe to work and publish a correct wsdl. A couple questions though: 1) what is wsdl.exe intended to be used for if not for WCF? 2) What was I doing wrong with SvcUtil.exe when using the DataContractSerializer?
oltman
wsdl.exe was used for "old-style" ASMX webservices - that's .NET 1.x/2.x technology - last century almost ;-)
marc_s
not sure about the DataContractSerializer - it's the default and should be used, but there are certain conditions (and obviously your wsdl had one of those, I guess) that make it impossible for WCF to use the DataContractSerializer (like attributes in the XML message) and then it'll switch to use the XmlSerializer.
marc_s
All very good to know. Thanks!
oltman
A good post regarding SvcUtil's use of XmlSerializer instead of the DataContractSerializer: http://webservices20.blogspot.com/2008/10/interoperability-gotcha-doclitwrapped.html
oltman
A: 

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.

sjclark76