May 2008 Entries

I ran in to this problem recently while developing a solution for a client.  For me it took a little while to figure out what was happening and the error was reported to be coming from a send shape that sent a message of type System.Xml.XmlDocument.

While this is the correct place for the error to be raised, it was not strictly related to them message that I was trying to send.

The actual cause of the problem is that I have a class that is used to return information back from a series of helper methods in a library that I have built.  The class has 3 public properties:

  • Result: An inumeration indicatign the level of success
  • Document: when successful, this is an XML document otherwise, it is null.
  • Exception: this is null if the call was successful, otherwise it contains all relevant information to describe the failure.

Document is a System.Xml.XmlDocument and it is this that is causing the error.  Essentially, BizTalk Server seems to be trying to serialize this class, either for dehydration, analyst reporting or some other reason.  All we need to do to avoid this error is simply mark the property as nonserializable, I.e:

/// <summary>
/// Contains the result of a receipt processing attempt.
/// </summary>
[Serializable()]
public class ReceiptResult
{
    private ReceiptSuccessResult _result;
    [NonSerialized()]
    private XmlDocument _document;
    private ReceiptException _exception;

    /// <summary>
    /// Gets the overall result of the validation attempt.
    /// </summary>
    public ReceiptSuccessResult Result
    {
        get { return this._result; }
    }

    /// <summary>
    /// Gets any XML document that may have been returned from the validation attempt.
    /// </summary>
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1059:MembersShouldNotExposeCertainConcreteTypes", MessageId = "System.Xml.XmlNode")]
    public XmlDocument Document
    {
        get { return this._document; }
    }

    /// <summary>
    /// Gets any exception that was returned from the validation attempt.
    /// </summary>
    public ReceiptException Exception
    {
        get { return this._exception; }
    }

    public ReceiptResult(ReceiptSuccessResult result)
    {
        this._result = result;
    }

    public ReceiptResult(ReceiptSuccessResult result, XmlDocument document)
    {
        this._result = result;
        this._document = document;
    }

    public ReceiptResult(ReceiptSuccessResult result, ReceiptException exception)
    {
        this._result = result;
        this._exception = exception;
    }
}

After that (for me at least) the error goes away.


Bookmark with :
Digg It! DZone StumbleUpon Technorati Reddit Del.icio.us Newsvine Furl Blinklist

Overview

The above message can be quite frustrating if you don't know why you are getting it.  Microsoft's take on this particular error message when you try to compile you BizTalk Server project is pretty much detailed in the article:

One of the times I have come across this problem is when I want to create a message in an orchestration that is not the result of a transform.  At first I was confused by this error, but it does sort of make sense.  To resolve the issue I figured that, if Visual Studio can create a document instance, then so can I.

The Problem

Consider the following scenario:

An orchestration wants to call a web service to save some data away to a third-party service.  Should this web service raise an exception for any reason, I want to create an instance of a message that contains an error report and then send it off to another orchestration for processing.  For this purpose I have create a schema for my error report message.  I don't have ownership of the web service, so I will make my call to it in a Scope shape and to that scope shape, I will add an exception handler to contain my error reporting.

All I want to do is set four properties in my error message; sender email, recipient email, subject and body.  If I just use a Message Assignment shape inside a Construct Message shape and try to set these properties, we get the error.

Solution

Using a couple of the less-than-well documented assemblies, we can create an empty XML instance of our document schema.  To do this, I added a helper class to my solution that would contain a series of helpers that could create an instance of a specified document schema.  These can be found in the following location on your BizTalk Server:

C:\Program Files\Microsoft BizTalk Server 2006\Microsoft.BizTalk.Pipeline.dll

C:\Program Files\Microsoft BizTalk Server 2006\Microsoft.XLANGs.BaseTypes.dll

using Microsoft.BizTalk.Component.Interop;
using Microsoft.XLANGs.BaseTypes;

Following that, I simply need to create a method for each message I need to create.  So for my error message type, I created the following:

/// <summary>
/// Creates an empty instance of the Error Message schema.
/// </summary>
/// <remarks>
/// Error handling has been left out in the hope that BizTalk Server might be able to raise the error message.
/// </remarks>
/// <returns>XmlDocument instance of the EBiz ErrorMessage schema</returns>
public static XmlDocument ConstructEBizErrorMessage()
{
    Type MessageSchemaType = typeof(TestProject.BizTalk.EBiz.ErrorMessage);

    DocumentSpec DocumentSpecification = new DocumentSpec(
        MessageSchemaType.FullName,
        MessageSchemaType.Assembly.FullName
        );

    XmlDocument SchemaInstance = new XmlDocument();
    using (StreamReader InstanceStreamReader = new StreamReader(DocumentSpecification.GetDocSchema().CreateXmlInstance()))
    {
        SchemaInstance.Load(InstanceStreamReader);
    }
    
    return SchemaInstance;
}

The class library with this method is then referenced by my BizTalk Server project that contains the orchestration and can be called from within a Message Assignment shape to effectively initialize the instance of the message so that the properties can be set.

The nice thing is, when I update my schema in the BizTalk Server project, this helper class needs no updating and will just work.

Lovely - time for a cup of tea. :)

Versions Metadata

Bookmark with :
Digg It! DZone StumbleUpon Technorati Reddit Del.icio.us Newsvine Furl Blinklist

I found these two great articles on MSDN about performance and scaling of ASP.Net applications.  They're well worth a read in my opinion.


Bookmark with :
Digg It! DZone StumbleUpon Technorati Reddit Del.icio.us Newsvine Furl Blinklist