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
http://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
Ensure that you have performed the One-Time Setup Procedure for the Windows Communication Foundation Samples.
To build the solution, follow the instructions in Building the Windows Communication Foundation Samples.
Run the sample by following these steps:
Right click the Service project and select Set as StartUp project, then press Ctrl+F5.
Wait for the console output confirming that the service is up and running.
Right click the Client project and select Set as StartUp project, then press Ctrl+F5.
The samples may already be installed on your computer. Check for the following (default) directory before continuing.
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.
We'd love to hear your thoughts. Choose the type you'd like to provide:
Our feedback system is built on GitHub Issues. Read more on our blog.