官术网_书友最值得收藏!

Configuring Default Endpoints

When programming with WCF, we will often need to create some simple WCF services for testing our ServiceContracts. These services often use very simple and typical endpoint and binding definitions. However, every time we need to set up such a service, we have to define the same endpoint and binding settings again and again, which really adds much duplicated work. Fortunately, WCF 4.0 introduces the Default Endpoint feature which saves us from defining common endpoint/binding settings repeatedly.

How to do it...

The steps for using a default endpoint are quite straightforward:

  1. Create a new Console Application project in Visual Studio 2010 targeting .NET Framework 4.0.
    How to do it...
  2. Add the ServiceContract in the service project and implementation types we need in the service project. We can use any valid ServiceContract and its corresponding implementation class here. For example, the following IHelloService service containing a single SayHello operation is used in our sample service application here:
    [ServiceContract]
        public interface IHelloService
        {
            [OperationContract]
            string SayHello(string user);
        }
  3. Add service configuration entries for the service type (defined in step 2) in the app.config file. We do not need to define any endpoint and binding configuration here. The following screenshot demonstrates the necessary service configuration elements:
    How to do it...
  4. Start a ServiceHost and specify the service type we want to run (see the following code):
    using (ServiceHost host = new ServiceHost(typeof(HelloService)))
    {
    host.Open();
    Console.ReadLine();
    }
    

How it works...

In the previous service definition and hosting code, we haven’t added any endpoint and binding configuration. The magic behind scene is the Default Endpoints feature. When we start a WCF 4.0 service host, if the runtime cannot find any endpoint defined (via app.config or code), it will automatically create a default endpoint for each ServiceContract implemented for the service class. The default endpoints will choose the proper binding based on its endpoint address (the URL scheme) by looking up a protocolMapping list predefined in the system configuration store (within the .NET 4 Machine.config.comments file). The following screenshot shows the protocolMapping list:

How it works...

For our previous example, since the endpoint address uses the HTTP scheme (derives from the baseAddress), the runtime will choose the BasicHttpBinding according to the protocolMapping list.

By using the DumpEndpoint function, we can confirm that Default Endpoint with BasicHttpBinding has been correctly set up in the previous case (refer to the next screenshot).

host.Open();
DumpEndpoint(host.Description.Endpoints);
…………….
}

private static void DumpEndpoint(ServiceEndpointCollection endpoints)
{
 foreach (ServiceEndpoint sep in endpoints)
 {
 Console.Write(“Address:{0}\nBinding:{1}\nContract:{2}\n”,sep.Address, sep.Binding.Name, sep.Contract);
 Console.WriteLine(“Binding Stack:”);
 foreach (BindingElement be in sep.Binding.CreateBindingElements())
 {
 Console.WriteLine(be.ToString());
 }
 }
}

The next screenshot shows the auto-configured default endpoint in the sample service.

How it works...

There’s more...

In addition to Default Endpoint, WCF 4.0 also provides the Default Binding feature which can save the life of developers who want to define a common binding setting for multiple endpoints. For example, we define the following anonymous binding configuration, which does not have an explicit name. Any endpoint that uses BasicHttpBinding will adopt the setting in this anonymous binding configuration.

There’s more...

See also

  • Complete source code for this recipe can be found in the \Chapter 2\recipe1\ folder
主站蜘蛛池模板: 二手房| 鄂托克前旗| 盐边县| 当阳市| 宜川县| 海兴县| 巩留县| 眉山市| 汝阳县| 务川| 正安县| 房产| 泸西县| 五莲县| 枣强县| 佛山市| 资溪县| 万荣县| 兴义市| 搜索| 宜阳县| 巧家县| 长宁县| 花莲县| 新余市| 伊宁市| 思茅市| 千阳县| 青龙| 伊金霍洛旗| 大新县| 台北市| 明光市| 宁南县| 和静县| 台东县| 江城| 靖边县| 涟水县| 义乌市| 武义县|