Freigeben über


BizTalk messages based on .NET types instead of Xsd schemas

Abstract: You can create a BizTalk message from a custom .NET class, instead of using an Xsd schema. This practice has some pros and cons. Let’s see…

Usually, the most common process is to start creating Xsd schemas, and use them in Orchestrations, to define BizTalk messages. But, why would you like to have BizTalk messages based on Xml? It seems obvious that all messages should be Xml based, but in fact, sometimes Xsd/Xml is not necessary and adds complexity.

When is Xsd/Xml needed?
Xml messages are very nice if you need Messaging. That is, interoperatibility, external communications, schema publication. But surprisingly, I find many situations where people are defining Xsd schemas for Xml messages where Xml is not needed at all. Well, Xml is still fashionable, so…
If a message lives inside an orchestration (is being passed from within orchestrations), but it’s never going to be published externally, or is never going to be sent/received externally, there’s no advantage in using Xml. In fact, there’s no reason to use Xml.

Xsd hell: how do you create a message from the scratch?
It’s a well known issue that creating an empty instance of a message inside an orchestration is not easy. Having the xml string hardcoded is not very elegant. Pointing to a file is not very elegant at all. You would need the path harcoded. Well, you can put it in a .config file, but… isn’t too complex to just create an empty message?
I’ve read some discussion about using Maps to create empty (or default valued) instances. Since I agree it’s a good idea, it’s still too complex to just create an instance of a message.
This message-from-the-scratch problem is inherent to the use of Xml, since a message is not instantiable.

Using a .NET class
You can use a .NET class, instead a Xsd schema. Define your orchestration message as a .NET type, using your class. Use references to XLANGs Base Types to promote your class properties to distinguised fields or properties.
Pros:
Instantiate it. Use constructors, destructors, static members for instance creation or whatever you want.
Use rich properties. Mark public properties as promoted or distinguished fields. Use get and set methods.
Reuse as objects in other projects.
Cons:
External publication and Interoperatibility. There is no Schema, so if you intend to publish it externally, what do you publish?
Dependency of XLANG assemblies, so limited reuse outside your BizTalk project.

Simple Sample:
Create a .NET class:
using System;
namespace STCEAI.Messages
{
[Serializable]
public class SimpleMessage
{
private string _id;

  public SimpleMessage()
{
}

  [Microsoft.XLANGs.BaseTypes.DistinguishedField]
public string Id
{
get{ return _id;}
set{ _id = value;}
}

  public static SimpleMessage Create()
{
SimpleMessage msg = new SimpleMessage();
msg.Id = System.Guid.NewGuid().ToString();
return msg;
}
}
}

Note the property is marked as Distinguished Field in order to make it visible from within the orchestration. This attribute is in Microsoft.BizTalk.XLANGs.BaseTypes.dll. You can also mark it as a promoted property, assign a Namespace, and include all the Xml serialization attributes as needed.

Usage inside an orchestration, in a Message Assignment Shape :
SimpleMessage_msg = new STCEAI.Messages.SimpleMessage();
SimpleMessage_msg.Id = myId;

or better:
SimpleMessage_msg = new STCEAI.Messages.SimpleMessage.Create();

Comments

  • Anonymous
    June 02, 2005
    you said "Cons:
    External publication and Interoperatibility. There is no Schema, so if you intend to publish it externally, what do you publish?
    "
    XSD.exe is a tool that will output a schema based upon the type. This is actually what the orchestration compiler does if you have a .NET type as a message type. Look at the generated orchestration - there is a schema inside. :)

  • Anonymous
    September 15, 2005
    However, an xsd-generated schema will not include the distinguished fields. Not sure what other attributes it might omit.

  • Anonymous
    July 19, 2006
    Great help - thanks for this! I was tearing my hair out.

    By the way i'm using XSD to generate the class then i'm doing a find and replace on to change the attributes over my fields from...

    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]

    to

    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified), Microsoft.XLANGs.BaseTypes.DistinguishedField()]

    Obviously this quick fix is heavily flawed but as there could be other XSD generated attributes that get generate.

    Its working for me at the moment though cause im only using basic types like String, Int, DateTime etc...

    You'll soon find out though because when you come to compile your orchestrations they'll complain when you haven't set one of your properties


    Thanks again for ending my search for information on this for the last 24 hours!!

  • Anonymous
    March 01, 2007
    Abstract:YoucancreateaBizTalkmessagefromacustom.NETclass,insteadofusinganXsdschema....

  • Anonymous
    June 09, 2009
    PingBack from http://hairgrowthproducts.info/story.php?id=4782

  • Anonymous
    July 13, 2009
    Thanks David, great post. I can't find the attribute in order to mark the field as promoted. Can you tell me what attribute is it?