Complete example of WCF Service Development and calling

Source: Internet
Author: User

Complete example of WCF Service Development and calling

Development Tool: vs2008

Development language: C #

Development content: simple permission management system

Step 1: Create a WCF Service Library

Click "OK" to create a WCF Service library sample program and automatically generate two class files: iservice1.cs and service1.cs. We can directly modify and develop our services, but delete them directly.

Step 2: Develop entity classes

In the solution, add the new class module. CS

In the class, you must first introduce the using system. runtime. serialization namespace.

The code of the object class is as follows:

Namespace wcfservicelib. model {// <summary> /// module entity /// </Summary> [datacontract] class module {[datamember] Public String moduleno; [datamember] Public String modulename ;}}

To serialize a section when a WCF Service is called, add the [datacontract] label before the object class, and add the [datamember] label before each member of the object class.

Step 3: Create a WCF Service Interface

To provide external services for a WCF Service, you must create an interface for the service and declare the content of the external service.

Add the new interface class imoduleservice. CS in the solution, and then introduce the namespace using system. servicemodel;

The Code is as follows:

Using system; using system. Collections. Generic; using system. LINQ; using system. Text; using system. servicemodel; using wcfservicelib. model;

Namespace wcfservicelib. iservice {// <summary> // Interface Class // </Summary> [servicecontract] public interface imoduleservice {[operationcontract] void addmodules (module book );

[Operationcontract] module getmodulebyid (string ID );

[Operationcontract] void removemodule (string ID );

[Operationcontract] void moduleupdate (module book );}}

The [servicecontract] label must be added before the interface class, and the [operationcontract] label must be added before each member. The label is used to ensure that the method can be accessed by external calls.

Step 4: interface class implementation

Implement the interface class declared in step 3. Only in this way will the WCF Service be providing services.

Add the interface implementation class moduleservice to the solution, and introduce the namespace using system. servicemodel;

The Code is as follows:

Using system; using system. Collections. Generic; using system. LINQ; using system. Text; using wcfservicelib. iservice; using system. servicemodel; using wcfservicelib. model;

Namespace wcfservicelib. service {// <summary> /// interface class implementation /// </Summary> [servicebehavior (instancecontextmode = instancecontextmode. single)] class moduleservice: imoduleservice {# region imoduleservice member list <module> _ modules = new list <module> ();

Public void addmodules (module M) {M. moduleno = guid. newguid (). tostring (); _ modules. Add (m );}

Public module getmodulebyid (string ID) {module M = _ modules. Find (P => P. moduleno = ID); Return m ;}

Public void removemodule (string ID) {module M = _ modules. Find (P => P. moduleno = ID); _ modules. Remove (m );}

Public void moduleupdate (module) {module M = _ modules. Find (P => P. moduleno = module. moduleno); M. modulename = module. modulename ;}

# Endregion }}

So far, the subject of the WCF Service has been developed. How can this service be used by callers? We need to register and publish the WCF Service.

Step 5: release of the WCF Service

Tools for visual registration and release of the WCF Service are provided in Versions later than vs2008.

The following page is displayed:

There are two endpoints: the first is the service node, and the second is the metadata node. Because we created it using the service library project that comes with vs, the configuration file has not deleted this information, so the first node still retains its own service information iservice. next we will change this service to the service information we wrote above.

Click "services"-"services. service1 "in the name on the right, the" service type Browser "dialog box is displayed. In this type, find the DLL file compiled by this WCF Service Project, double-click the service to display the published service in the service. Click it and click OK.

Then, expand "services"-> "wcfservicelib. service. moduleservice-> endpoints, click the first "empty name". From the contract in "emptyproperties" on the right, we can see that the contract still uses services. iservice1. Follow the steps above and find the DLL.

Close the Configuration window and save the settings.

So far, the service has been developed and released. Next we can use it after hosting the WCF. There are three methods for hosting a WCF Service. The most common method is IIS hosting. Let's take a look at how the WCF Service is hosted by IIS.

Step 6: release of the WCF Service IIS

1. Right-click the solution to add a website and select the WCF Service as the type.

2. The newly created WCF Service site automatically generates two class files for us in the app_code file: iservice. CS and service. CS. These two files are useless for us. We delete them.

3. Add a reference to the WCF Service library project. The result is as follows:

4. Modify the service. SVC file with the following code:

<% @ Servicehost Language = "C #" DEBUG = "true" service = "wcfservicelib. Service. moduleservice" %>

5. Right-click Web. config and select "Edit WCF configuration ".

Close the WCF editing tool and save the settings.

6. Right-click Service. SVC and select View in browser to run the service in IE.

7. Deploy the site in IIS and create a virtual directory to point to the site. The page is displayed as follows.

At this point, the WCF Service is successfully hosted in IIS. Next, let's take a look at how to use the WCF Service.

Step 7: Use of the WCF Service

1. Create an Asp.net Application

2. Right-click "Reference" ---> Add service reference

After the solution is successfully added, the files in the solution are as follows:

3. the following code calls the WCF Service in the background:

Protected void page_load (Object sender, eventargs e) {If (! Ispostback) {servicereference1.imoduleserviceclient c = new modules (); C. addmodules (New wcf_cilent.servicereference1.module ());

If (C. getallmodule (). length> 0) {response. write (C. getallmodule () [0]. moduleno. tostring (); response. end () ;}4. Run the program. The following call result is displayed. The call is successful.

 

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.