用VisualStudio2010学习WCF服务编程总结(1)操作重载

一、服务端:

新建WCF服务应用程序

1,服务契约

1 [ServiceContract]
2 public interface ICalculator
3 {
4         [OperationContract(Name = "AddInt")]
5         int Add(int arg1, int arg2);
6 
7         [OperationContract(Name = "AddDouble")]
8         double Add(double arg1, double arg2);
9 }

2, 实现契约的服务类型

 1 namespace MyService
 2 {
 3     public class Service1 : ICalculator
 4     {
 5         public int Add(int arg1, int arg2)
 6         {
 7             return arg1 + arg2;
 8         }
 9 
10         public double Add(double arg1, double arg2)
11         {
12             return arg1 + arg2;
13         }
14     }
15 }

3,生成项目,将其配置到IIS中。

 

二、客户端

1,新建控制台应用程序。

2,然后添加服务引用,选择在IIS中配置好宿主。

3,在项目文件中多了一个Service References文件双击展开找到一个文件(我的是myServiceReference),右键选择在对象浏览器中查看。

4,在对象浏览器中,找到myClient中的引用的ICalculator接口,双击进行编辑。

5,代码如下:

 

 1 namespace MyCilent.myServiceReference {
 2     
 3     
 4     [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel""4.0.0.0")]
 5     [System.ServiceModel.ServiceContractAttribute(ConfigurationName="myServiceReference.ICalculator")]
 6     public interface ICalculator {
 7         
 8         [System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/ICalculator/AddInt", ReplyAction="http://tempuri.org/ICalculator/AddIntResponse", Name="AddInt")]
 9         int Add(int arg1, int arg2);
10         
11         [System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/ICalculator/AddDouble", ReplyAction="http://tempuri.org/ICalculator/AddDoubleResponse",Name="AddDouble")]
12         double Add(double arg1, double arg2);
13     }

14     
15     [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel""4.0.0.0")]
16     public interface ICalculatorChannel : MyCilent.myServiceReference.ICalculator, System.ServiceModel.IClientChannel {
17     }
18     
19     [System.Diagnostics.DebuggerStepThroughAttribute()]
20     [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel""4.0.0.0")]
21     public partial class CalculatorClient : System.ServiceModel.ClientBase<MyCilent.myServiceReference.ICalculator>, MyCilent.myServiceReference.ICalculator {
22         
23         public CalculatorClient() {
24         }
25         
26         public CalculatorClient(string endpointConfigurationName) : 
27                 base(endpointConfigurationName) {
28         }
29         
30         public CalculatorClient(string endpointConfigurationName, string remoteAddress) : 
31                 base(endpointConfigurationName, remoteAddress) {
32         }
33         
34         public CalculatorClient(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) : 
35                 base(endpointConfigurationName, remoteAddress) {
36         }
37         
38         public CalculatorClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) : 
39                 base(binding, remoteAddress) {
40         }
41         
42         public int Add(int arg1, int arg2) {
43             return base.Channel.Add(arg1, arg2);
44         }
45         
46         public double Add(double arg1, double arg2) {
47             return base.Channel.Add(arg1, arg2);
48         }

49     }
50 }

 

posted @ 2012-09-22 11:49  HelloCG  阅读(959)  评论(1编辑  收藏  举报