WCF契约描述

Code
WCF系统架构

 1 namespace WCF
 2  {
 3  
 4      /*----------------------------------------------------------
 5       WCF契约
 6       1服务契约[ServiceContract]
 7         客户端能够执行的服务操作.
 8       2数据契约[Data Contract]
 9         定义与服务交互的数据类型.如Int,String,Double 等
10       3错误契约[Fault Contract]
11          *服务端有什么错误 告诉客户端应用程序
12       4消息契约[Message Contract]
13         允许服务直接与消息交互
14      ------------------------------------------------------------ */
15      
16      //服务契约
17      [ServiceContract]
18      public interface IService1
19      {
20          [OperationContract]/*用于描述 该类是服务的一部分*/
21          string GetData(int value);
22  
23          [OperationContract]
24          CompositeType GetDataUsingDataContract(CompositeType composite);
25  
26          //不会成为服务的一部分
27          string GetAny(string name);
28      }
29  
30      //服务
31      public class ServiceInfo : IService1
32      {
33  
34          public string GetData(int value)
35          {
36              return (value + value).ToString(CultureInfo.InvariantCulture);
37          }
38  
39          public CompositeType GetDataUsingDataContract(CompositeType composite)
40          {
41              if (composite == null)
42              {
43                  throw new ArgumentNullException("composite");
44              }
45              if (composite.BoolValue)
46              {
47                  composite.StringValue += "Suffix";
48              }
49              return composite;
50          }
51  
52          public string GetAny(string name)
53          {
54              return "不是WCF服务的方法";
55          }
56      }
57  
58      //数据契约
59      [DataContract]
60      public class CompositeType
61      {
62          bool _boolValue = true;
63          string _stringValue = "Hello  WCF!";
64  
65          [DataMember]
66          public bool BoolValue
67          {
68              get { return _boolValue; }
69              set { _boolValue = value; }
70          }
71  
72          [DataMember]
73          public string StringValue
74          {
75              get { return _stringValue; }
76              set { _stringValue = value; }
77          }
78      }
79  
80      [AttributeUsage(AttributeTargets.Interface|AttributeTargets.Class,Inherited =false)]
81      public sealed class ServiceContractAttribute : Attribute //允许开发者定义一个服务契约
82      {     
83          public string Name { get; set; }
84          public string NameSpace { get; set; }
85          //更多成员
86      }
87  }
88  
posted @ 2012-10-22 14:50  在 水 一 方  阅读(335)  评论(1编辑  收藏  举报