Handling Output

MSRS allows you to format service state using an Extensible Stylesheet Language Transformations (XSLT) file. This gives the service developer control over how the state data is displayed. An XSLT file is used to transform an XML document or, in this case, the XML returned from a service. The result is a formatted HTML, XML, or text document. For more information about XSLT specification, refer to the following URL: http://www.w3.org/TR/xslt.

To transform XML, you will need to create an XSLT file. You can do this using Visual Studio by right-clicking the project in Solution Explorer and clicking Add and New Item. To format as HTML, the XSLT file will need to contain an output element that includes a method attribute set with a value of "html." For example, the following XSLT file can be used to format the single state item from a service named DssService1:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:svc1="http://schemas.tempuri.org/2007/07/dssservice1.html">

  <xsl:output method="html"/>

  <xsl:template match="/svc1:DssService1State">
    <html>
      <head>
        <title>DSS Service 1</title>
        <link rel="stylesheet" type="text/css"
      href="/resources/dss/Microsoft.Dss.Runtime.Home.Styles.Common.css" />
      </head>
      <body style="margin:10px">
        <h1>DSS Service 1 - XSLT Example</h1>
        <table border="1">
          <tr class="odd">
            <th colspan="2">Service State formatted with XSLT</th>
          </tr>
          <tr class="even">
            <th>Output Message:</th>
            <td>
              <xsl:value-of select="svc1:OutputMsg"/>
            </td>
          </tr>
         </table>
      </body>
    </html>
  </xsl:template>

</xsl:stylesheet>

The stylesheet requires a reference to the service contract (http://schemas.tempuri.org/2007/07/dssservice1.html). If you do not know the URI for the contract, you can open the manifest.xml file for the service and retrieve it from the dssp:Contract element. This reference is bound to an alias, svc1, that is used throughout the stylesheet.

When the stylesheet is complete, you will need to add it as an embedded resource of the service assembly. This is done by right-clicking the XSLT file in Solutions Explorer and changing the Build Action to Embedded Resource. You will also need to add code such as the following:

[EmbeddedResource("Robotics.DssService1.TransformOutput.xslt")]
string _transform = null;

At this point you can reference it from the HttpGet message handler. You have to use the HttpGet operation because this returns XML and not a SOAP message. Code such as the following can be used to reference the newly embedded resource: httpGet.ResponsePort.Post(new HttpResponseType(System.Net.HttpStatusCode.OK, _state, _transform));

After compiling the service, the output displayed in an Internet browser should appear as formatted HTML (see Figure 2-7).

Screenshot of the state from DssService1 formatted with an XSLT file.

Figure 2-7. Screenshot of the state from DssService1 formatted with an XSLT file.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.13.184