Integrating WCF and Enterprise Services


Windows Communication Foundation (WCF) is a new communication technology that is part of .NET Framework 3.0. WCF is covered in detail in Chapter 40, “Windows Communication Foundation.”

WCF Service Façade

Adding a WCF façade to an Enterprise Services application allows using WCF clients to access the serviced components. Instead of using the DCOM protocol, with WCF you can have different protocols such as HTTP with SOAP or TCP with a binary formatting.

The utility ComSvcConfig.exe allows adding a WCF service to Enterprise Services applications. When creating a WCF façade with this utility, you have to specify several options:

  • Application - Here, you have to specify the name of the Enterprise Services application that shows up in the Component Services Explorer and is defined with the attribute [ApplicationName] or the application id that is defined with the attribute [ApplicationId].

  • Contract - With the contract option, you specify the contracts of the serviced components that should be available for WCF. To create a WCF façade, the contract is specified by using the name of the component, the interfaces, and optional the methods that should be offered. If all components should be available, you can specify a *. The preferred option is specifying the component and interface names, for example, Wrox.ProCSharp.EnterpriseServices .OrderControl,IOrderControl. The name that must be specified is the ProgId of the component that is by default the name of the class, including the namespace and the interfaces without the namespace. You can find the names in the Component Services Explorer.

Tip 

You can read about ProgId and COM interop in Chapter 23, “COM Interoperability.”

  • Hosting - With the hosting option, you can specify in which process the WCF service should run. Possible values for this option are complus and was. With complus the WCF service is configured to run inside the process of the Enterprise Services application. This option is only possible if the application is configured as a server application: [ApplicationActivation(ActivationOption.Server)]. was specifies that Windows Activation Services, the successor of Internet Information Services, should be used to host the WCF service façade. Here, you also have to specify the options /Website and /Webdirectory to define the location.

  • Mex - By specifying the option /mex, a metadata-exchange endpoint is created, so that the client programmer can easily access the contract.

Tip 

You can find the utility ComSvcConfig.exe in this directory: <windows>\Microsoft.NET\ Framework\v3.0\Windows Communication Foundation.

Now you can create a WCF façade to the application created previously. Before creating the façade, the assembly must be installed in the GAC, and using the complus hosting option, the application must be configured as a server application. The façade is created with this command line:

 ComSvcConfig /install /application:"Wrox.NorthwindDemo"    /contract:Wrox.ProCSharp.EnterpriseServices.OrderControl,IOrderControl    /hosting:complus /mex /verbose 

And this is the result received. Of course, you will get a different GUID.

 Microsoft (R) COM+ Service Model Integration Configuration Tool [Microsoft (R) Windows (R) Communication Foundation, Version 3.0.4506.25] Copyright (c) Microsoft Corporation. All rights reserved. Generated endpoints for interface Wrox.ProCSharp.EnterpriseServices.OrderControl, IOrderControl. Generated Mex endpoint for the service. Updated file: C:\Program Files\ComPlus Applications\{}\application.config Warning: COM+ application Wrox.NorthwindDemo must be restarted for configuration changes to take effect.

In the directory that is listed with the output from the ComSvcConfig utility, you can find the following configuration file application.configuration. This configuration file lists the behaviors and endpoints for WCF.

Tip 

In Chapter 40 you can read the details about WCF configuration.

The <comContracts> element contains the mapping to the serviced component. The GUID specified with the contract attribute is the interface ID (IID) of the interface IOrderControl. The methods that are exposed for WCF are defined within the <exposedMethods> element.

The <service> element specifies the exposed WCF service with the endpoint configuration. The binding is set to netNamedPipeBinding with a comTransactionalBinding configuration, so transactions can flow from the caller to the serviced component. With other network and client requirements, you can specify a different binding, but this is all covered in Chapter 40.

 <?xml version="1.0" encoding="utf-8"?> <configuration>    <system.serviceModel>       <behaviors>          <serviceBehaviors>             <behavior name="ComServiceMexBehavior">                <serviceMetadata httpGetEnabled="false" />                <serviceDebug includeExceptionDetailInFaults="false" />             </behavior>          </serviceBehaviors>       </behaviors>       <bindings>          <netNamedPipeBinding>             <binding name="comNonTransactionalBinding" />             <binding name="comTransactionalBinding" transactionFlow="true" />          </netNamedPipeBinding>       </bindings>       <comContracts>          <comContract contract="{}"             name="IOrderControl"             namespace="http://tempuri.org/"             requiresSession="true">             <exposedMethods>                <add exposedMethod="NewOrder" />             </exposedMethods>          </comContract>       </comContracts>       <services>          <service behaviorConfiguration="ComServiceMexBehavior"             name="{},                   {}">             <endpoint address="IOrderControl" binding="netNamedPipeBinding"                 bindingConfiguration="comTransactionalBinding"                 contract="{}" />             <endpoint address="mex" binding="mexNamedPipeBinding"                 bindingConfiguration=""                 contract="IMetadataExchange" />             <host>                <baseAddresses>                   <add baseAddress=                     "net.pipe://localhost/Wrox.NorthwindDemo/                      Wrox.ProCSharp.EnterpriseServices.OrderControl" />                </baseAddresses>             </host>          </service>       </services>    </system.serviceModel> </configuration>

Client Application

Create a new console application named WCFClientApp. Because the service offers a MEX endpoint, you can add a service reference from Visual Studio by selecting the menu Project image from book Add Service Reference/... (see Figure 38-10).

image from book
Figure 38-10

Tip 

To add a service reference from Visual Studio, you must have the .NET Framework 3.0 extensions for Visual Studio installed.

With the service reference a proxy class is created, the assemblies System.ServiceModel and System .Runtime.Serialization are referenced, and an application configuration file referencing the service is added to the client application.

Now, you can use the generated entity classes and the proxy class OrderControlClient to send an order request to the serviced component:

  static void Main() {    Order order = new Order();    order.customerId = "PICCO";    order.orderDate = DateTime.Today;    order.shipAddress = "Georg Pipps";    order.shipCity = "Salzburg";    order.shipCountry = "Austria";    OrderLine line1 = new OrderLine();    line1.productId = 16;    line1.unitPrice = 17.45F;    line1.quantity = 2;    OrderLine line2 = new OrderLine();    line2.productId = 67;    line2.unitPrice = 14;    line2.quantity = 1;    OrderLine[] orderLines = { line1, line2 };    order.orderLines = orderLines;    OrderControlClient occ = new OrderControlClient();    occ.NewOrder(order); } 




Professional C# 2005 with .NET 3.0
Professional C# 2005 with .NET 3.0
ISBN: 470124725
EAN: N/A
Year: 2007
Pages: 427

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net