Use "Go" for WCF Service Configuration Editor

Source: Internet
Author: User
Tags hosting

Original: http://www.cnblogs.com/Ming8006/p/3772221.html

Configuration through the WCF Service configuration Editor
Modify Client Side

Reference

In the previous article, we created a simple WCF program that was coded to define the endpoint additions and service behavior, but in the case of a true WCF application development, it was usually done directly by configuration.

For beginners, the configuration of WCF is overly complex, and it is not realistic to manually edit the configuration file directly. In this case, the WCF Service Configuration Editor tool can be used directly to generate the XML file for WCF configuration using the VS provisioning tool.

Configuration through the WCF Service Configuration Editor [1]

The following are the steps to use the WCF Service Configuration editor:

1. Open VS, right-click in the hosting project and create a new app. config file.

2. Click the menu bar Tools option and select WCF Service Configuration Editor from the drop-down menu.

3. Select "File->open->config File" in the tool window that pops up. Locate the app. config file that was just built and open it.

4. Create a new service, as shown, click on the "Create a new service" link before you find the WcfServices.Services.CalculatorService service in the service project.

5. Click Next to find the ICalculator contract in the contracts project.

6. Next, select the mode of communication for HTTP.

7. Click Next and select Basic Web Service Interoperability.

8. Click Next to enter the server endpoint address: Http://localhost:8080/calculatorservice. Next finish.

9. Add behavior (Behavior) for the service, which is important. In the advanced directory, right-click a new service behavior, new service Behavior Configuraton, and then rename the behavior to Calculatorbehavior. Create a new stack Element ' Servicemetadata ' and set its httpgetenabled to true. As shown in the following:

10. After this is done, we go back to the top service directory and add the calculatorbehavior behavior configuration just for the calculator service. As shown in the following:

11. Then configure the host's address, select Host, and then click New Base address at the bottom right, enter: Http://localhost:8080/calculatorservice

12. You can add a new server-side endpoint for configuration Ws-metadataexchange, and of course not. In the services directory of the endpoint right-click, create a new endpoint, name and address arbitrarily, to ensure that the binding is mexhttpbinding.

13.ctrl+s save, so that the app. Config file is automatically full, as follows:

1<?xml version="1.0"encoding="Utf-8"?>2<configuration>3<system.serviceModel>4<behaviors>5<serviceBehaviors>6<behavior name="Caclulaterbehavior">7<servicemetadata httpgetenabled="true"/>8</behavior>9</serviceBehaviors>Ten</behaviors> One<services> A<service behaviorconfiguration="Caclulaterbehavior"Name="WcfServices.Services.CalculatorService"> -<endpoint address="Http://localhost:8080/calculatorservice"binding="BasicHttpBinding" -bindingconfiguration=""contract="WcfServices.Contracts.ICalculator"/> the -<baseAddresses> -<add baseaddress="Http://localhost:8080/calculatorservice"/> -</baseAddresses> + -</service> +</services> A</system.serviceModel> at</configuration>
View Code

14. Modify the code of the hosting class as follows:

1 usingSystem;2 usingSystem.ServiceModel;3 usingwcfservices.services;4 5 namespacewcfservices.hosting6 {7     class Program8     {9         Static voidMain (string[] args)Ten         { One             using(ServiceHost host =NewServiceHost (typeof(CalculatorService))) A             { -Host. Opened + =Delegate -                 { theConsole.WriteLine ("Calculaorservice has started, press any key to stop it!"); -                 }; -   - host. Open (); + Console.read (); -             } +         } A     } at}
View Code

Note :

1 <endpoint address= "" binding= "BasicHttpBinding" name= "Calculator"   contract= "Contracts.icalculator" >2   

The address of the Endpiont is empty, only with the base address of the host. Of course, you can also directly with endpoint address, not the base address of the host. But what if the host has multiple services? With more endpoint hanging under the same host, then the base address is very important.

Modify Client Side

Return

Open the client's project (do not close the service side), select service Reference under the client project, right-click on your service namespace, and tap Update Service Reference. A new app. config file is generated.

Then rebuild the client-side project, the ClientApp code does not need to change.

Configuring the client [2]

In a real WCF application, most of the endpoints are defined in a configured way. For clients, we can also specify the three features of the endpoint with the following configuration, and specify an endpoint configuration name (CalculatorService) for the corresponding endpoint. Add the application Configuration file (app. config) to the client project as follows:

<?xml version="1.0"encoding="Utf-8"?><configuration> <system.serviceModel> <client> <endpoint address="Http://localhost:8080/calculatorservice"binding="BasicHttpBinding"contract="WcfServices.Contracts.ICalculator"Name="CalculatorService"/> </client> </system.serviceModel></configuration>

For the client project, we remove the service reference that we added, and add a reference to the contracts project for our client project. With this service contract, the service proxy object is created through channelfactory<icalculator> and the corresponding service invocation is made directly.

usingSystem;usingSystem.ServiceModel;usingwcfservices.contracts;namespacewcfservices.client{classProgram {Static voidMain (string[] args) {            using(Channelfactory<icalculator> ChannelFactory =NewChannelfactory<icalculator> ("CalculatorService") ) {ICalculator proxy=Channelfactory.createchannel (); using(Proxy asIDisposable) {Console.WriteLine ("x + y = {2} when x = {0} and y = {1}",1,2, Proxy. ADD (1,2));            } console.read (); }        }    }}
View Code

Source code: Simplewcfwithconfig.zip

Reference:

[1] Use of the WCF Service Configuration editor http://www.cnblogs.com/judastree/archive/2012/08/26/2657555.html

[2] My WCF Journey (1): Create a simple WCF program http://www.cnblogs.com/artech/archive/2007/06/14/782845.html

Use "Go" for WCF Service Configuration Editor

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.