Reputation: 5882
I'm looking to return a string value to the client via my HTTP post service on WCF.
I can return a status code okay via the following:
WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.OK;
... however I'm not entirely sure how to return a string value to the client.
Any pointers would be much appreciated.
Thanks
Nick
namespace TextWCF
{
[ServiceContract]
public interface IShortMessageService
{
[WebInvoke(UriTemplate = "invoke", Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest)]
[OperationContract]
void PostSMS(Stream input);
}
}
[OperationBehavior]
public void PostSMS(Stream input)
{
StreamReader sr = new StreamReader(input);
string s = sr.ReadToEnd();
sr.Dispose();
NameValueCollection qs = HttpUtility.ParseQueryString(s);
string user = Convert.ToString(qs["user"]);
string password = qs["password"];
string api_id = qs["api_id"];
string to = qs["to"];
string text = qs["text"];
string from = qs["from"];
WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.OK;
WebOperationContext.Current.OutgoingResponse. = HttpStatusCode.OK;
}
Upvotes: 0
Views: 2651
Reputation: 15242
You need to have your method actually return something as Neil pointed out.
So just change your method signature to look like
namespace TextWCF
{
[ServiceContract]
public interface IShortMessageService
{
[WebInvoke(UriTemplate = "invoke", Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest)]
[OperationContract]
string PostSMS(Stream input);
}
}
[OperationBehavior]
public string PostSMS(Stream input)
{
StreamReader sr = new StreamReader(input);
string s = sr.ReadToEnd();
sr.Dispose();
NameValueCollection qs = HttpUtility.ParseQueryString(s);
string user = Convert.ToString(qs["user"]);
string password = qs["password"];
string api_id = qs["api_id"];
string to = qs["to"];
string text = qs["text"];
string from = qs["from"];
WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.OK;
WebOperationContext.Current.OutgoingResponse. = HttpStatusCode.OK;
return "Some String";
}
Upvotes: 2