问题
I'm looking for your best solutions for creating a new message instance based on a pre-defined XSD schema to be used within a Biztalk orchestration.
Extra votes go to answers with clear & efficient examples or answers with quality referenced links.
回答1:
What exactly are you looking for? Is it just creating a new message with a fixed content (like a sort of template)? Or based on something else? You really need to clarify the question and be more specific to get a proper answer.
If you're referring to just creating a message from scratch based with sort of hardcoded content (or close to), then I've found that putting them as embedded resources in a helper C# assembly to be a pretty clean way of doing it.
回答2:
There are several options when wanting to create a new instance of a message in a BizTalk orchestration.
I've described the three I usually end up using as well as adding some links at the bottom of the answer.
How to define which is the best method really depends - the XMLDocument method is in some regards the tidiest except that if your schema changes this can break without you knowing it. Scott Colestock describes some methods of mitigating that risk.
The BizTalk Mapping method is probably the simplest to understand and won't break when the schema changes. For small schemas this can be a good choice.
For all of these methods an important thing to remember is that if you want to use distinguished fields or promoted properties you will want to create empty elements to populate. You will hit runtime XLANG
errors if you try to assign values to elements that are missing (even though those elements may be optional)
BizTalk Map
The simplest option is to just use a BizTalk map - you don't even necessarily need to map anything into the created instance.
To create empty elements you can just map in a string concatenation functoid with an empty string parameter.
Assign one message to another
If you want to create a new instance of a message you can simply copy one mesage to another message of the same schema, in a message assignment shape.
Use an XMLDocument variable
For this you create an orchestration variable of type XMLDocument
and then in a message assignment
use the LoadXML
method to load an XML snippet that matches your schema. You then assign the XMLDocument
to the desired BizTalk message.
varXMLDoc.LoadXml(@"<ns0:SomeXML><AnElementToPopulate></AnElementToPopulate></SomeXML>");
msgYourMessage = varXMLDom;
The inclusion of AnElementToPopulate
allows you to using property promotion to assign to it.
I seldom remember the syntax to do this off the top of my head, this is my go to blog entry for reminding myself of the syntax.
Another link here details some methods.
回答3:
To create a new message you can simply create a new System.Xml.XmlDocument
and assign that to a message variable. You can use it's Load
or LoadXml
methods to load the required content that conforms to the schema.
回答4:
This tutorial may be of some help:
BizTalk Server 2006 Tutorial - A Walk Through the Process Creating services with contract-first design using BizTalk Server 2006 R2 and Windows Communication Foundation
http://dotnet.sys-con.com/node/647092
回答5:
Check out my blog post - Fun with Message Creation in BizTalk - for a basic performance comparison between various options.
回答6:
xsd.exe /classes /namespace:MyNamespace myschemafile.xsd
You can use this to generate c# classes for a given schema file. The result is a .cs file that you can include in one of your solution projects.
When using within a "Message Assignment Shape", you can instantiate one of these generated classes, fill out values for all it's properties, then finally assign the Message part to your instance. Biztalk will auto-magically serialize the instance for you. Nice and OO. No need for any fancy xlang stuff.
I didn't really have much luck with some of the other solutions like loading up a temp XmlDocument with hardcoded XML, or going the whole hog and using the documentSpecification.GetDocSchema().CreateXmlInstance() that others have suggested.
来源:https://stackoverflow.com/questions/313269/what-is-the-best-way-to-create-a-new-message-within-a-biztalk-orchestration