【问题标题】:Can't execute WCF REST services for POST and GET with basic endpoint address无法使用基本端点地址为 POST 和 GET 执行 WCF REST 服务
【发布时间】:2014-04-17 05:30:54
【问题描述】:

我正在尝试使用以下端点(以及其他端点)创建一组 WCF REST 服务:

  • /Session(支持 HTTP POST 并创建 Session 对象。在响应中返回 sid)
  • /Session/{sid}(支持 HTTP GET 并返回代表先前创建的 Session 的 JSON 对象)

以下是服务合同中的定义:

[OperationContract]
[WebInvoke(Method="POST", RequestFormat=WebMessageFormat.Json, ResponseFormat=WebMessageFormat.Json, UriTemplate="Session")]
        string InitializeSession(Stream contents);

[OperationContract]
[WebInvoke (Method="GET", RequestFormat=WebMessageFormat.Json,
ResponseFormat=WebMessageFormat.Json, UriTemplate="Session/{sid}")]
        string RetrieveSession(string sid);

如果没有定义 GET 操作,我可以很好地调用 POST 并在响应中获得预期的 sid。当包含 GET OperationContract 时,调用 POST 会抛出:

500 System.ServiceModel.ServiceActivationException

响应中没有其他数据(非常有帮助),即使我有

<serviceDebug includeExceptionDetailInFaults="true"/>

在 web.config 的服务行为中定义。

OperationContract 定义对于我想要实现的目标是否正确(假设我想要实现的目标是正确的 RESTful)?如果是这样,我可能会遗漏哪些愚蠢的配置选项可以允许访问这两个操作?

【问题讨论】:

标签: c# web-services wcf rest


【解决方案1】:
Try changing your operation contracts of your get and post methods to some thing like below: 

1. Post

[OperationContract]
[WebInvoke(UriTemplate = "Session", Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]

2. Get
 [OperationContract]
 [WebInvoke(UriTemplate = "Session/{sid}", Method = "GET", ResponseFormat = 
  WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]

【讨论】:

    猜你喜欢
    • 2013-09-14
    • 2011-10-19
    • 2017-12-05
    • 2011-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多