开发者

WCF+WebService: 2 fields are generated to represent 1 integer

开发者 https://www.devze.com 2023-02-04 12:41 出处:网络
In my WCF service I have method with \'int\' parameter: [OperationContract] PublishResult PublishEnrollmentProfile(

In my WCF service I have method with 'int' parameter:

    [OperationContract]
    PublishResult PublishEnrollmentProfile(
        string siteName, int methodId,...
        );

When I created WebService reference to this WCF service the following signature was generated:

    [System.Web.Services.Protocols.SoapDocumentMethodAttribute("...",
        RequestNamespace="...", ResponseNamespace="...",
        Use=System.Web.Services.Description.SoapBindingUse.Literal,
        ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
    [return: System.Xml.Serialization.XmlElementAttribute(IsNullable=true)]
    public PublishResult PublishEnrollmentProfile(
        [System.Xml.Serialization.XmlElementAttribute(IsNullable=true)]
        string siteName,
        int methodId,
        [System.Xml.Serialization.XmlIgnoreAttribute()]
        bool methodIdSpecified, ...)
    {
        object[] results = this.Invoke("PublishEnrollmentProfile", new object[] {
                    siteName,
                    deployServerName,
                    methodId,
                    methodIdSpecified,
                    deviceClass,
                    deviceName,
            开发者_如何学JAVA        registrationCode});
        return ((PublishResult)(results[0]));
    }

You can see that instead of one integer parameter I've got 2: integer (for value) and bool (for mark 'if value was specified).

Is this ok? Why do I need the 2nd parameter (bool)?

Thanks a lot!


Specified" fields are only generated on optional parameters that are structs. (int, datetime, decimal etc). All such variables will have additional variable generated with the name Specified.

Since the "Specified" fields are only generated on optional parameters, if you placed the parameters for the PublishEnrollmentProfile method inside of a DataContract and set the DataMember attribute on methodID to [DataMember(IsRequired=true)] the specified field should go away, unless this is an optional field in which case you would want to leave it how it is.

Here's a blog posting with some samples.

UPDATE

So you have your Operation Contract.

[OperationContract]
PublishResult PublishEnrollmentProfile(string siteName, int methodId,...);

If the parameters of that method are not optional then you should create a DataContract and redefine the OperationContract like so:

{
   [OperationContract]
   PublishResult PublishEnrollmentProfile(PublishEnrollmentProfileRequest request);
}

And then you have your DataContract like this.

[DataContract]
public class PublishEnrollmentProfileRequest
{
    private string _siteName;
    [DataMember]
    public string siteName
    {
       get;
       set;
    }


    private int _methodId;
    [DataMember(IsRequired=True)]
    public int methodId
    {
       get;
       set;
    }

    .
    .
    .

} 

So you have a "request" object that you pass into the WCF service which has fields siteName and methodId. In the example I provided methodId is now required, this would elminate the "Specified" field.

0

精彩评论

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