Reprint Source: http://www.cnblogs.com/subin1990/archive/2013/03/06/2946170.html
2013-03-06 15:22 by Jinjinledao, 1353 reading, 0 reviews, Favorites, compilation
This is one of the methods that I used in my project to call the WCF service, which is now recorded to deepen the memory. Add the Project "Simplewcf.contract" (as an example of the number of N-cubic computations), which is written in the contract for the WCF service.
In this project, add an interface that contains a method for n-th-square computation.
1 [ServiceContract]
2 public interface Isquareservice
3 {
4 #region Get a number of n-th square value
5 //<summary> 6//Description: Gets the n-th value of a number 7// </summary>
8 //<param Name= "Dbvalue" > number of calculations to be performed </param> 9// <param name= "Intnum" > several Parties </param>
10 /// <returns>string type calculation result
</returns> [OperationContract] Getsquarevalue (double dbvalue, int intnum); #endregion }
2. Add the Project "Simplewcf.implement", here is the implementation of the WCF service.
In this project, add a class that inherits the interfaces in "Simplewcf.contract".
1 [ServiceBehavior] 2 class SquareService:SimpleWCF.Contract.ISquareService 3 {4 #region gets a number of n-th square Value 5//<summary> 6///Description: Gets the n-th value of a number 7//</summary> 8//<param name = "Dbvalue" > number to be calculated by </param> 9//<param name= "Intnum" > several Parties </param>//<retur Calculation result of ns>string type </returns> one public String getsquarevalue (double dbvalue, int intnum) 12 {13 if (Intnum < 0), the second-party calculation of the "return" will not be less than 0.
"; + + Else if (Intnum = = 0) {1 "; 20} 21 else if (Intnum = = 1): {dbvalue.tostring return (); 24} 25 Else the {double dbresultvalue = 1; +-(int i = 0; i < in Tnum; i++)-{-DBResultvalue *= Dbvalue;
Dbresultvalue.tostring return (); 38} PNS #endregion}
3. Add the Project "Simplewcf.service", which is used to unify the management of WCF services.
Right-click the project and add a WCF service named "Squareservice.svc". Delete the automatically generated interface and code implementation classes.
Right-click the service, select View markup, and modify the service item's value to "SimpleWCF.Implement.SquareService".
After the modification is complete, you can right-click the service and select "View in Browser" to view the service.
4. Add the Silverlight project "Simplewcf.client" to automatically generate a Web project "SimpleWCF.Client.Web" that hosts the project.
In "Simplewcf.client", add the service reference.
Click the "Discover" button to find the WCF services available on your system. Modify the namespace to "Squareservice", which you can invoke in a Silverlight program.
1//<summary> 2///Description: Click the Calculate button to get the results 3//</summary> 4//<param Nam E= "Sender" ></param> 5//<param name= "E" ></param> 6 private void Btncalculate_clic K (object sender, RoutedEventArgs e) 7 {8 Squareservice.squareserviceclient clientsquare = new Squar
Eservice.squareserviceclient (); 9 clientsquare.getsquarevaluecompleted + = new EVENTHANDLER<SQUARESERVICE.GETSQUAREVALUECOMPLETEDEVENTARGS&G
t; (clientsquare_getsquarevaluecompleted); Ten Clientsquare.getsquarevalueasync (double. Parse (this.txtNumber.Text), Int.
Parse (This.txtN.Text)); //<summary> 15///Description: Display the calculation results in the text box below//</summary> 17 <param name= "Sender" ></param>//<param name= "E" ></param> void clients Quare_getsquarevaluecompleted (object sender, Squareservice.getsquarevaluecomplEtedeventargs e) {this.txtResultValue.Text = "calculation result:" + E.result; 22}
The result is the following figure: