A journey to WCF-the first WCF example (2) and the wcf example

Source: Internet
Author: User

A journey to WCF-the first WCF example (2) and the wcf example
Step 4: Boarding services through self-boarding

The WCF Service depends on a running process (host). The service host specifies a host for the service. WCF is a message-based communication framework that uses an Endpoint-based communication method.

The endpoint consists of Address, Binding, and Contract ,. Since the three elements should be the first letter ABC, there is an easy-to-remember formula: Endpoint = ABC. An end contains all the information required for communication. For example.

Address ):An address that indicates the position of the endpoint. The address determines the service location and solves the service addressing problem.

  • Binding ):How to bind a client to an endpoint for communication. Binding implements all the details of communication, including network transmission, message encoding, and other processing of messages to implement certain functions (such as secure, reliable transmission, transactions, etc. WCF has a series of system definition bindings, such as BasicHttpBinding, WsHttpBinding, and NetTcpBinding,
  • Contract ):A protocol that identifies available operations. A protocol is an abstraction of service operations and a definition of the message exchange mode and message structure.
  • Behavior (Behavior ):A group of local implementation details of a specified endpoint.
  • The purpose of service hosting is to enable a process to provide a running environment for the WCF Service Application. You can add one or more endpoints to a service to expose them to potential service callers. The service caller finally calls the service through a matched endpoint.

    1) code-based boarding

    We can use code to complete all service boarding work. In the Main method of the Program. cs file in the Hosting project, use code to realize the boarding implementation of the BookService WCF Service Application. The Code is as follows:

    Using SCF. wcfService; using System. collections. generic; using System. linq; using System. serviceModel; using System. serviceModel. description; using System. text; using System. threading. tasks; namespace Hosting {class Program {// the startup Program without a configuration file static void Main (string [] args) {using (ServiceHost host = new ServiceHost (typeof (BookService ))) {host. addServiceEndpoint (typeof (IBookService), new WSHttpBinding (),
    "Http: // 127.0.0.1: 8888/BookService"); if (host. description. behaviors. find <ServiceMetadataBehavior> () = null) {ServiceMetadataBehavior behavior = new ServiceMetadataBehavior (); behavior. httpGetEnabled = true; behavior. httpGetUrl = new Uri ("http: // 127.0.0.1: 8888/BookService/metadata"); host. description. behaviors. add (behavior);} host. opened + = delegate {Console. writeLine ("BookService, press any key to terminate the service! ") ;}; Host. Open (); Console. Read ();}}}

     

    The WCF Service host is completed through a special object: ServiceHost. In the preceding code, a ServieHost object is created based on the type of the WCF Service Application (typeof (BookService) and an endpoint is added. The specific address is http: // 127.0.0.1: 8888/BookService. WSHttpBinding is used and the IBookService type of the service agreement is specified.

    Loose coupling is a basic feature of SOA. The loose coupling between the client and the server in the WCF Service Application is reflected in the fact that the client only needs to understand the basic description of the WCF Service, rather than the specific implementation details, you can call the WCF Service normally. The description of the WCF Service is published in the form of Metadata (Metadata. The publishing of metadata in WCF is implemented through ServiceMetadataBehavior, a special service behavior. In the service boarding Code provided above, we added ServiceMetadataBehavior for the created ServiceHost, and adopted the metadata acquisition method based on HTTP-GET. The publishing address of metadata is specified through the HttpGetUrl of ServiceMetadataBehavior. After the ServiceHost Open method is called to successfully host the service, we can obtain metadata related to the service through this address.

     

    3) run the generated hosting.exe and type http: // 127.0.0.1: 8888/BookService/metadata in the address bar of the browser. You will get the service metadata in the form of WSDL, as shown in.

     

     Get the metadata of a WCF Service in HTTP-GET

    Ii) Implementing boarding through configuration files

    In actual applications, the boarding of the WCF Application Service generally does not add endpoints and define service behavior directly by coding, but is implemented by writing the configuration file, this can be easily modified.

    1) Now I add an app. config configuration file to the Hosting project and add the following configuration information to the configuration file app. config.

     <?xml version="1.0" encoding="utf-8"?><configuration>  <system.serviceModel>    <behaviors>      <serviceBehaviors>        <behavior name="metadataBehavior">          <serviceMetadata httpGetEnabled="true" httpGetUrl="http://127.0.0.1:8888/BookService/metadata" />        </behavior>      </serviceBehaviors>    </behaviors>     <services>      <service  behaviorConfiguration="metadataBehavior" name="SCF.WcfService.BookService">        <endpoint address="http://127.0.0.1:8888/BookService"            binding="wsHttpBinding" bindingConfiguration="" contract="SCF.WcfService.IBookService" />      </service>    </services>  </system.serviceModel>  <startup>    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />  </startup></configuration>

    2) add an app. after the config configuration file and configuration information, the original boarding Code cannot be used. You need to modify the service boarding code and the code will become more concise, you only need a few lines of code. The Code is as follows.

    Static void Main (string [] args) {try {using (ServiceHost host = new ServiceHost (typeof (BookService) {host. Opened + = delegate {
    Console. WriteLine ("BookService, use the configuration file and press any key to terminate the service! ") ;}; Host. Open (); Console. Read () ;}} catch (Exception ex) {Console. WriteLine (ex. Message );}}

    3) execute the hosting.exe application. The result is shown in.

    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.