Introduction

In KrishG’s previous post, he talked about the various ways, in BizTalk RFID 2009,  you can create EPCIS data and then send it to a EPCIS Capture service using the EPCIS Capture Client. But, once you have implemented your application, how do you test it?

Well, here I will try to walk through the process of creating a  EPCIS Capture Service in .NET to test your applications.

Pre-requisites

  • EPCIS XSD definitions. You can get them from http://www.epcglobalinc.org/standards/epcis
  • .NET Framework 3.5 (Has better support for implementing REST services/clients).
  • Microsoft BizTalk RFID 2009 (for testing the Capture Service implemented).

Generating the C# Classes of EPCIS XSD Types

As the first step, from the XSD files provided by EPCGlobal, lets generate C# classes to work with in code. To do this, we will use the .NET framework tool xsd.exe.

Unzip the EPCIS XSDs into a folder and on the Visual Studio Command Prompt type the following command:

xsd.exe EPCglobal-epcis-1_0.xsd EPCglobal.xsd StandardBusinessDocumentHeader.xsd /c /f /order /n:SampleCaptureService.EpcisTypes

This will create a CS file(for me it was named epcglobal-epcis-1_0_epcglobal_standardbusinessdocumentheader.cs). Lets rename this file to a more friendly name, like EpcisTypes.cs.

Creating the Capture Service Project

Open Visual Studio and create a new WCF Service Library Project. Add the EpcisTypes.cs file we created above, into the project. Delete the sample Service that comes from the WCF Service Library Template. (Named IService1.cs and Service1.cs).

EPCIS Capture Service is a XML over HTTP service (REST). WCF supports defining ‘RESTful’ service in .NET Framework 3.5 with System.ServiceModel.Web.dll assembly. Thus, we will add a reference to this assembly to our project.

Implementing the EPCIS Capture Service

Now lets define the Capture service interface that the Capture Service will implement.

 
[ServiceContract, XmlSerializerFormat]
public interface IEpcisCaptureService
{
    [OperationContract]
    [WebInvoke(Method = "POST", UriTemplate = "Capture", RequestFormat = WebMessageFormat.Xml)]
    void Capture(EPCISDocumentType document);
}

By applying the ServiceContract  and XmlSerializerFormat attribute to our interface, we are simply telling WCF that this interface defines a service and it should use XML Serializer to serialize/deserialize messages for this service respectively.

The WebInvoke attribute on the Capture method, is to specify to WCF that this method will be invoked by the Web programming model (REST) and Capture is the method to be used to respond to HTTP POST requests. This is as per the the definition of the Capture operation by EPCIS standard.

Now lets implement this WCF Service Interface.

 
public class EpcisCaptureService : IEpcisCaptureService
{
    #region IEpcisCaptureService Members

    public void Capture(EPCISDocumentType document)
    {
        // Your Capture Code Here
    
    }
    #endregion
}

Configuring the Capture Service

The last thing we have to do is write the App.Config

Open the App.config and write the following configuration.

 
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <services>
      <service name="SampleEpcisCaptureService.EpcisCaptureService">
        <host>
          <baseAddresses>
            <add baseAddress = "http://localhost:8731/SampleEpcisCaptureService/" />
          </baseAddresses>
        </host>
        <!-- Service Endpoints -->
        <!-- Unless fully qualified, address is relative to base address supplied above -->
        <endpoint address ="soap" binding="wsHttpBinding" contract="SampleEpcisCaptureService.IEpcisCaptureService"/>
        <endpoint address ="rest" binding="webHttpBinding" contract="SampleEpcisCaptureService.IEpcisCaptureService" behaviorConfiguration="webhttpbehaviour"/>      
      </service>
    </services>
    <behaviors>
      <endpointBehaviors>
        <behavior name="webhttpbehaviour">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

Now here we have defined two endpoints for our service. One is the standard WCF’s WsHttpBinding based endpoint. While the other is a WebHttpBinding binding at http://localhost:8731/SampleEpcisCaptureService/rest with a additional WebHttpBehavior added to enpointBehaviors. WebHttpBinding is a new binding introduced in .NET 3.5 to support REST services.  Please note that for the WebHttpBinding to work, specifying the WebHttp behavior is necessary.

Running the Service

To run this service, create a Console Application project that refers to your WCF Service Library you just created above. In this console application, create a ServiceHost for our service, copy the configuration into the app’s App.Config and then build and run the project

Testing our Service

For testing our service, we will use BizTalk RFID EPCIS Capture Client. For this, create a Test Application project, add a reference to Microsoft.Rfid.Epcis.dll and copy the following code into Main() and run the app.

 
// Creating a dummy ObjectEvent
ObjectEvent oe = new ObjectEvent(DateTime.Now, new [] { new Uri("http://abc")}, EpcisAction.Observe);

// Creating a REST client to http://localhost:8731/SampleEpcisCaptureService/rest
CaptureClient client = CaptureClient.GetRestClient("http://localhost:8731/SampleEpcisCaptureService/rest");

// Executing the Capture method with the dummy ObjectEvent
client.Capture(new [] {oe});

Alternatively, You can also use the Sample that KrishG wrote to test your service.

In my next post, we will take a closer look at the EPCIS Object Model as defined by Microsoft BizTalk RFID 2009.

You can download a complete working sample below. The attached sample capture service writes the EPCIS events received by it to a SQL database in XML format.