msdn上有篇很好的wcf入门例子。很适合初学者。一步一步教你如何创建wcf程序。
这篇文章其实也就是把msdn上的代码组装了下。
首先创建服务器端程序。代码如下:
接口:ICalculator,定义了加减乘除四个方法add,subtract,multiply,divide。
创建客户端:添加服务引用
这篇文章其实也就是把msdn上的代码组装了下。
首先创建服务器端程序。代码如下:
接口:ICalculator,定义了加减乘除四个方法add,subtract,multiply,divide。
Code
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.ServiceModel;
6
7namespace Microsoft.ServiceModel.Samples
8{
9 [ServiceContractAttribute]
10 interface ICalculator
11 {
12 [OperationContractAttribute]
13 double Add(double a,double b);
14 [OperationContractAttribute]
15 double Subtract(double a, double b);
16 [OperationContractAttribute]
17 double Multiply(double a, double b);
18 [OperationContractAttribute]
19 double Divide(double a, double b);
20
21 }
22}
23
接口实现类:CalculatorService1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.ServiceModel;
6
7namespace Microsoft.ServiceModel.Samples
8{
9 [ServiceContractAttribute]
10 interface ICalculator
11 {
12 [OperationContractAttribute]
13 double Add(double a,double b);
14 [OperationContractAttribute]
15 double Subtract(double a, double b);
16 [OperationContractAttribute]
17 double Multiply(double a, double b);
18 [OperationContractAttribute]
19 double Divide(double a, double b);
20
21 }
22}
23
Code
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5
6namespace Microsoft.ServiceModel.Samples
7{
8 class CalculatorService:ICalculator
9 {
10
11 ICalculator 成员#region ICalculator 成员
12
13 public double Add(double a, double b)
14 {
15 double result = a + b;
16 Console.WriteLine("Received Add({0},{1})",a,b);
17 Console.WriteLine("Return: {0}",result);
18 return result;
19 }
20
21 public double Subtract(double a, double b)
22 {
23 double result = a - b;
24 Console.WriteLine("Received Subtract({0},{1})",a,b);
25 Console.WriteLine("Return :{0}",result);
26 return result;
27 }
28
29 public double Multiply(double a, double b)
30 {
31 double result = a * b;
32 Console.WriteLine("Received Multiply({0},{1})",a,b);
33 Console.WriteLine("Return :{0}",result);
34 return result;
35 }
36
37 public double Divide(double a, double b)
38 {
39 double result = a / b;
40 Console.WriteLine("Received Divice({0},{1})",a,b);
41 Console.WriteLine("Return :{0}",result);
42 return result;
43 }
44
45 #endregion
46 }
47}
48
主程序代码:1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5
6namespace Microsoft.ServiceModel.Samples
7{
8 class CalculatorService:ICalculator
9 {
10
11 ICalculator 成员#region ICalculator 成员
12
13 public double Add(double a, double b)
14 {
15 double result = a + b;
16 Console.WriteLine("Received Add({0},{1})",a,b);
17 Console.WriteLine("Return: {0}",result);
18 return result;
19 }
20
21 public double Subtract(double a, double b)
22 {
23 double result = a - b;
24 Console.WriteLine("Received Subtract({0},{1})",a,b);
25 Console.WriteLine("Return :{0}",result);
26 return result;
27 }
28
29 public double Multiply(double a, double b)
30 {
31 double result = a * b;
32 Console.WriteLine("Received Multiply({0},{1})",a,b);
33 Console.WriteLine("Return :{0}",result);
34 return result;
35 }
36
37 public double Divide(double a, double b)
38 {
39 double result = a / b;
40 Console.WriteLine("Received Divice({0},{1})",a,b);
41 Console.WriteLine("Return :{0}",result);
42 return result;
43 }
44
45 #endregion
46 }
47}
48
Code
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.ServiceModel;
6using System.ServiceModel.Description;
7
8namespace Microsoft.ServiceModel.Samples
9{
10 class Program
11 {
12 static void Main(string[] args)
13 {
14 Uri baseAdderess = new Uri("http://localhost:8000/ServicesModelSamples/Service");
15 ServiceHost selfHost = new ServiceHost(typeof(CalculatorService), baseAdderess);
16 selfHost.AddServiceEndpoint(typeof(ICalculator), new WSHttpBinding(), "CalculatorService");
17 ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
18 smb.HttpGetEnabled = true;
19 selfHost.Description.Behaviors.Add(smb);
20 selfHost.Open();
21 Console.WriteLine("The service is ready");
22 Console.WriteLine("Please press <Enter> to terminate service ");
23 Console.ReadLine();
24
25 }
26 }
27}
28
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.ServiceModel;
6using System.ServiceModel.Description;
7
8namespace Microsoft.ServiceModel.Samples
9{
10 class Program
11 {
12 static void Main(string[] args)
13 {
14 Uri baseAdderess = new Uri("http://localhost:8000/ServicesModelSamples/Service");
15 ServiceHost selfHost = new ServiceHost(typeof(CalculatorService), baseAdderess);
16 selfHost.AddServiceEndpoint(typeof(ICalculator), new WSHttpBinding(), "CalculatorService");
17 ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
18 smb.HttpGetEnabled = true;
19 selfHost.Description.Behaviors.Add(smb);
20 selfHost.Open();
21 Console.WriteLine("The service is ready");
22 Console.WriteLine("Please press <Enter> to terminate service ");
23 Console.ReadLine();
24
25 }
26 }
27}
28
创建客户端:添加服务引用
Code
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.ServiceModel;
6using Client.ServiceCalculator;
7
8namespace Client
9{
10 class Program
11 {
12 static void Main(string[] args)
13 {
14 EndpointAddress epAddress = new EndpointAddress("http://localhost:8000/ServiceModelSamples/Service/CalculatorService");
15 CalculatorClient client = new CalculatorClient();
16 //Add
17 double a = 100.00d;
18 double b = 15.99d;
19 double result = client.Add(a, b);
20 Console.WriteLine("Add:{0} + {1} = {2}",a,b,result);
21 //Substract
22 a = 100.00d;
23 b = 15.99d;
24 result = client.Subtract(a, b);
25 Console.WriteLine("Sub:{0} + {1} = {2}",a,b,result);
26 //Multiply
27 a = 9.00d;
28 b = 81.25d;
29 result = client.Multiply(a, b);
30 Console.WriteLine("Mul:{0} + {1} = {2}",a,b,result);
31 //Divide
32 a = 100.00d;
33 b = 15.99d;
34 result = client.Divide(a, b);
35 Console.WriteLine("Div:{0} + {1} = {2}",a,b,result);
36
37 }
38 }
39}
40
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.ServiceModel;
6using Client.ServiceCalculator;
7
8namespace Client
9{
10 class Program
11 {
12 static void Main(string[] args)
13 {
14 EndpointAddress epAddress = new EndpointAddress("http://localhost:8000/ServiceModelSamples/Service/CalculatorService");
15 CalculatorClient client = new CalculatorClient();
16 //Add
17 double a = 100.00d;
18 double b = 15.99d;
19 double result = client.Add(a, b);
20 Console.WriteLine("Add:{0} + {1} = {2}",a,b,result);
21 //Substract
22 a = 100.00d;
23 b = 15.99d;
24 result = client.Subtract(a, b);
25 Console.WriteLine("Sub:{0} + {1} = {2}",a,b,result);
26 //Multiply
27 a = 9.00d;
28 b = 81.25d;
29 result = client.Multiply(a, b);
30 Console.WriteLine("Mul:{0} + {1} = {2}",a,b,result);
31 //Divide
32 a = 100.00d;
33 b = 15.99d;
34 result = client.Divide(a, b);
35 Console.WriteLine("Div:{0} + {1} = {2}",a,b,result);
36
37 }
38 }
39}
40