RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Building Declarative WCF Services in .NET 4.0 : Page 2

Building WCF services declaratively in XAML lets you integrate services with workflows and provides flexibility that identical services implemented in code cannot.


Implementing Service Logic Declaratively

With the contract definitions and projections in place, the next step is to actually implement the service logic in XAML. To do that, you need to encapsulate the details of the implementation in a <Service.Implementation> element. The following structure shows the <Service> element children required to define a service contract:

<Service  ….>
  <SoapContractProjection x:Name="IHelloWorldSoapProjection">
   <!-- service contract definition goes here -->
  <Service.Implementation >
<!-- service implementation goes here -->
<!-- service end points go here -->

Table 1 shows the elements you need to declare WCF services in XAML.

Table 1. WCF Declarative Elements: The table lists and provides a brief explanation of the elements used to express a WCF service declaratively in XAML.

Element Description
<Service> Acts as a root element that contains all the details of the service including the endpoint, service implementation, and so on.
<Service.KnownProjections> Lets you specify the contract projections exposed through a service
<Service.Implementation> Holds implementation logic for a declarative service
<WorkflowServiceImplementation> Specifies the name of the service implementation that contains the workflow implementation
<WorkflowServiceImplementation.Body> Contains the body of the actual workflow implementation
<Service.Endpoints> Specifies the service end point; contains an <Endpoint> element for each end point the service exposes
<ServiceContract> Specifies details of the service, including the name of the service interface
<OperationContract> Specifies details of the operation, such as the name of the operation
<OperationArgument> Specifies the names and direction of arguments supplied to the operation.
<DynamicActivityAction> Lets you define variables and associate them with a workflow that implements the operation

Listing 1 contains the complete service definition for a service interface named IHelloWorld, which exposes a method named HelloWorld().

Note that Listing 1 contains the actual service implementation inside the <WorkflowServiceImplementation.Body> element. That element contains the <ServiceOperation.Body> element, which uses the <DynamicActivityAction> element to specify the input and output variables to the workflow operation:

  <Variable x:Name="op1" x:TypeArguments="p:String" />                
  <Variable x:Name="HelloWorldResult" 
   x:TypeArguments="p:String" />

You assign the input variable as the output from the HelloWorld method through the <wma:Assign> element:

<wma:Assign x:TypeArguments="p:String" To="out 
   [HelloWorldResult]" Value="op1" /> 

Then you define the service endpoint using the <Service.EndPoints> element, in which you specify the end point URI as well as the type of the binding (BasicHttpBinding in this case):

<Endpoint Uri="http://localhost:8080/HelloWorld">

After you have the end point defined, you can specify the contract projections that will be made available for the IHelloWorld service through the <EndPoint.ContractProjection> element:

       <ServiceContract x:Name="IHelloWorld">
        <OperationContract Name="HelloWorld" 
         <OperationArgument Name="op1" Type="p:String"/>                
         <OperationArgument Name="HelloWorldResult" 
         Type="p:String" Direction="Out"/>

That’s all there is to exposing a WCF service through XAML declaration. Although at first the XAML looks a little intimidating, you'll find that it becomes very straightforward after you've created a few of these.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date