一、准备
这里涉及到三个文件,现在只是简单的把代码贴出来,后面再详细的讲一下。
三个文件分别是(都是wcf服务应用程序项目下的):
1、IService1.cs
2、Service1.svc
3、Web.config
wcf的契约文件:IService1.cs
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Runtime.Serialization; 5 using System.ServiceModel; 6 using System.ServiceModel.Web; 7 using System.Text; 8 using DAL; 9 10 namespace HttpVisitWCF2 11 { 12 // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IService1”。 13 [ServiceContract] 14 public interface IService1 15 { 16 17 [OperationContract] 18 [WebGet(UriTemplate="/GetData/{value}",RequestFormat=WebMessageFormat.Json,ResponseFormat=WebMessageFormat.Json)] 19 TestModel GetData(string value); 20 21 [OperationContract] 22 CompositeType GetDataUsingDataContract(CompositeType composite); 23 24 // TODO: 在此添加您的服务操作 25 } 26 27 28 // 使用下面示例中说明的数据约定将复合类型添加到服务操作。 29 [DataContract] 30 public class CompositeType 31 { 32 bool boolValue = true; 33 string stringValue = "Hello "; 34 35 [DataMember] 36 public bool BoolValue 37 { 38 get { return boolValue; } 39 set { boolValue = value; } 40 } 41 42 [DataMember] 43 public string StringValue 44 { 45 get { return stringValue; } 46 set { stringValue = value; } 47 } 48 } 49 }