Learning about WCF (2)-developing a WCF Service

Source: Internet
Author: User

In the previous article, I reviewed some basic knowledge about WCF. In this article, I will share with you how to develop a WCF Service that obtains and adds student information through examples.

Developing a WCF Service endpoint involves the following tasks::

Development Service Contract: Operation of the WCF service available to the specified endpoint.

Development binding: bind the protocol for the pointing endpoint to communicate with the outside world.

Add, delete, update, and configure endpoints: Add and bind endpoints in the configuration file (encoding is also supported, but not recommended .)

Add behavior: A behavior is a component that enhances the runtime behavior of services, endpoints, and operations.

Develop a WCF Service Contract

A WCF Service Contract is A. net interface or class modified with the metadata attribute [servicecontract. Each WCF Service can have one or more contracts, and each contract is a set of operations.

First, we define a. net interface: istuservicecontract, and define two methods to add and obtain student information respectively.

Void addstudent (student Stu); stucollection getstudent ();

Use the metadata attribute servicecontract of the WCF Service Model to mark the interface istuservicecontract and design the interface as a WCF contract. Use operationcontract to mark addstudent and getstudent

Getstudent () returns a collection of stucollection types. Addstudent () requires passing in the student object class as the parameter.

Code
Namespace wcfstudent
{
[Servicecontract]
Public interface istuservicecontract
{

[Operationcontract]
Void addstudent (student Stu );

[Operationcontract]
Stucollection getstudent ();

}

[Datacontract]
Public class student
{
Private string _ stuname;
Private string _ stusex;
Private string _ stuschool;

[Datamember]
Public String stuname
{
Get {return _ stuname ;}
Set {_ stuname = value ;}
}

[Datamember]
Public String stusex
{
Get {return _ stusex ;}
Set {_ stusex = value ;}
}

[Datamember]
Public String stuschool
{
Get {return _ stuschool ;}
Set {_ stuschool = value ;}
}
}

Public class stucollection: List <student>
{

}
}

The WCF Service exchanges soap information with the customer. The sender must serialize the data that the WCF Service interacts with the customer into XML and deserialize the XML at the receiver. Therefore, the student object passed to the addstudent operation must be serialized as XML before being sent to the server. By default, WCF uses the XML serializer datacontractserializer, which serializes and deserializes the data exchanged between the WCF Service and the customer.

As a developer, what we need to do is to use the metadata attribute datacontract to mark the types of data exchanged between WCF and its customers. Use the metadata attribute datamember to mark the attribute to be serialized in the exchange data type. (For details, refer to the above Code)

Implement the WCF Service Contract

It is as easy as writing a class in a WCF Service Contract. Here we create a class to process student first. Studentmanage

Code
Namespace wcfstudent
{
Public static class studentmanage
{
Private Static datatable td_stu;
Static studentmanage ()
{
Td_stu = new datatable ();
Td_stu.columns.add (New datacolumn ("name "));
Td_stu.columns.add (New datacolumn ("sex "));
Td_stu.columns.add (New datacolumn ("school "));
}

Public static void addstudent (string name, string sex, string School)
{
Datarow ROW = td_stu.newrow ();
Row ["name"] = Name;
Row ["sex"] = sex;
Row ["school"] = School;
Td_stu.rows.add (ROW );
}

Public static ienumerable getstudent ()
{
Return td_stu.defaultview;
}
}
}

Next, create a wcfstudenttext class to implement the istuservicecontract interface.

 

Code
Namespace wcfstudent
{
Public class wcfstudenttext: istuservicecontract
{
Public wcfstudenttext ()
{
//
// Todo: add the constructor logic here
//
}

Public void addstudent (student Stu)
{
Studentmanage. addstudent (STU. stuname, Stu. stusex, Stu. stuschool );
}

Public stucollection getstudent ()
{
Ienumerable list = studentmanage. getstudent ();
Stucollection stucollect = new stucollection ();
Student Stu;
Ienumerator iter = List. getenumerator (); // you can use the getenumerator method to reference the ienumerator object.

Bool first = true;
Propertydescriptorcollection PPS = NULL;
While (ITER. movenext () // use the ienumerator object to iterate the student information stored in the ienumerator set. Each propertydescriptor is a student information.
{
If (first)
{
First = false;
PPS = typedescriptor. getproperties (ITER. Current );
}

Stu = new student ();
Stu. stuname = (string) Pam ["name"]. getvalue (ITER. Current );
Stu. stusex = (string) PSP ["sex"]. getvalue (ITER. Current );
Stu. stuschool = (string) Pam ["school"]. getvalue (ITER. Current );
Stucollect. Add (Stu );
}

Return stucollect;
}
}
}

The ienumerator object is used to iterate the student information stored in the ienumerator set. Each propertydescriptor is a student information.

Resident WCF Service

Add an ADO. NET data service file wcfstudenttext. SVC and modify the file content as follows:

<% @ Servicehost service = "wcfstudent. wcfstudenttext" %>

The last thing we need to do is modify the Web. config file:

 

Code
<System. servicemodel>
<Services>
<Service name = "wcfstudent. wcfstudenttext" behaviorconfiguration = "servicebehavior">
<! -- Service endpoints -->
<Endpoint address = "" binding = "wshttpbinding" Contract = "wcfstudent. istuservicecontract">
<! --
During deployment, the following identification elements should be deleted or replaced to reflect
The identifier of the service to be deployed. After the deletion
Automatically derive the corresponding identifier.
-->
<Identity>
<DNS value = "localhost"/>
</Identity>
</Endpoint>
<Endpoint address = "mex" binding = "mexhttpbinding" Contract = "imetadataexchange"/>
</Service>
</Services>

 

Set the name of the WCF Service to wcfstudent. wcfstudenttext, and the service contract of the WCF Service endpoint to the Contract wcfstudent. istuservicecontract we wrote.

Of course, we can use vs2008 to directly create a WCF project, which will bring us a lot of convenience.

In this way, a WCF Service is complete.

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.