简体   繁体   中英

Can't execute WCF REST services for POST and GET with basic endpoint address

I'm attempting to create a set of WCF REST services with the following endpoints (among others):

  • /Session (supports HTTP POST and creates a Session object. Returns sid in the response)
  • /Session/{sid} (supports HTTP GET and returns a JSON object representing a previously created Session)

Here are the definitions in the service contract:

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

Without the GET Operation defined I can invoke the POST just fine and get the expected sid in the response. When the GET OperationContract is included, invoking the POST throws:

500 System.ServiceModel.ServiceActivationException

with no additional data in the response (very helpful) even though I've got

<serviceDebug includeExceptionDetailInFaults="true"/>

defined in the Service Behavior in web.config.

Do the OperationContract definitions look correct for what I'm trying to achieve (assuming what I'm trying to achieve is properly RESTful)? If so any ideas on what silly config option I'm likely missing that would allow access to both operations?

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)]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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