Click here to Skip to main content
15,867,568 members
Articles / Programming Languages / C#
Article

WCF to BizTalk (Passing Typed DataSet)

Rate me:
Please Sign up or sign in to vote.
4.78/5 (4 votes)
13 Dec 2009CPOL4 min read 34.1K   6   3
This article explains how huge data that is persisted on a typed dataset could be sent from a WCF Service to BizTalk using wsHTTPBinding/basicHTTPBinding and thus enable solutions to benefit from the Biztalk Mapper to transform a WCF message to a destination schema.

Introduction

This article details a simple but powerful method of transferring data persisted in a relational typed dataset from a WCF Service to BizTalk Server, thus allowing the developer the opportunity to use the BizTalk Mapper to transform a WCF response message to an end destination message easily.

Background

I am currently working on an integration project which mainly focuses on parsing and processing HL7 messages.. yeah, you read it right, "HL7" meaning, we need to deal with lots of elements for a single message.

Now, for one of the HL7 message types (VXQ), I had to implement the Request-Response model of the solution using BizTalk Server, with the HL7 Accelerator as my parsing and transformation engine, and WCF as my SOA comprising of the business logic and data tier that would interact with SQL Server to retrieve the data requested in the VXQ message.

In simple, VXQ requests for a patient and his hierarchical data that we have in our fully relational tables in a SQL Server database.

Bird's eye view of the steps to be followed

  1. Create the typed dataset replicating the data relations as in the datastore (in this article, it is SQL Server).
  2. Create a single Stored Procedure that will return multiple resultsets for all the tables that you need to construct the outbound message for.
  3. Populate the dataset using your preferred approach, or the conventional ADO.NET approach, i.e., DataAdapter.Fill will fill all the tables returned by the Stored Procedure.
  4. Using XSDObjectGenerator.exe, create the serializable class of the typed dataset, so it can be used as the DataContract for the WCF part.
  5. Create the WCF part and specify the operation contract and data contract (with the serializable class created in step 4).
  6. Convert the dataset to the serializable XML object that can be sent to BizTalk.
  7. Publish the WCF service and consume it in BizTalk Server Orchestration using the request_response port.

Using the code

  1. Create the typed dataset (HL7dataset.xsd) and define the data relation: We can do this by simply dragging and dropping the tables of interest from the Server Explorer or creating them in Visual Studio. Please see the screenshot below:
  2. Click

    Make sure you enable the "Nested Relation" checkbox. Otherwise, the code generation (XSDobjectGen) will not be able to create the hierarchical class for you.

  3. Create the Stored Procedure to return the multiple dataset: This is fairly simple and you can find a lot of articles for the same in CodeProject.
  4. Populate the dataset using DataAdapter.Fill: Here, we have to use table index based data population to the dataset from the SQL Server source. Refer to the code below.
  5. C#
    scmd.Connection = new SqlConnection(@"Data Source="[ServerName]";Initial " + 
                      @"Catalog=[DatabaseName];Persist Security " + 
                      @"Info=True;User ID=User;Password=welcome");
    SqlDataAdapter sadapter = new SqlDataAdapter(scmd);
    
    sadapter.TableMappings.Add("Table", "HL7Patients");
    sadapter.TableMappings.Add("Table1", "HL7Contacts");
    sadapter.Fill(objHL7dataset);

    Note that, we have to use table mappings to fill the typed dataset. It would not automatically load based on the table names; hence, we need to use table indexes and correlate the order in the Stored Procedure as well.

  6. Using the XSDObject generator, create the serializable class of the typed dataset: You can download the XSDObjectGenerator from here.
  7. Create the serializable class by using the VS command prompt with the following command:

    [Directory]\XSDObjectGenerator>XSDObjectGen.exe HL7dataset.xsd 
             /l:cs /n:"MyProject.HL7Services"  /f:HL7dataset.cs
  8. Create the WCF part and specify the operation contract and data contract (with the serializable class created in step 4): Create the XML serializable WCF service using the attribute [XmlSerializerFormat] for the operation.
  9. C#
    [ServiceContract] 
    public interface IVXQService { 
        [OperationContract] 
        [XmlSerializerFormat] 
        MyProject.HL7Services.HL7DataSet ProcessVXQ(InboundMsg objVXQ); 
    }

    Note: The "HL7DataSet" that you see in the above code is not the typed dataset, but instead, it is the XMLSerializable class that was created in step 4. To avoid conflict with class names and its members, make sure you use different namespaces, but retain the class name as-is. Otherwise, we won't be able to convert the typed dataset to the XMLSerializable class and send it to BizTalk.

  10. Convert the DataSet to the serializable XML object: This is the trickiest part. Here we convert the dataset to a MemoryStream and then deserialize it to a message that will be sent to BizTalk.
  11. C#
    MemoryStream stream = new MemoryStream(); 
    XmlTextWriter writer = new XmlTextWriter(stream, System.Text.Encoding.UTF8); 
    writer.WriteStartDocument();
    ds.WriteXml(writer); stream.Seek(0, SeekOrigin.Begin); 
    using (StreamReader str = new StreamReader(stream))
    {
        XmlSerializer xs1 = 
               new XmlSerializer(typeof(MyProject.HL7Services.HL7DataSet));
        MyProject.HL7Services.HL7DataSet Result = 
              (MyProject.HL7Services.HL7DataSet)xs1.Deserialize(str);
    }
  12. Publish the WCF service and consume it in a BizTalk Server Orchestration using the request_response port: Now that the WCF service is ready, publish it and add a reference to the WCF service from the BizTalk project: "Right click the BizTalk project-->Add-->Add Generated Items-->Consume WCF Service".
  13. Click

    Once the wizard creates and adds all the artifacts for the WCF reference, we would have a multipart message, one for the request and another for the response in the orchestration named after your WCF service (VXQService). We will be able to implement our logic in the response message either by using transform shapes or message assignment shapes.

    Note: We need to import the custom binding file created by the above step using the BizTalk Administration Console, which will automatically create the Request-Response send port in the BizTalk application.

Points of interest

  1. Converting the typed dataset to an XML serializable object that can be transmitted to BizTalk.
  2. Increasing the property "maxReceivedMessageSize="365536"" for the WCF client (BizTalk Server Send Port) to accept an XML message of size greater than the default value.
  3. It should work for all types of WCF bindings.
  4. Steps are clearly explained in the article. If anyone needs the sample code, please let me know, I would be uploading it on a demand basis.

History

Draft version.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead IncentOne Inc
United States United States
I am a qualified software professional with over 7 yrs of experience, I started my career with Asp and VB and since then have been learning and going along with microsoft and their products. BizTalk server is my favorite one and have worked in all versions of it and exploring BizTalk 2009 now.


Vasanth Subramanyam

Comments and Discussions

 
Generalsource code please Pin
zd.luo8-Jun-10 16:06
zd.luo8-Jun-10 16:06 
GeneralRe: source code please Pin
vasanth1922-Sep-10 8:26
vasanth1922-Sep-10 8:26 
GeneralNice technique, source code please Pin
MightyMart14-Dec-09 4:19
MightyMart14-Dec-09 4:19 
Vasanth19, this looks like really good approach to be used in my Biztalk projects. Altough the technique seems pretty simple, would it be possible to obtain the source code so that BT newbies can benefit?

Thanks

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.