[WCF] Use Visual Studio to configure and publish service metadata

Source: Internet
Author: User

In WCF, A client applicationProgramTo use a deployed WCF Service, you must know a few pieces of data about the service, called the metadata of the service. These metadata includes: 1) Service Contract metadata; 2) service access address; 3) service access mode configuration. In the previous articleArticleIn the operation example, I use Direct CopyCodeThe metadata of the service contract is disclosed to the client application, and the access address is written in the client application code by hard encoding. The access method of the Service uses basichttpbindng in the standard binding, it is also determined by hard encoding in the customer application.

To sum up, in the previous operation example, the reason why I published service metadata is that I developed both service and customer applications. However, in most real system development scenarios, service developers and client application developers are different, with different teams and even different technologies used in development. Today, I am talking about how to use the add service reference function in Visual Studio to obtain service metadata, the customer's application development teams use Visual Studio very efficiently. First, write a service contract. Create a new solution in Visual Studio 2008, and select the WCF Service library for the first project. Name it wcfstudy8. After creating a project, delete the built-in app. config, iservice. CS, and service. CS files. Add the interface code file icalculationservice. CS with the following content:

Code

Using System;   Using System. servicemodel;   Namespace Wcfstudy8 { [Servicecontract (namespace = " Http://www.WCFStudy8.net/2009/08/06 " )]   Public Interface Icalculationservice { [Operationcontract]   Int Sum ( Params Int [] Numbers ); } } After a service contract is created, add the service that implements the contract. The class code file calculationservice. CS has the following content: Code

Using System; Namespace Wcfstudy8 { Public Class Calculationservice: icalculationservice { # Region Icalculationservice members Public int sum (Params int [] numbers) { Int result = 0; Foreach (int n in numbers) { Result + = N; } Return result; } # Endregion } } Create a new project to store the WCF Service. For simplicity, select the Windows console program project and name it host. (The details of storing services using complex applications will be described later) write the following code in the program. CS file of the host project: Code

Using System;   Using System. servicemodel;   Namespace Host { Class Program { Static Void Main ( String [] ARGs) { Using (Servicehost host = New Servicehost ( Typeof (Wcfstudy8.calculationservice )))   { Host. open (); Console. writeline ( " Host is on the run, press any key to close it. " ); Console. Readline (); } } } }

Then, add an application configuration file app. config for the host project. In this file, I will configure the service deployment data and use the WCF configration configuration software to edit the configuration. The value is that this configuration software is useful for learning how to configure WCF. However, the reason is completely manually entered into the configuration file (as long as you are familiar with the WCF configuration later ). Therefore, I am not going to detail the use of this software. To show how to start the software.

This software can also be used in.. NET Framework sdks folder. net 3.0 is installed together, and the location on my computer is: C: \ Program Files \ microsoft sdks \ windows \ v6.0a \ bin \ svcconfigeditor.exe to the app. config open the software, enter the interface as follows, select create a new service ..., you will be guided to create a service: After you select create a new service, You must select the service contract for the new service implementation, click the browser button, find the assembly where icalculationservice is located, and select: after the service contract is selected, the software requires you to select the protocol type used by the Service. Here I select the HTTP protocol. Of course, this Protocol is only a protocol used by the public endpoint, do not assume that this protocol represents a service. The Protocol only corresponds to the endpoint, not the service. In addition, I select the basic HTTP protocol: After the endpoint protocol is selected, the software requires you to enter an address for the endpoint, it gives Your default text is "http: //". If you want to add an address to this end, the endpoint address will be an absolute address. The good habit is to set the base address for the service and let the endpoint use the relative address. In this way, you only need to modify the base address of the service when modifying the domain name, instead of modifying the endpoint address one by one. Set the endpoint address to "calculation": After the endpoint address is set, the process for creating a service is complete, but this does not mean that the service deployment is complete. Next, enter the service deployment configuration. In the same way, when the software was used at the beginning, the endpoint created during the service creation process only had the address and no name, I used the software to give it a name, but it is the same as the address (of course, this is not necessary), and the setting of the endpoint name will also be reflected in the metadata of the entire service. Now, I need to add Server Behavior In the future, you can understand what is a server behavior as a feature unique to this service. Select the Node path on the software interface: Advanced-> service Behaviors And then click New Service Behavior configration... On the editing page, I set the name of the newly added server behavior to metabehavior and add Servicemetadata Element: After configuring the server behavior, click the servicemetadata element of the newly added behavior and set the httpgetenabled attribute to true. This attribute can be used to access this element through HTTP connections. The last step is to configure the service endpoint for obtaining metadata. The endpoint for obtaining metadata is the endpoint used by Visual Studio to add service reference. Through this endpoint, the customer application developer can obtain all metadata related to the service. This includes the three service metadata that I mentioned at the beginning of the article. The reason why this endpoint can provide metadata to the customer is that the server behavior I just set is more precisely because of the servicemetadata element. So far, readers only need to know about Chengdu. Select node services> endpoints, and right-click Create endpoint. The endpoint name is changed to Mex, the address is also set to Mex, and the public service contract is imetadataexchange, which is defined in system. servicemodel. in the DLL assembly, I also selected to bind this endpoint to mexhttpbinding. All settings, such as: Now, the last configuration is to set its server behavior for this service, obviously, this behavior is what I just added: Well, now we have configured all the service deployment configurations. It seems very troublesome and time-consuming. In fact, as long as you are skilled, you can configure it all at once. Save the configuration, and return to the Visual Studio 2008 page. A message is displayed, indicating whether the file is modified externally. Select Yes to all. Now we have configured the service. Now let's take a look at the changes in the app. config file. The following is the content:

Code

<? XML version = "1.0" encoding = "UTF-8" ?>   < Configuration >   < System. servicemodel >   < Behaviors >   < Servicebehaviors >   < Behavior Name = "Metabehavior" >   < Servicemetadata Httpgetenabled = "True" />   </ Behavior >   </ Servicebehaviors >   </ Behaviors >   < Services >   < Service Name = "Wcfstudy8.calculationservice" > < Endpoint Address = "Calculation" Binding = "Basichttpbinding" Bindingconfiguration = "" Name = "Calculation" Contract = "Wcfstudy8.icalculationservice" /> < Endpoint Address = "Mex" Binding = "Mexhttpbinding" Bindingconfiguration = "" Name = "Mex" Contract = "Imetadataexchange" /> </ Service > </ Services > </ System. servicemodel > </ Configuration > The content in this configuration file will be loaded when the service host process starts and used to initialize the service, that is, through the configuration file, developers do not need to set each attribute of the service host in the program code (of course, you can also set it ). The attributes in the configuration file will be described later. It can be seen that the configuration in the software just now has been reflected. Currently, you also need to obtain metadata from the customer application. I will continue this content in the next article.
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.