Web Services in Messaging - Part II

by matt 17. April 2007 08:27
This article is really just to give more clarity on my previous article about consuming web services in BizTalk using messaging without Orchestrations. I wanted to be able to take those who are fairly new to BizTalk Server through the whole process. What I did not mention in my pervious article is that this solution was reached after a fair bit of investigation between Marvin Smit and I while I was attending one of the Develop Mentor training sessions that he was running. While the end result seems pretty straight forward, it took us a while to get there. There are no doubt improvements that can be made to the process, but this is a nice starting block.

Anyway, down to business:

Building the Solution

The following are the overall steps needed to perform a demonstration of consuming a web service using Messaging only. I don’t really care which language you use. My weapon of choice is C# out of personal preference, so that’s what you’re stuck with if I have to write any code.
  1. The first thing that you should do is launch Visual Studio and create an empty Solution name WebMessaging.
  2. In the WebMessaging solution, create a ASP.Net Web Service Project. Call this web service ‘MyWebService’. I’ll assume that this web serive runs of local host in this example, but there may well be ports etc involved depending on how you set the project up.
  3. In the WebMessaging solution, create and empty BizTalk Server project and name it ‘MyBizTalkSample’.
  4. We’ll build the web service first, as this make more sense to me.
    1. Open up service.cs and create a new class name ‘OrderStatusUpdate’. This class will have two properties: the order Id; the order status. You can use the following code below for the class (notice that it must be serializable):
      [Serializable]
      public class OrderStatusUpdate
      {
          private Guid ordered;
          private int orderStatus;

          public Guid OrderId
          {
              get { return this.orderId; }
              set { this.orderId = value; }
          }

          public int OrderStatus
          {
              get { return this.orderStatus; }
              set { this.OrderStatus = value; }
          }

          public OrderStatusUpdate()
          {
              // Just empty here, this is for serialization
          }
      }
    2. Next we need a web method that will use this class. We simply want to send a status update in and deal with it. No output is required here. So, add the following web method to your web service:
      [WebMethod]
      public void UpdateOrderStatus(OrderStatusUpdate statusUpdate)
      {
          EventLog.WriteEntry(
              String.Format(
                  "Order [{0}] status set to '{1}'",
                  statusUpdate.OrderId,
                  statusUpdate.OrderStatus
                  )
              );
      }
      Note that I also added the following code to the service constructor for good measure:
      if (!EventLog.SourceExists("MyWebService"))
      {
          EventLog.CreateEventSource("MyWebService", "Application");
      }

Problems Found

Enumerations in web services were the first problem that we found when trying this out. This kind of hampered us quite a bit in the beginning. In my first attempt, I added the following enumeration to my web service class:

enum OrderStatus : int
{
    Accepted = 1,
    Declined = 2,
    Invoiced = 3,
    Dispatched = 4,
    Canceled = 5,
}

Lovely. Well – actually not. When the WSDL is created for a method that requires a parameter of this type as follows:

[WebMethod]
public void UpdateOrderStatus(Guid OrderId, OrderStatus NewStatus)
{
    // Do something exciting and revolutionary with the order status
}

The WSDL generated for that web method by VS.Net gets the enumeration wrong in my opinion (after a lengthy discussion to which I agreed).  It specified that the alloed values for something implementing the OrderStatus enumration are 'blah' and 'blah'.  But my enumeration is an integer, so that does not make sense!  My SOAP request woudl need to look as follows in order to comply with the WSDL:
POST /WebService/Service.asmx HTTP/1.1
Host: localhost
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/UpdateOrderStatus"

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<UpdateOrderStatus xmlns="http://tempuri.org/">
<OrderId>guid</OrderId>
<NewStatus>OrderAccepted or OrderDeclined</NewStatus>
</UpdateOrderStatus2>
</soap:Body>
</soap:Envelope>
Doesn't look too goot to me.  In short, for now - avoid enumerations.

Tags:

BizTalk Server

Powered by BlogEngine.NET 1.5.0.7
Theme by Interakting

Interakting

A full service digital agency offering online strategy, design and usability, systems integration and online marketing services that deliver real business benefits and ensure your online objectives are met.

Calendar

<<  February 2012  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
2728291234
567891011

View posts in large calendar