(Some of the content is from the Internet and learned from the materials found on the Internet. below, this article is suitable for beginners)
Source code download: http://download.csdn.net/detail/li0531/5172849
Tool: vs2008
[Server]
1. Create a project
Overall project demonstration:
1 is an interface class (default)
2. Implementation Interface Class (default)
3. configuration file (default)
Open iservice1 and add the following code:
Using system; using system. collections. generic; using system. LINQ; using system. runtime. serialization; using system. servicemodel; using system. text; namespace wcfserver {// source code download: http://download.csdn.net/detail/li0531/5172849 // Note: If you change the interface name here "iservice1", you must also update the app. references to "iservice1" in config. // The service contract refers to the interface or class [servicecontract] public interface iservice1 {// [operationcontract] // string getdata (INT value ); // [operationcontract] // compositetype getdatausingdatacontract (compositetype composite); // task: add service operation [operationcontract]/* ticket adding method */void addticket (INT count ); [operationcontract]/* method for purchasing tickets */INT buytickets (INT num ); [operationcontract] // service contract is the implementation method for providing services/* method for querying tickets */INT getremainingnum ();} // use the data protocol described in the following example to add a composite type to the service operation [datacontract] public class compositetype {// bool boolvalue = true; // string stringvalue = "hello "; // [datamember] // public bool boolvalue // {// get {return boolvalue;} // set {boolvalue = value ;} /// // [datamember] // Public String stringvalue // {// get {return stringvalue;} // set {stringvalue = value ;} //} // Add bool boolcount = true; // judge whether there is still a ticket int how133 = 10; // how many tickets are there [datamember]/* determine if there are still tickets */Public bool boolcalue {get {return boolcount;} set {If (howmany> 0) {boolcount = false ;} else {boolcount = true ;}} [datamember]/* Number of returned votes */Public int howmany {get {return howmany ;}} set {howmany = value ;}}}}
Service1:
Using system; using system. collections. generic; using system. LINQ; using system. runtime. serialization; using system. servicemodel; using system. text; namespace wcfserver {// Note: If you change the class name "iservice1", you must update the app. references to "iservice1" in config. Public class service1: iservice1 {// initialization class compositetype T = new compositetype (); // Public String getdata (INT value) // {// return string. format ("You entered: {0}", value); //} // public compositetype getdatausingdatacontract (compositetype composite) // {// If (composite. boolvalue) // {// composite. stringvalue + = "suffix"; //} // return composite; //} # region iservice1 Member/* method for adding votes */Public void addticket (INT count) {T. howtasks = T. howmany + count;}/* method for purchasing tickets */Public int buytickets (INT num) {If (T. boolcalue) {T. howtasks = T. howmany-num; return 1 ;}else {return 0 ;}/ * method for returning votes */Public int getremainingnum () {return T. howion ;}# endregion }}
The app. config file does not need to be moved because our class can directly add the default code to the default class.
Add a project to the solution to enable the Service.
Wcftest
The interface is as follows:
Form code:
Using system; using system. collections. generic; using system. componentmodel; using system. data; using system. drawing; using system. LINQ; using system. text; using system. windows. forms; using system. servicemodel; namespace wcftest {public partial class form1: FORM {public form1 () {initializecomponent () ;}// defines servicehost host = NULL; private void btnopen_click (Object sender, eventargs e) {int Flag =-1; try {// error message // 0 open exception // 1 close exception // enable service if (btnopen. TEXT = "enable service") {flag = 0; btnopen. TEXT = "Disable service"; // wcfserver. service1 is the service host = new servicehost (typeof (wcfserver. service1); // start the service host. open (); this. labmsg. TEXT = "service started";} else {flag = 1; btnopen. TEXT = "enable service"; if (host. state! = Communicationstate. closed) // determine whether the service is disabled {Host. close (); // close the service} This. labmsg. TEXT = "Service Disabled" ;}} catch (exception ex) {Switch (FLAG) {Case 0: MessageBox. show ("enable service exception:" + ex. tostring (); break; Case 1: MessageBox. show ("service disconnection exception:" + ex. tostring (); break; default: break ;}}}}}
Add the app. config file
<? XML version = "1.0" encoding = "UTF-8"?> <Configuration> <system. servicemodel> <services> <! -- Add a service --> <service name = "wcfserver. service1" behaviorconfiguration = "calculatorservicebehavior"> <! -- Name must be the same as the host instance initialization service in the Code as behaviorconfiguration behavior configuration -->
[Client]
Add test project wcftext
Add a web reference as shown in Figure
Add a page (of course, you can organize the page beautifully. Here is just an example)
The Code practice is as follows:
Using system; using system. collections. generic; using system. componentmodel; using system. data; using system. drawing; using system. LINQ; using system. text; using system. windows. forms; namespace wcftext {public partial class form1: FORM {public form1 () {initializecomponent () ;}// initialization method servicereference1.service1client tclient = new wcftext. servicereference1.service1client (); // purchase a ticket private void btnshappi Ng_click (Object sender, eventargs e) {int num = convert. toint32 (txtnums. value. tostring (); If (tclient. getremainingnum () <= 0) {This. labmsg. TEXT = "no ticket"; return;} If (tclient. getremainingnum () <num) {This. labmsg. TEXT = "insufficient ticket, remaining" + tclient. getremainingnum (). tostring () + "Zhang"; return;} int I = tclient. buytickets (Num); // call the method if (I = 1) {This. labmsg. TEXT = "purchased" + num. tostring () + "Zhang succeeded! ";} This. labmsg. text + = "remaining tickets" + tclient. getremainingnum (). tostring () + "Zhang";} // query the fare private void btngetpj_click (Object sender, eventargs e) {This. labmsg. TEXT = ""; this. labmsg. TEXT = tclient. getremainingnum (). tostring () ;}// add ticket private void btnadd_click (Object sender, eventargs e) {tclient. addticket (convert. toint32 (txtnum. value. tostring (); this. labmsg. TEXT = "add" + txtnum. value. tostring () + "successful tickets" + "remaining tickets" + tclient. getremainingnum (). tostring ();} private void form1_load (Object sender, eventargs e) {labsf. TEXT = "Origin Site: Jinan d110 ()"; labzd. TEXT = "terminal: juye d110 ()"; txtnums. value = 1; txtnum. value = 1 ;}}}
:
Code ends here ----
Good luck!