定义参数模型
[DataContract]
public class DataResult
{
[DataMember]
public string Code { get; set; }
[DataMember]
public string Message { get; set; }
}
[DataContract]
public class InputModel
{
[DataMember]
public int ID { get; set; }
[DataMember]
public string Name { get; set; }
}
创建 Web 终结点
[ServiceContract]
public interface IGetDataService
{
[OperationContract]
[WebInvoke(Method = "GET", UriTemplate = "GetData/{value}", ResponseFormat = WebMessageFormat.Json)]
string GetData(string value);
[OperationContract]
[WebInvoke(Method = "GET", UriTemplate = "GetTaskID?value={value}", ResponseFormat = WebMessageFormat.Json)]
string GetTaskID(string value);
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "SaveMessage", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
DataResult SaveMessage(InputModel inputParameter);
}
这里定义了两个Get方法不同的URL:
使用“UriTemplate = GetData/{value}”时,value不可为空
使用UriTemplate = "GetTaskID?value={value}"时,value可为空。
实现服务协定
public class GetDataService : IGetDataService
{
public string GetData(string value)
{
return string.Format("You entered: {0}", value);
}
public string GetTaskID(string value)
{
return string.Format("You entered: {0}", value);
}
public DataResult SaveMessage(InputModel inputParameter)
{
DataResult result = new DataResult
{
Code = "1",