开发者

How to serialize parameters in Web Service method

I have this problem. I have WCF .Net C# web service with this method:

public interface IMyService
{
    // TODO: Add your service operations here
    [OperationContract]
    ListOfRequests GetListOfRequests(string par1,
                                     string par2,
                                     string par3,
                                     DateTime par4,
                                     DateTime par5,
                                     string par6,
                                     string par7);
}
public class MyService : IMyService
{
     public ListOfRequests GetListOfRequests(string par1,
                                 string par2,
                                 string par3,
                                 DateTime par4,
                                 DateTime par5,
                                 string par6,
                                 string par7)
     {
            // .... web method code here;
     }
}

The problem is that when I generate the web service the wsdl schema return this:

<xs:element name="GetListOfRequests">
  <xs:complexType>
   <xs:sequence>
    <xs:element minOccurs="0" name="par1" nillable="true" type="xs:string" /> 
    <xs:element minOccurs="0" name="par2" nillable="true" type="xs:string" /> 
    <xs:element minOccurs="0" name="par3" nillable="true" type="xs:string" /> 
    <xs:element minOccurs="0" name="par4" type="xs:dateTime" /> 
    <xs:element minOccurs="0" name="par5" type="xs:dateTime" /> 
    <xs:element minOccurs="0" name="par6" nillable="true" type="xs:string" /> 
    <xs:element minOccurs="0" name="par7" nillable="true" type="xs:string" /> 
   </xs:sequence>
  </xs:complexType>
  </xs:element>

and I want my parameters to be not null like this:

<xs:element name="GetListOfRequests">
  <xs:complexType>
   <xs:sequence>
    <xs:element minOccurs="1" name="par1" nillable="false" type="xs:string" /> 
    <xs:element minOccurs="1" name="par2" nillable="false" type="xs:string" /> 
    <xs:element minOccurs="1" name="par3" nillable="false" type="xs:string" /> 
    <xs:element minOccurs="1" name="par4" type="xs:dateTime" /> 
    <xs:element minOccurs="1" name="par5" type="xs:dateTime" /> 
    <xs:element minOccurs="1" name="par6" nillable="false" type="xs:string" /> 
    <xs:element minOccurs="0" name="par7" nillable="true" type="xs:string" /> 
   开发者_JAVA百科</xs:sequence>
  </xs:complexType>
  </xs:element>

How can I serizalize parameters to achieve this? Thank you in advance for your help. Regards, Georgi


You can remove

nillable="true"

with

[DataContract]
public class ListOfRequests
{
   DataMember(EmitDefaultValue=false)] 
   public string ListOfRequestsMember;
   // ...
}

The problem here is that the default value of type string is null. So setting this property to false you omit the data when it is set to its default value

In the .NET Framework, types have a concept of default values. For example, for any reference type the default value is null, and for an integer type it is 0. It is occasionally desirable to omit a data member from the serialized data when it is set to its default value. To do this, set the EmitDefaultValue property to false (it is true by default).

ref

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜