Reputation: 1611
I want to read the body of a HttpRequest received by a WCF web service.
The WCF web service looks like so:
[ServiceContract]
public interface ITestRestService
{
[OperationContract]
[WebInvoke(Method = "POST",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "/test")]
string Test(Stream aStrm);
}
The client that sends to this service successfully invokes the Test()
method but the aStrm
throws an exception:
'aStrm.ReadTimeout' threw an exception of type 'System.InvalidOperationException'
Should I be using the stream to send the body or something else?
I can send the data as part of the url when the configs for that contract look like so:
[WebInvoke(Method = "POST",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "?s={aStr}")]
string Test(string aStr);
but is this common practice? shouldn't I logically be adding the content to the body of the request instead of the url?
I've read similar questions but I'm still uncertain.
Upvotes: 0
Views: 1093
Reputation: 22362
I agree with you that the body of the POST request is a better place to send the data. Though some web services do send data via the url instead. Apart from semantics there are security concerns if sensitive data is sent as url's can show up in administrative logs in plain text, etc.
Unless you really need a Stream
for large amounts of data, you can create a model class to transport the data. Earlier versions of ASP.NET Web API (the successor of WCF Web API) required you to use a full class for the POST body.
I would try something like
public class PostData
{
public string aStr { get; set; }
}
[WebInvoke(Method = "POST",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "?s={aStr}")]
string Test(PostData data);
Here is a reference you can use regarding Posting with WCF. He demonstrates posting JSON data via a model. http://blog.alexonasp.net/post/2011/05/03/REST-using-the-WCF-Web-API-e28093-POST-it!.aspx
Upvotes: 1