WCF learning-building a simple WCF application (2), and building an application through wcf Learning
Let's go on to the http://www.cnblogs.com/songjianhui/p/7060698.html in the previous article.
I. The client adds a reference to call the service.
After the WCF application service is successfully hosted, the WCF Service application starts listening for service call requests. In addition, the service host publishes the service description in the form of metadata, and the corresponding client can obtain the metadata. Next, we will create a client program to call the service.
1. Run the service boarding program (hosting.exe) first)
2) In "Solution Explorer" of Visual Studio 2013, expand the Client project, left-click to select "Reference", and right-click the project. The menu is displayed, in the context menu, select "Add Service References )". For example.
3) a dialog box is displayed, as shown in. In the "Address" input box of the dialog box, enter the source address of the Service metadata release: http: // 127.0.0.1: 3721/calculatorService/metadata, and enter a namespace in the "namespace" input box, click "OK", for example ). Visual studio 2013 automatically generates a series of code and configuration for service calling.
4) A Class automatically generated in Visual Studio 2013 contains a service agreement interface, a service proxy object, and other related classes.
5) We can instantiate the CalculatorServiceClient object and execute the corresponding method to call the WCF Service operation. The code used by the client to call the WCF Service is as follows:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace Client 8 { 9 class Program10 {11 static void Main(string[] args)12 {13 MyWcfService.CalculatorServiceClient client = new MyWcfService.CalculatorServiceClient();14 Console.WriteLine("x+y={2} when x={0} and y={1}", 1, 2, client.Add(1, 2));15 Console.WriteLine("x-y={2} when x={0} and y={1}", 1, 2, client.Add(1, 2));16 Console.WriteLine("x*y={2} when x={0} and y={1}", 1, 2, client.Add(1, 2));17 Console.WriteLine("x/y={2} when x={0} and y={1}", 1, 2, client.Add(1, 2));18 Console.ReadLine();19 }20 }21 }
6) Result
2. The client calls the WCF Service through ChannelFactory <T>.
1) WCF uses a contract-based service call method. As shown in the preceding example, Visual Studio2013 creates a service contract interface equivalent to the server on the client during service reference addition. Because the server and client are in the same solution. Therefore, the server and client can reference the same contract.
2) To demonstrate this scenario, we remove the added Service reference and add a reference to the Service. Interface project for the Client project. Create a ChannelFactory <ICalculator> in the client program based on the address and bound object, then call the service proxy object created by the CreateChannel method to complete the service call (here we will create a console for demonstration)
3) create a console application to reference Service. Interface and System. ServiceModel;
4) Compile the ChanelClient code
1. Configure endpoints through code
1 using System; 2 using System. collections. generic; 3 using System. linq; 4 using System. text; 5 using System. threading. tasks; 6 using Service. interface; 7 using System. serviceModel; 8 9 namespace ChannelClient10 {11 class Program12 {13 static void Main (string [] args) 14 {15 // create a ChannelFactory <ICalculator> Based on the address and bound object through code 16 using (ChannelFactory <ICalculator> channelFactory = new ChannelFactory <ICalculator> (new WSHttpBinding ()," http://127.0.0.1:3721/calculatorService ") 17 {18 // create a service proxy Object 19 ICalculator proxy = channelFactory. createChannel (); 20 // call the calculator Method 21 Console. writeLine ("x + y = {2} when x = {0} and y = {1}", 1, 2, proxy. add (1, 2); 22 Console. writeLine ("x-y = {2} when x = {0} and y = {1}", 1, 2, proxy. add (1, 2); 23 Console. writeLine ("x * y = {2} when x = {0} and y = {1}", 1, 2, proxy. add (1, 2); 24 Console. writeLine ("x/y = {2} when x = {0} and y = {1}", 1, 2, proxy. add (1, 2); 25 26 Console. readLine (); 27} 28} 29} 30}
2. Configure the endpoint through the configuration file (configured in app. config)
<?xml version="1.0" encoding="utf-8" ?><configuration> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> <system.serviceModel> <client> <endpoint name="calculatorService" address="http://127.0.0.1:3721/CalculatorService" binding="wsHttpBinding" contract="Service.Interface.ICalculator"/> </client> </system.serviceModel></configuration>
The code in ChannelClient must be modified.
1 using System; 2 using System. collections. generic; 3 using System. linq; 4 using System. text; 5 using System. threading. tasks; 6 using Service. interface; 7 using System. serviceModel; 8 9 namespace ChannelClient10 {11 class Program12 {13 static void Main (string [] args) 14 {15 // create a ChannelFactory <ICalculator> Based on the address and bound object through code 16 // using (ChannelFactory <ICalculator> channelFactory = new ChannelFactory <ICalculator> (new WSHttpBinding ()," http://127.0.0.1:3721/CalculatorService ") 17 // {18 // create a service proxy Object 19 // ICalculator proxy = channelFactory. createChannel (); 20 // call the calculator Method 21 // Console. writeLine ("x + y = {2} when x = {0} and y = {1}", 1, 2, proxy. add (1, 2); 22 // Console. writeLine ("x-y = {2} when x = {0} and y = {1}", 1, 2, proxy. add (1, 2); 23 // Console. writeLine ("x * y = {2} when x = {0} and y = {1}", 1, 2, proxy. add (1, 2); 24 // Console. writeLine ("x/y = {2} when x = {0} and y = {1}", 1, 2, proxy. add (1, 2); 25 26 // Console. readLine (); 27 //} 28 29 // create a ChannelFactory <ICalculator> Based on the address and bound object through the configuration file 30 using (ChannelFactory <ICalculator> channelFactory = new ChannelFactory <ICalculator> ("calculatorService" )) 31 {32 // create a service proxy object 33 ICalculator proxy = channelFactory. createChannel (); 34 // call the calculator Method 35 Console. writeLine ("x + y = {2} when x = {0} and y = {1}", 1, 2, proxy. add (1, 2); 36 Console. writeLine ("x-y = {2} when x = {0} and y = {1}", 1, 2, proxy. add (1, 2); 37 Console. writeLine ("x * y = {2} when x = {0} and y = {1}", 1, 2, proxy. add (1, 2); 38 Console. writeLine ("x/y = {2} when x = {0} and y = {1}", 1, 2, proxy. add (1, 2); 39 40 Console. readLine (); 41} 42} 43} 44}
5) execute the hosting.exe Application
6) Run ChanelClient
Updating .................................
Need source code send mail to 1163598274@qq.com every Sunday unified send