【问题标题】:WCF Rest coed for JsonWCF Rest coed for Json
【发布时间】:2012-12-05 15:48:37
【问题描述】:

通常我看到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");

这里直接使用class,为什么?

【问题讨论】:

    标签: c# json wcf rest interface


    【解决方案1】:

    因为ServiceContract 不一定是interface 合约。使用接口可以让您在多个实现之间共享同一个契约,并且可以进行单元测试,但这不是必需的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-04
      • 2017-11-25
      相关资源
      最近更新 更多