Configuration-Based Activation

This sample demonstrates how to activate Windows Communication Foundation (WCF) services without requiring a .svc file.

Dd807499.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\Hosting\ConfigBasedActivation

Sample Details

In this sample, the client is the WCF test client and the service is hosted in IIS.

Note

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

Activation of services without requiring a .svc file

In .NET Framework 3.5, a .svc file was required for activating a service. This caused additional management overhead, because an additional file was required to be deployed and maintained along with the application. With the release of .NET Framework version 4, the activation components can be configured using the application configuration file.

.NET Framework 4 introduces a new configuration element (ServiceActivationElement), in the ServiceHostingEnvironmentSection of the application configuration file. The ServiceHostingEnvironmentSection collection accepts a collection of services to activate, as shown in the following code example.

<serviceActivations>
   <add relativeAddress="Calculator.svc" service="Microsoft.ServiceModel.Samples.CalculatorService" />


<serviceActivations>

The observation to make is the configuration looks very similar to the configuration of .svc files. An additional attribute that is introduced is the relativeAddress that provides the address of the service. The relative address is also the virtual path for the service. The host retrieves the Web.config file of the file from the virtualPath location, if present; otherwise the host searches its parent folder recursively.

Note

This sample requires hosting in IIS to function.

To use this sample

  1. Using Visual Studio 2010, open the Service.csproj file.

  2. To build the solution, press CTRL+SHIFT+B.

  3. Test the service by running WCFTestClient.exe.

  4. Using Windows Explorer, navigate to the %SystemDrive%\Program Files\Microsoft Visual Studio 10.0\Common7\IDE folder.

  5. Run WcfTestClient.exe.

  6. Set the MEX address of the service.

  7. Press CTRL+SHIFT+A to set the service address.

  8. Set the address to https://localhost/ServiceModelSamples/Calculator.svc.

  9. Perform the Add operation. Set value on the n1 parameter to 10 and set value on the n2 parameter to 15.

  10. Press Invoke.

    The expected result is 25.

To set up, build, and run the sample

  1. Be sure 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. After the solution has been built, run Setup.bat to set up the ServiceModelSamples Application in IIS. The ServiceModelSamples directory should now appear as an IIS Application.

  4. To run the sample in a single- or cross-computer configuration, follow the instructions in Running the Windows Communication Foundation Samples.

See Also

Other Resources

AppFabric Hosting and Persistence Samples