[英]WCF Rest coed for Json
通常,我看到WCF REST JSON的代码可能是:
// Start the service and browse to <a href="http://:/SampleService/help[ServiceContract] public">http://<machine_name>:<port>/SampleService/help [ServiceContract]
public interface ISampleService {
[WebGet(UriTemplate = "")]
List<SampleItem> GetCollection();
这意味着引入了接口。 但是在某些情况下,我发现根本没有使用过代码界面。 如
[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class AlertService
{
AlertContext alertProxy = new AlertContext();
AlertDetailsContext alertDetailProxy = new AlertDetailsContext();
Analytics analyticsProxy = new Analytics();
[OperationContract]
[WebInvoke(BodyStyle=WebMessageBodyStyle.Bare, RequestFormat=WebMessageFormat.Json, ResponseFormat=WebMessageFormat.Json, UriTemplate="/SearchAlerts")]
public List<Alert> SearchAlerts(AlertFilter filter)
{
WebOperationContext.Current.OutgoingResponse.Headers.Add("Cache-Control", "no-cache, must-revalidate");
WebOperationContext.Current.OutgoingResponse.Headers.Add("Expires", "Sat, 26 Jul 1997 05:00:00 GMT");
这里的类直接使用,为什么?
因为ServiceContract
不必一定是interface
合同。 使用接口可以使您在多个实现之间共享同一协定,并且可以进行单元测试,但这不是必需的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.