Thursday, July 17, 2008

Get the XmlEnum Value for an Enum

When you serialize an enumeration (Enum in VB.NET, enum in C#), by default the XML string representation of the enumerated member is the name of the enumeration member (i.e. the same as calling the ToString function of the enumeration member). For example, if the Color property of the following object was set to Green, the XML representation of that property would be <Color>Green</Color>:

Public Enum PrimaryColor
    Invalid = 0
    Red = 1
    Green = 2
    Blue = 3
End Enum

<Serializable()> _
Public Class Sample

   
Public Property Color() As PrimaryColor
        Get
            Return mColor
        End Get
        Set(ByVal value As PrimaryColor)
            mColor = value
        End Set
    End Property
    Private mColor As PrimaryColor

End
Class

This default behavior can be modified using the XmlEnumAttribute Class. For example, if the enumeration defined above was changed as shown below, the XML representation of the Color property would be <Color>G</Color>.

Public Enum PrimaryColor
    Invalid = 0
    <System.Xml.Serialization.XmlEnum("R")> _
    Red = 1
    <System.Xml.Serialization.XmlEnum("G")> _
    Green = 2
    <System.Xml.Serialization.XmlEnum("B")> _
    Blue = 3
End Enum

Even with the new XmlEnum values applied to the enumeration members, the ToString function will still return the name of the enumeration member, not the XmlEnum value. The following function will take an enumeration value and return its XML string value (either the XmlEnum value, if present, or the ToString value):

Public Shared Function ToXmlString( _
    ByVal value As [Enum]) As String

    Dim XmlEnumAttributes() As _
        System.Xml.Serialization.XmlEnumAttribute
    Dim EnumFieldInfo As System.Reflection.FieldInfo

   
' Verify Input Arguments
    If value Is Nothing Then
        Throw New ArgumentNullException("value")
    End If

    ' Get the Field Information for the 
    '
enumeration member
    EnumFieldInfo = _ 
        value.GetType().GetField(value.ToString())

    ' Get the XmlEnum attributes for the 
    ' enumeration member
    XmlEnumAttributes = CType( _
        EnumFieldInfo.GetCustomAttributes( _
        GetType(System.Xml.Serialization.XmlEnumAttribute), True), _
        System.Xml.Serialization.XmlEnumAttribute())

    ' Check to see if an XmlEnum attribute was found
    If XmlEnumAttributes.Length < 1 Then
        ' Return the default value
        Return value.ToString()
    Else
        ' Return the XmlEnum value
        Return XmlEnumAttributes(0).Name
    End If

End Function

Friday, December 28, 2007

.NET Method Parameters and Their SOAP

This article provides an overview of the different ways .NET Web Services SOAP will be generated based on how the parameters or return values are declared.

No Output
The following is the SOAP for a web method with no output:

VB.NET Code

<WebMethod()> _
Public Sub NoOutput()

End Sub

C# Code

[WebMethod]
public void NoOutput()
{
}

SOAP Input

  <soap:Body>
    <NoOutput xmlns="urn:sample" />
  </soap:Body>

SOAP Output

  <soap:Body>
    <NoOutputResponse xmlns="urn:sample" />
  </soap:Body>

Output Only
The following is the SOAP for a web method with no parameters that returns a string value:

VB.NET Code

<WebMethod()> _
Public Function StringOutput() As String
    Return "test"
End Function

C# Code

[WebMethod]
public string StringOutput()
{
    return "test";
}

SOAP Input

  <soap:Body>
    <StringOutput xmlns="urn:sample" />
  </soap:Body>

SOAP Output

  <soap:Body>
    <StringOutputResponse xmlns="urn:sample">
      <StringOutputResult>string</StringOutputResult>
    </StringOutputResponse>
  </soap:Body>

Input Only Parameter 
The following is the SOAP for a web method with a single integer input-only parameter and no return value:

VB.NET Code

<WebMethod()> _
Public Sub InputOnly(ByVal InputInt As Integer)
End Sub

C# Code

[WebMethod]
public void InputOnly(int InputInt)
{
}

SOAP Input

  <soap:Body>
    <InputOnly xmlns="urn:sample">
      <InputInt>int</InputInt>
    </InputOnly>
  </soap:Body>

SOAP Output

  <soap:Body>
    <InputOnlyResponse xmlns="urn:sample" />
  </soap:Body>

Output Only Parameter
The following is the SOAP for a web method with a single integer output-only parameter and no return value:

VB.NET Code

<WebMethod()> _
Public Sub OutputOnly( _
    <Runtime.InteropServices.Out()> ByRef _
    OutputInt
As Integer)
    OutputInt = 0
End Sub

C# Code

[WebMethod]
public void OutputOnly(out int OutputInt)
{
    OutputInt = 0;
}

SOAP Input

  <soap:Body>
    <OutputOnly xmlns="urn:sample" />
  </soap:Body>

SOAP Output

  <soap:Body>
    <OutputOnlyResponse xmlns="urn:sample">
      <OutputInt>int</OutputInt>
    </OutputOnlyResponse>
  </soap:Body>

Input-Output Parameter
The following is the SOAP for a web method with a single integer input-output parameter and no return value:

VB.NET Code

<WebMethod()> _
Public Sub InputOutput(ByRef InOutInt As Integer)
End Sub

C# Code

[WebMethod]
public void InputOutput(ref int InOutInt)
{
}

SOAP Input

  <soap:Body>
    <InputOutput xmlns="urn:sample">
      <InOutInt>int</InOutInt>
    </InputOutput>
  </soap:Body>

SOAP Output

  <soap:Body>
    <InputOutputResponse xmlns="urn:sample">
      <InOutInt>int</InOutInt>
    </InputOutputResponse>
  </soap:Body>

VMware and .NET Timers

The Problem
We recently had a client inform us that a Windows Service we had written would occasionally appear to be running, but not actually be doing anything. Once the Windows Service was restarted, it would begin processing again. The service would get into this state after it had been running for awhile, but it was never the same amount of time. Sometimes it would run for days without issue, but other times the problem would appear after only 8 hours of operation. We had not made any changes to the Windows Service in a long time so we were a little baffled. We attempted to reproduce the error, but were not successful.

After much investigation it was discovered that the client had moved the Windows Service to a VMware Virtual Machine. We had the client move the service back to a physical machine and the problem went away. At that point we knew something in our service did not agree with VMware, but our service was written using Visual Basic 2005 and was not doing anything out of the ordinary (e.g. no calls to unmanaged code, no third-party libraries, etc.).

We finally narrowed the problem down to a System.Timers.Timer we were using to determine when the Windows Service should start processing again once it had completed its work and had gone to sleep. Apparently the System.Timers.Timer would stop firing at some point, which prevented our application from waking up. The timer would only stop firing if the service was run on a VMware Virtual Machine.

The Solution
We were able to replace the System.Timers.Timer with a background worker thread (specifically a System.ComponentModel.BackgroundWorker control) that used a call to System.Threading.Sleep. This allowed our Windows Service to work on both physical machines and VMware Virtual Machines.

Some Lessons Learned

  • .NET Timers and other time keeping techniques may not function properly in a VMware Virtual Machine.
  • Additional testing needs to be performed to verify an application will work properly in a VMware Virtual Machine.
  • The System.Threading.Sleep method does not appear to be affected by the issues that caused the System.Timers.Timer to stop firing.

More Details
VMware has an excellent article called "Timekeeping in VMware Virtual Machines", which can be found at http://www.vmware.com/resources/techresources/238. This article contains information that might explain why our .NET timer stopped firing.

Another valuable resource for .NET timers is an article written by Alex Calvo called "Comparing the Timer Classes in the .NET Framework Class Library", which can be found at http://msdn.microsoft.com/msdnmag/issues/ 04/02/TimersinNET/default.aspxhttps://docs.microsoft.com/en-us/archive/msdn-magazine/2004/february/comparing-the-timer-classes-in-the-net-framework-class-library.