Six steps for getting started with WCF Development

Source: Internet
Author: User
Here I use a simple scenario: the server provides an interface for the client to obtain customer information to read customer information and complete the six steps for getting started with WCF development.
1. Define the WCF Service Contract

A. Right-click the project reference node and choose system. servicemodel reference.

B. Add the following namespace references to the code file.

Using system. servicemodel;

Using system;

C. Create a new interface named icustomerservice and add a method named mermerinfomation to obtain the customer information, and return the customer information of the string type.

D. Add servicecontract attributes to the interface icustomerservice to make it an interface exposed in the WCF Service.

E. Add operationcontract attributes to the customerinfomation method so that it becomes a public member in the open interface of the WCF Service.

F. Code:

1 using system;
2
3 using system. servicemodel;
4
5 namespace conwcf
6
7 {[servicecontract (namespace = "http://microsoft.servicemodel.samples/")]
8
9 public interface customerservice
10
11 {
12
13 [operationcontract]
14
15 string customerinformation ();
16
17}
18
19}
20

 

 

2. Implement the WCF Service Contract
 

It is easy to implement the WCF service contract, that is, to implement the interface defined by the WCF Service Contract defined in the previous step. See the code below

1 using system;
2
3 using system. servicemodel;
4
5 namespace conwcf
6
7 {[servicecontract (namespace = "http://microsoft.servicemodel.samples/")]
8
9 public interface icustomerservice
10
11 {
12
13 [operationcontract]
14
15 string customerinformation ();
16
17}
18
19 public class customerservice: icustomerservice
20
21 {
22
23 # region icustomerservice Member
24
25 Public String customerinformation ()
26
27 {
28
29 return "this is the customer's information! ";
30
31}
32
33 # endregion
34
35}
36
37}
38
39

 

3. Start the WCF Service

A. Add an application configuration file named app. config.

B. Configure the basic address of the WCF Service as follows:

<Host>

<Baseaddresses>

<Addbaseaddress = "http: // localhost: 8000/conwcfr"/>

</Baseaddresses>

</Host>

C. Configure the port of the WCF Service. Address = "", which means to use the basic address configured above. You can also specify it here. Bingding = "wshttpbinding" indicates that the WCF Service uses the HTTP protocol. Then, configure the WCF Service Contract (namespace. Service Contract interface name), as shown below:

<Endpointaddress = ""

Binding = "wshttpbinding"

Contract = "conwcf. icustomerservice"/>

D. Configuration File

E. Simply start the server

Servicehost host = new servicehost (typeof (customerservice ));

Host. open ();

Console. writeline ("Customer Information Service started ");

Console. writeline ("press any key to end the service! ");

Console. Read ();

Host. Close ();

F. when the service is started, enter http: // localhost: 8000/conwcfr In the IE column, and you will receive some help messages.

G. exception: the service name in the configuration file must be: namespace. Implement the name of the WCF Service Contract class. Otherwise, an unconfigured exception will occur.

<Service

Name = "conwcf. customerservice"

Exception information: Service 'conwcf. customerservice 'has zero application (non-infrastructure) endpoints. this might be because no configuration file was found for your application, or because no service element matching the service name cocould be found in the configuration file, or because no endpoints were defined in the service element.

This exception caused me to become dizzy for a long time, causing me to think that troubleshooting errors from IIS and port to the configuration environment means I cannot figure out why it will be linked with the life name of the class. However, it was finally solved.

4. Create a basic WCF Client

After the WCF server is created, it is much easier to create a client. You can directly use the svcutil command line tool to generate code. I have installed the Windows SDK and installed it with the cmdshell command line tool. After opening it, you can run the svcutil command, which is run in the framework 3.0 or later environment. To view detailed help information, enter svcutil /?, Press enter.

1. Start the WCF server created in the previous steps.

2. Use CD in mongoshell to go to the directory where you want to store the client code, and enter the following command to generate the code and configuration file.

D: "client> svcutil/language: C #/out: customerclient. CS/config: App. config http :/

/Localhost: 8000/conwcfr

The above command specifies the language of the Code to be generated, the code file and the configuration file name, and the address of the WCF server. Make sure that the WCF server is running when running the command.

5. basic configuration of the WCF Client

The configuration of the WCF client is to configure the protocol for calling the WCF server, transmission bandwidth, service address, security, and other information. The configuration file automatically generated by the previous command is as follows.

1 <? XML version = "1.0" encoding = "UTF-8"?>
2 <configuration>
3 <system. servicemodel>
4 <bindings>
5 <wshttpbinding>
6 <binding name = "wshttpbinding_icustomerservice" closetimeout = "00:01:00"
7 opentimeout = "00:01:00" receivetimeout = "00:10:00" sendtimeout = "00:01:00"
8 bypassproxyonlocal = "false" transactionflow = "false" hostnamecomparisonmode = "strongwildcard"
9 maxbufferpoolsize = "524288" maxcompute edmessagesize = "65536"
10 messageencoding = "text" textencoding = "UTF-8" usedefaultwebproxy = "true"
11 allowcookies = "false">
12 <readerquotas maxdepth = "32" maxstringcontentlength = "8192" maxarraylength = "16384"
13 maxbytesperread = "4096" maxnametablecharcount = "16384"/>
14 <reliablesession ordered = "true" inactivitytimeout = "00:10:00"
15 enabled = "false"/>
16 <security mode = "message">
17 <transport clientcredentialtype = "Windows" proxycredentialtype = "NONE"
18 realm = ""/>
19 <message clientcredentialtype = "Windows" negotiateservicecredential = "true"
20 algorithmsuite = "default" establishsecuritycontext = "true"/>
21 </Security>
22 </binding>
23 </wshttpbinding>
24 </bindings>
25 <client>
26 <endpoint address = "http: // localhost: 8000/conwcfr" binding = "wshttpbinding"
27 bindingconfiguration = "wshttpbinding_icustomerservice" Contract = "icustomerservice"
28 name = "wshttpbinding_icustomerservice">
29 <identity>
30 <userprincipalname value = "30da1d0b1d1e4d2 \ Administrator"/>
31 </identity>
32 </Endpoint>
33 </client>
34 </system. servicemodel>
35 </configuration>

 

6. Use the WCF Client

In the client project, right-click the project reference node and choose system. servicemodel reference.
Add the client code file and Configuration File Created in section 4.
The client calls the service on the server. You only need to create an instance that generates the client class and call it. However, you need to confirm that the server is in the starting status, as shown below:
 

1 using system;
2
3 namespace conwcfcustomerclient
4
5 {
6
7 class Program
8
9 {
10
11 static void main (string [] ARGs)
12
13 {
14
15 customerserviceclient client = new customerserviceclient ();
16
17 string message = client. customerinformation ();
18
19 console. writeline (Message );
20
21 console. Read ();
22
23}
24
25}
26
27}
28 // from: http://blog.csdn.net/mych/archive/2008/05/15/2448809.aspx

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.