通过C#WCF服务返回输出值

时间:2011-04-12 13:55:19

标签: c# asp.net wcf web-services

我希望通过WCF上的HTTP post服务将字符串值返回给客户端。

我可以通过以下方式返回状态代码:

  

WebOperationContext.Current.OutgoingResponse.StatusCode   = HttpStatusCode.OK;

...但是我不完全确定如何将字符串值返回给客户端。

任何指针都会非常感激。

由于

尼克

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;
    }

1 个答案:

答案 0 :(得分:2)

你需要让你的方法实际返回Neil所指出的东西。

所以只需将方法签名更改为

即可
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";
    }