Partilhar via


Simplified Configuration for WCF Services

This sample demonstrates how to implement and configure a typical service and client using Windows Communication Foundation (WCF). This sample is the basis for all other basic technology samples.

This service, which exposes an endpoint for communicating with the service, uses the simplified configuration in .NET Framework version 4. Prior to .NET Framework 4, the endpoint is typically defined in a configuration file (Web.config), as shown in the following example configuration code.

<?xml version="1.0" encoding="utf-8" ?>
<!-- Copyright ©) Microsoft Corporation.  All Rights Reserved. -->
<configuration>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="CalculatorServiceBehavior">
          <serviceMetadata httpGetEnabled="True"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service name="Microsoft.Samples.GettingStarted.CalculatorService"
               behaviorConfiguration="CalculatorServiceBehavior">
        <endpoint address="" binding="basicHttpBinding" contract="ICalculator"/>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
    </services>
  </system.serviceModel>
</configuration>

In .NET Framework 4, the <service> element is optional. When a service does not define any endpoints, an endpoint for each base address and contract implemented are added to the service. The base address is appended to the contract name to determine the endpoint and the binding is determined by the address scheme. The following code example demonstrates a simplified configuration file. As configured, the service can be accessed at https://localhost/servicemodelsamples/service.svc by a client on the same computer. For clients on remote computers to access the service, a fully-qualified domain name must be specified instead of localhost. The service does not expose metadata by default. As such, the service turns on the ServiceMetadataBehavior behavior.

<?xml version="1.0" encoding="utf-8" ?>
<!-- Copyright © Microsoft Corporation.  All Rights Reserved. -->
<configuration>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceMetadata httpGetEnabled="True"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

To use this sample

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

  2. To build the solution, follow the instructions in Building the Windows Communication Foundation Samples.

  3. Run the sample by following these steps:

    1. Right click the Service project and select Set as StartUp project, then press Ctrl+F5.

    2. Wait for the console output confirming that the service is up and running.

    3. Right click the Client project and select Set as StartUp project, then press Ctrl+F5.

Ee530014.Important(en-us,VS.100).gif Note:
The samples may already be installed on your computer. Check for the following (default) directory before continuing.

<InstallDrive>:\WF_WCF_Samples

If this directory does not exist, go to Windows Communication Foundation (WCF) and Windows Workflow Foundation (WF) Samples for .NET Framework 4 to download all Windows Communication Foundation (WCF) and WF samples. This sample is located in the following directory.

<InstallDrive>:\WF_WCF_Samples\WCF\Basic\Services\ConfigSimplificationIn40

See Also

Concepts

Simplified Configuration

Other Resources

AppFabric Management Samples