Step by step to learn about the WCF series-create and call a HelloWorld instance for the WCF Service through the configuration file (6)

Source: Internet
Author: User

 Reading directory

 I. Preface

Ii. Configuration

3. Generate proxy and configuration files through metadata

  I. Preface

  .This article compares it with the article "step by step learning about WCF-creating and calling a HelloWorld instance for a WCF Service, using encoding method (4, if you have any questions, refer to the link article.

  Ii. Configuration

    .In the Host project, some configurations are added for our Host end. This configuration has two aspects: one is about Service configuration, that is, the configuration of the Service loaded in our Host. Another aspect is the configuration of some specific Service behaviors in the Host.

At the Host end, let's look at the configuration file.

1: Service configuration

<Service/> node

  1.1: we added a <service/> in <services/>. The name of this service is the name of the service we implemented earlier. The format is name = "HelloWorldService. helloWorldService "is the name of our namespace + specific implementation service. BehaviorConfiguration = "serviceBehavior", indicating that its service behavior configuration adopts the serviceBehavior policy.

1 <service name="HelloWorldService.HelloWorldService" behaviorConfiguration="serviceBehavior">

1.2: we can see that two endpoints are exposed, both of which are bound to the Http protocol, but there are still differences. The above endpoints are bound using basic Http, the following endpoints are bound using mexHttp. This is a special method in WCF for metadata interaction. During metadata exchange, the built-in interface "IMetadataExchange" of WCF is specified. This interface is used to exchange metadata, and the specific Endpoint address "mex" is specified later ". The contract bound to the above "basicHttpBinding" endpoint is to learn about the WCF summary one step at a time-create and call a HelloWorld instance for the WCF Service by encoding (4) "HelloWorldService. IHelloWorldService

1 <endpoint address="HelloWorldService" binding="basicHttpBinding" contract="HelloWorldService.IHelloWorldService" />2 <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />

1.3: after the description of the <endpoint/> node is complete, it is the <Host/> node, and the service is running on the Host, the Host mainly describes the basic address "http: // localhost: 8000". Therefore, the actual address of the "HelloWorldService" service is "http: // localhost: 8000/HelloWorldService, so the address for metadata exchange is "http: // localhost: 8000/mex". Now the Service configuration is complete.

1 

2: Service Behavior Configuration

<Behaviors/>

In <behaviors/>, we can specify the specific service behavior. <serviceMetadata httpGetBinding = "true"/> indicates whether the service metadata can be obtained through the Http protocol, that is to say, after we apply this configuration option, we allow the Get method to operate the metadata.

1 <behaviors>2  <serviceBehaviors>3       <behavior name="serviceBehavior">4         <serviceMetadata httpGetBinding="true"/>5       </behavior>6     </serviceBehaviors>7 </behaviors>

3.: Generate proxy and configuration files through metadata

  .We save the configuration file on the Host side. Next we start the Host, because we have indicated in the configuration file that the Http service and port 8000 are used, therefore, in the browser, We can enter http: // localhost: 8000/To see the specific content related to this service. We can use the svcutil.exe tool and click http: // localhost: 8000 /? The WSDL shows the specific description information and generates the corresponding Proxyl code on the client. For this WSDL configuration file, we can see some of the information we specified, such information as what protocols we use for transmission is described in detail in WSDL, we have added the corresponding configuration file on the Host end so that the Host can not only provide external service 3 based on Tcp location at localhost port 9000, at the same time, the service is also provided based on the Http protocol location in localhost port 8000, so if we compare the two services, in fact, we can complete these tasks by writing code through the configuration file, this means that when we want to provide a service externally at the Host end, we do not need to write any code for this service, what we need to do is to complete the configuration of this service in the configuration file.
    .To demonstrate how to use the service provided under the Http protocol on the Client, we start the Host first, we enter the Client project, and add a reference in the Client, the address is http: // localhost: 8000/4, that is, the Host that provides Http-based messages, visual Studio automatically generates the corresponding proxy file and Configuration File 5 based on the content l in the WSDL. In Reference. there is a Reference under svcmap. cs, in this Reference. cs contains a proxy class automatically generated by the system. When we use it, we directly use the proxy class for service operations. The corresponding configuration files related to the Host and the configuration information and parameters are also generated on the Client. For these parameters, we can configure them as needed. Now we go to the Client. because the system has generated the Proxyl Code related to the IHelloWorldService contract for us, we do not need the manual code, then we do not need to call Proxyl through CreateChannel. We can directly use Proxy. helloWorldServiceContractClient proxy = new Proxy. helloWorldServiceContractClient (); instantiate a new Proxyl object by instantiating an object. Then, the instantiated proxyl object automatically contains contract-related information, after Proxyl is generated, I can call the corresponding method to complete the corresponding task, so that the distributed call based on Http is implemented. In WCF, both http-based message transmission and Tcp-based message transmission follow the same model, if we develop a complex system that uses the Tcp protocol for data transmission, but it needs to be changed to the Http protocol in the actual deployment process, then, we can modify some parameters through configuration to migrate the project from Tcp to Http.

  

  

Figure 1

  

Figure 2

  

 

Figure 3

Figure 4

  

Figure 5

  

Figure 6

1: first start the Host

2: Start the Client.

It does not work the same way as the coding method in the following article, "how to learn about WCF at a time-how to create and call a HelloWorld instance for the WCF Service (4 )".

  

 

 

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.