共用方式為


COM+ Integration: Web Hosted

Download sample

This sample demonstrates how to expose an interface from a COM+ application as a Windows Communication Foundation (WCF) service and how to call this from a WCF client. This sample consists of a client console program (.exe) and an Enterprise Services library application (.dll) registered within COM+.

NoteNote:

The setup procedure and build instructions for this sample are located at the end of this topic.

The Enterprise Services application contains a single component that implements a single ICalculator interface that exposes math methods (Add, Subtract, Multiply, and Divide).

// Define the component's interface.
public interface ICalculator
{
    double Add(double n1, double n2);
    double Subtract(double n1, double n2);
    double Multiply(double n1, double n2);
    double Divide(double n1, double n2);
}

This interface is exposed as a service contract that defines a request-reply communication pattern. The client makes synchronous requests to a math operation and the service and underlying component replies with the result. Client activity is visible in the console window.

The service is hosted in-process by Internet Information Services (IIS) and is activated by the first message sent to the service. The ESCalculatorService class implementation calculates and returns the appropriate result.

// Supporting implementation for the ICalculator interface.
public class ESCalculatorService : ServicedComponent, ICalculator
{
    public double Add(double n1, double n2)
    {
        return n1 + n2;
    }
    public double Subtract(double n1, double n2)
    {
        return n1 - n2;
    }
    public double Multiply(double n1, double n2)
    {
        return n1 * n2;
    }
    public double Divide(double n1, double n2)
    {
        return n1 / n2;
    }
}
NoteNote:

The class does not include any ServiceModel-specific code, and is a typical Enterprise Services assembly that is attributed with Enterprise Services attributes, signed and added to the Global Assembly Cache (GAC).

The COM+ Service Model Configuration Tool (ComSvcConfig.exe) is used to add a supporting service for the selected interface. The following syntax is used to perform this:

ComSvcConfig.exe /install /application:ServiceModelSample /contract:ServiceModelSample.ESCalculator,ICalculator hosting:was /webDirectory:ServiceModelSamples /mex /verbose

In this case, the tool adds a service for the ICalculator interface of the ServiceModelSample.ESCalculator component, which is within the ServiceModelSample application. The service is hosted by IIS within the ServiceModelSample virtual directory. The added service exposes a single endpoint for communicating with the service. By default, the endpoint address for communicating with the service is constructed from the component's ProgID (that is, https://localhost/ServiceModelSamples/ServiceModelSample.ESCalculator.svc), though this service endpoint address can be modified by changing the name of the .svc service file. The tool adds the configuration for the service's binding to the Web.config file. The configuration for the service's binding defaults to a standard wsHttpBinding binding, which provides HTTP communications using SOAP 1.2 and Web services standards for addressing and security.

The tool also exposes a metadata exchange (MEX) endpoint at https://localhost/ServiceModelSamples/ServiceModelSample.ESCalculator.svc/mex, as well as the ability to access WSDL metadata from a browser through HTTP GET at https://localhost/ServiceModelSamples/ServiceModelSample.ESCalculator.svc?wsdl. These metadata features can be disabled by omitting the /mex tool option.

The service contract is derived directly from the ICalculator interface and is equivalent to the following ServiceContract definition.

[ServiceContract(Namespace="http://Microsoft.ServiceModel.Samples")]
public interface ICalculator
{
    [OperationContract]
    double Add(double n1, double n2);
    [OperationContract]
    double Subtract(double n1, double n2);
    [OperationContract]
    double Multiply(double n1, double n2);
    [OperationContract]
    double Divide(double n1, double n2);
}

The client communicates on a contract using a client that is generated by the Service Metadata Utility Tool (Svcutil.exe). The client is contained in the generatedClient.cs file. This utility retrieves metadata for a service and generates a client used to communicate on a contract type. The hosted service must be available to generate the proxy code, because it is used to retrieve the updated metadata. Run the following command from a command prompt in the client directory to generate the typed proxy:

svcutil.exe /n:http://Microsoft.ServiceModel.Samples,Microsoft.ServiceModel.Samples https://localhost/ServiceModelSamples/ServiceModelSample.ESCalculator.svc/mex /out:generatedClient.cs

Given the generated client for communicating on a given contract, the client can access a given service endpoint by configuring the appropriate address and binding. Like the service, the client uses a configuration file (App.config) to specify the endpoint it communicates with. The client endpoint configuration consists of a configuration name, an absolute address for the service endpoint, the binding, and the contract.

<system.serviceModel>
  <client>
    <endpoint
      address="https://localhost/ServiceModelSamples/servicemodelsample.escalculator.svc/ICalculator" 
      binding="wsHttpBinding" 
      bindingConfiguration="comNonTransactionalBinding" 
      contract="ICalculator" />
  </client>
  <bindings>
    <wsHttpBinding>
          <binding name="comNonTransactionalBinding" >
        <reliableSession enabled="true"/>
      </binding>
    </wsHttpBinding>
  </bindings>
</system.serviceModel>

The client implementation constructs an instance of the generated client. It can then be used to begin communicating with the service.

// Create a client.
CalculatorClient client = new CalculatorClient();

// Call the Add service operation.
double value1 = 100.00D;
double value2 = 15.99D;
double result = client.Add(value1, value2);
Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result);

// Call the Subtract service operation.
value1 = 145.00D;
value2 = 76.54D;
result = client.Subtract(value1, value2);
Console.WriteLine("Subtract({0},{1}) = {2}", value1, value2, result);

// Call the Multiply service operation.
value1 = 9.00D;
value2 = 81.25D;
result = client.Multiply(value1, value2);
Console.WriteLine("Multiply({0},{1}) = {2}", value1, value2, result);

// Call the Divide service operation.
value1 = 22.00D;
value2 = 7.00D;
result = client.Divide(value1, value2);
Console.WriteLine("Divide({0},{1}) = {2}", value1, value2, result);

//Closing the client gracefully closes the connection and cleans up resources.
client.Close();

When you run the sample, the operation requests and responses are displayed in the client console window. This demonstrates the use of the generated WCF service from a WCF client. Press ENTER in the client window to shut down the client.

Add(100,15.99) = 115.99
Subtract(145,76.54) = 68.46
Multiply(9,81.25) = 731.25
Divide(22,7) = 3.14285714285714

Press <ENTER> to terminate client.

To set up and build the sample

  1. Ensure that you have performed the One-Time Setup Procedure for the Windows Communication Foundation Samples.

  2. To build the C# or Visual Basic .NET edition of the solution, follow the instructions in Building the Windows Communication Foundation Samples.

  3. Ensure \InetPub\wwwroot\ServiceModelSamples and its bin subdirectory are empty.

  4. From a command prompt, navigate to the sample's service\bin folder.

  5. Enter gacutil.exe /i ESCalculator.dll to add the assembly to the Global Assembly Cache. Make sure Gacutil.exe is in your path.

  6. Enter regsvcs.exe ESCalculator.dll to register the assembly's component and the ServiceModelSample application with COM+. Make sure Regsvcs.exe is in your path.

  7. Enter ComSvcConfig.exe /install /application:ServiceModelSample /contract:"ServiceModelSample.ESCalculator,ICalculator" /hosting:was /webDirectory:ServiceModelSamples /mex /verbose to expose the interface as an IIS-hosted service.

    Make sure ComSvcConfig.exe is in your path.

To run the sample on the same machine

  1. Ensure that you can access the services using a browser by entering the following address: https://localhost/ServiceModelSamples/ServiceModelSample.ESCalculator.svc. A confirmation page should be displayed in response.

  2. Run Client.exe from \client\bin\, from under the language-specific folder. Client activity is displayed on the client console window.

  3. If the client and service are not able to communicate, see Troubleshooting Tips.

    NoteNote:

    The sample builds a console application client program. You must launch it using a command prompt to see its output.

To run the sample across machines

  1. On the service machine, create a virtual directory named ServiceModelSamples. The Setupvroot.bat script included with the Virtual Directory Setup Instructions can be used to create the disk directory and virtual directory.

  2. Copy the file ESCalculator.dll from the service\bin directory to a directory on the service machine.

  3. From a command prompt, navigate to that destination directory on the service machine.

  4. Enter gacutil.exe /i ESCalculator.dll on the service machine to add the assembly to the Global Assembly Cache.

  5. Enter regsvcs.exe ESCalculator.dll on the service machine to register the assembly's component and the ServiceModelSample application with COM+.

  6. Enter ComSvcConfig.exe /install /application:ServiceModelSample /contract:"ServiceModelSample.ESCalculator,ICalculator" /hosting:was /webDirectory:ServiceModelSamples /mex /verbose on the service machine to expose the contract as an IIS-hosted service.

  7. Copy the client program files from the \client\bin\ folder, under the language-specific folder, to the client machine.

  8. In the client configuration file, change the address value of the endpoint definition to match the new address of your service. Replace any references to "localhost" with a fully-qualified domain name in the address.

  9. Ensure that you can access the service from the client machine using a browser.

  10. On the client machine, launch Client.exe from a command prompt.

To clean up after the sample

  1. Enter ComSvcConfig.exe /u /application:ServiceModelSample /contract:ServiceModelSample.ESCalculator,ICalculator to uninstall the COM+ integration.

  2. Enter regsvcs.exe /u ESCalculator.dll to uninstall the application from COM+.

  3. Enter gacutil.exe /u ESCalculator to remove the component from the Global Assembly Cache.

Footer image

Send comments about this topic to Microsoft.
© Microsoft Corporation. All rights reserved.