Nimble Coder

Adventures in Nimble Coding
posts - 79, comments - 67, trackbacks - 1

Automatically setting the Specified property in WSDL Generated files

The wsdl.exe tool with Visual Studio can be used to manually generate a code file to use with a project. If you use the nillable or minOccurs attributes in the XSD though, the wsdl.exe tool generates a boolean 'Specified' field that must be set to true in order to transmit the data through the web service. For example:

WSDL File (myfile.wsdl)

<xs:element name="duedate" type="xs:dateTime" nillable="1" minOccurs="0" maxOccurs="1" />

Generated CS File (wsdl.exe myfile.wsdl)

// ...

private System.Nullable<System.DateTime> duedateField;

private bool duedateFieldSpecified;

// ...

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(IsNullable=true)]
public System.Nullable<System.DateTime> duedate {
    get {
        return this.duedateField;
    }
    set {
        this.duedateField = value;
    }
}

/// <remarks/>
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool duedateSpecified {
    get {
        return this.duedateFieldSpecified;
    }
    set {
        this.duedateFieldSpecified = value;
    }
}

"Breaking Change"

What I wanted was simply add the line to the 'duedate' setter to check if the newly assigned value was null or not (the assumption is that a non-null value would mean the field is 'specified'):

    set {
        this.duedateField = value;
        this.duedateFieldSpecified = (value != null);
    }

I looked around for a solution, but all I found a very good suggestion to add this functionality to the wsdl.exe tool. Unfortunately the status was "Closed (Won't Fix)" and the comment was:

Thank you for the suggestion.
This would be nice enhancement, but unfortunately this also is a breaking change: in the previous versions of the .net framework setting value of 'xxx' field did not toggle the xxxSepcified value, so if we change this runtime behavior of some existing applications may change and will require user code change to run the same way as before.

On the contrary, I contend it would not be difficult or breaking to simply add the functionality through optional parameters, such as:

wsdl.exe /autospecified:1 myfile.wsdl

The optional parameters would not affect default operation nor break existing code, but instead we have to deal with a short-sighted decision.

Quick and Dirty Solution

I decided to write a quick script to try and automatically generate the extra line of code for each setter. I decided to use awk (there are a number of sources for a Windows awk/gawk - cygwin, unxutils, and more) as it is very quick to implement and script. The following script is the result:

#!/usr/bin/awk -f

# This is a quick and dirty script to add automatically set the
# 'myFieldSpecified' variables generated from the Visual Studio
# wsdl.exe tool. The script searches for System.Nullable types 
# (myField) and verifies a specified variable exists 
# (myFieldSpecified). It then searches for the setter and adds
# the line: this.myFieldSpecified = (value != null);
#
# Copyright (C) 2008 Ryan Van Slooten
#
# Version:
#   1.0     2008-04-01      Initial release
#

BEGIN { FS = " "; }

# Match the nullable types in the file: int, DateTime, etc.
/[ \t]*private System.Nullable/ {
    print $0;

    # Add name of field to nullable array
    name = substr($3, 1, length($3)-1);
    nullable[name] = "";
    next; 
}

# Match the xxxFieldSpecified variables
/[ \t]*private .*FieldSpecified;/ {
    print $0;

    # Increment value of 'specified' null field to nullable array
    name = substr($3, 1, length($3)-10);
    specified = substr($3, 1, length($3)-1);
    nullable[name] = specified;
    next; 
}

# Match the field setter and check if it is nullable and specified
/[ \t]*this.* = value;/ {
    print $0;

    # Check if field is nullable and has a specified property
    name = substr($1, 6);
    if (name in nullable) {
        if (length(nullable[name]) > 0) {
            # Fix lost indentation level
            indent = substr($0, 1, index($0, $1)-1);

            # Adjust the xxxFieldSpecified value
            $1 = $1 "Specified";
            $3 = "(" substr($3, 1, length($3)-1) " != null);";
            print indent $0;

            # Remove element from array
            delete nullable[name];
        }
    }
    next;
}

# default line handler
{ print $0; }

kick it on DotNetKicks.com

Technorati tags: , , ,

Print | posted on Tuesday, April 01, 2008 3:51 PM | Filed Under [ C# Tools Programming ]

Feedback

Gravatar

# re: Automatically setting the Specified property in WSDL Generated files

[code]
[DataMember(IsRequired=true)]
public DateTime EditDate { get; set; }

[/code]



source :
stackoverflow.com/.../2047168#2047168
9/13/2010 12:35 PM | Tommy Masschelein

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 6 and 7 and type the answer here:

Powered by: