简体   繁体   English

WCF Rest换Json

[英]WCF Rest coed for Json

Usually I see the code of WCF REST JSON is likely: 通常,我看到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();

That means interface is introduced. 这意味着引入了接口。 But in some occurence I saw in the code interface is never used at all. 但是在某些情况下,我发现根本没有使用过代码界面。 Such as

 [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");

Here class is used directly, why? 这里的类直接使用,为什么?

Because the ServiceContract doesn't have to be an interface contract. 因为ServiceContract不必一定是interface合同。 Using an interface allows you to share the same contract among multiple implementations and can make unit testing possible, but it is not required. 使用接口可以使您在多个实现之间共享同一协定,并且可以进行单元测试,但这不是必需的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM