- Microsoft Windows Communication Foundation 4.0 Cookbook for Developing SOA Applications
- Steven Cheng
- 525字
- 2021-04-13 17:04:03
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.
The steps for using a default endpoint are quite straightforward:
- Create a new Console Application project in Visual Studio 2010 targeting .NET Framework 4.0.
- Add the
ServiceContract
in the service project and implementation types we need in the service project. We can use any validServiceContract
and its corresponding implementation class here. For example, the followingIHelloService
service containing a singleSayHello
operation is used in our sample service application here:[ServiceContract] public interface IHelloService { [OperationContract] string SayHello(string user); }
- 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: - Start a
ServiceHost
and specify the service type we want to run (see the following code):
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:

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.

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.

- PS是這樣玩的:輕松掌握 Photoshop 通關秘籍
- After Effects CC影視后期制作實戰從入門到精通
- SketchUp印象 城市規劃項目實踐(第2版)
- RESTful PHP Web Services
- Instant MuseScore
- iPhone Applications Tune/Up
- 新編AutoCAD 2016從入門到精通
- Salesforce CRM: The Definitive Admin Handbook
- Unity 3D\2D手機游戲開發:從學習到產品(第4版)
- Photoshop網店美工實例教程(第2版 全彩微課版)
- Elasticsearch數據搜索與分析實戰
- AutoCAD 2022中文版從入門到精通(標準版)
- Instant Markdown
- 深入淺出WebAssembly
- 剪映專業版:短視頻創作案例教程(全彩慕課版)