Thursday, October 30, 2008 9:08 AM cibrax

REST and Workflow services play well together - Part II

In my previous post "REST and Workflow services play well together", I mentioned that Http Cookies were one of the built-in mechanisms for transferring the workflow context across calls between the client and the service. While cookies work well for http services, from my point of view, simple Http Headers naturally fit better in a REST architecture. As consequence, I decided to extend the WCF channel stack to support a new a custom channel (Or custom binding) for transferring the workflow context as a http header.

The configuration of this binding WebHttpContextBinding is quite straightforward, I just created a new binding "WebHttpContextBinding" that can easily configured for workflow service,

<services>

  <service name="RestWorkflows.OrderWorkflow">

    <endpoint address="" behaviorConfiguration="MyServiceBehavior" binding="webHttpContext" bindingConfiguration="myServiceBinding" contract="RestWorkflows.IOrderService" />

  </service>

</services>

<bindings>

  <webHttpContext>

    <binding name="myServiceBinding"></binding>

  </webHttpContext>

</bindings>

<behaviors>

  <endpointBehaviors>

     <behavior name="MyServiceBehavior">

       <webHttp />

     </behavior>

  </endpointBehaviors>

</behaviors>

<extensions>

  <bindingExtensions>

    <add name="webHttpContext" type="Microsoft.ServiceModel.Samples.WebHttpContextBindingCollectionElement, WebHttpContext, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />

  </bindingExtensions>

</extensions>

The result of using this binding will be a new Http Header WscContext (encoded as Base64) being transmitted between the client and the service,

<MessageLogTraceRecord Time="2008-10-29T14:22:59.5258021-02:00" Source="TransportSend" Type="System.ServiceModel.Channels.BodyWriterMessage" xmlns="http://schemas.microsoft.com/2004/06/ServiceModel/Management/MessageTrace">
   <Addressing>
     <Action></Action>
   </Addressing>
   <HttpResponse>
      <StatusCode>Created</StatusCode>
      <WebHeaders>      <WscContext>77u/PENvbnRleHQgeG1sbnM9Imh0dHA6Ly9zY2hlbWFzLm1pY3Jvc29mdC5jb20vd3MvMjAwNi8wNS9jb250ZXh0Ij48UHJvcGVydHkgbmFtZT0iaW5zdGFuY2VJZCI+NDZkZGY3NTktMzdjNS00ZDZlLWI2ZTItNjVmMDNjMDVmYTAyPC9Qcm9wZXJ0eT48L0NvbnRleHQ+</WscContext>
       </WebHeaders>
    </HttpResponse>
    <order xmlns="http://starbucks.example.org" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
      <cost>6</cost>
      <drink>latte</drink>
      <next xmlns:a="http://example.org/state-machine">
         <a:next>

          <a:rel>http://starbucks.example.org/payment</a:rel>
           <a:type>application/xml</a:type>
           <a:uri>http://localhost:8000/payment/order/0b756654-161b-43d0-912f-4236552dc337</a:uri>
         </a:next>
         <a:next>
             <a:rel>http://starbucks.example/order/update</a:rel>
             <a:type>application/xml</a:type>
             <a:uri>http://localhost:8000/order/0b756654-161b-43d0-912f-4236552dc337</a:uri>
         </a:next>
       </next>
     </order>
</MessageLogTraceRecord>

<MessageLogTraceRecord Time="2008-10-29T14:22:59.5726021-02:00" Source="TransportReceive" Type="System.ServiceModel.Channels.BufferedMessage" xmlns="http://schemas.microsoft.com/2004/06/ServiceModel/Management/MessageTrace">
   <HttpRequest>
      <Method>PUT</Method>
       <QueryString></QueryString>
       <WebHeaders>
      <WscContext>77u/PENvbnRleHQgeG1sbnM9Imh0dHA6Ly9zY2hlbWFzLm1pY3Jvc29mdC5jb20vd3MvMjAwNi8wNS9jb250ZXh0Ij48UHJvcGVydHkgbmFtZT0iaW5zdGFuY2VJZCI+NDZkZGY3NTktMzdjNS00ZDZlLWI2ZTItNjVmMDNjMDVmYTAyPC9Qcm9wZXJ0eT48L0NvbnRleHQ+</WscContext>
        <Content-Length>566</Content-Length>
        <Content-Type>application/xml</Content-Type>
        <Expect>100-continue</Expect>
         <Host>localhost:8000</Host>
      </WebHeaders>
    </HttpRequest>
    <order xmlns="http://starbucks.example.org" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
          <cost>6</cost>
          <drink>cappuchino</drink>
          <next xmlns:a="http://example.org/state-machine">
            <a:next>
              <a:rel>http://starbucks.example.org/payment</a:rel>
              <a:type>application/xml</a:type>
              <a:uri>http://localhost:8000/payment/order/0b756654-161b-43d0-912f-4236552dc337</a:uri>
             </a:next>
             <a:next>
               <a:rel>http://starbucks.example/order/update</a:rel>
               <a:type>application/xml</a:type>
               <a:uri>http://localhost:8000/order/0b756654-161b-43d0-912f-4236552dc337</a:uri>
             </a:next>
           </next>
       </order>
</MessageLogTraceRecord>

The code for the custom binding is available to download from this location.

Filed under: , ,

Comments

# Routing to the right workflow service instance through URI templates (REST workflows part III)

Thursday, November 06, 2008 3:03 PM by Community Blogs

Continuing from my previous posts &quot; REST and Workflow services play well together I &quot; and &quot;

# My durable WCF RESTful calculator

Tuesday, November 25, 2008 2:29 PM by Community Blogs

A durable service in WCF is by a definition a service that can persist all its internal state across

Leave a Comment

(required) 
(required) 
(optional)
(required)