2011-04-12 47 views
0

我正在通过WCF上的HTTP post服务向客户端返回一个字符串值。通过C#WCF服务返回输出值

我可以返回一个状态码好通过以下:

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

你的方法设置了'void's。您可以更改您方法的声明,例如'公共字符串PostSMS(流输入)'如果你想返回一个'字符串'。 – 2011-04-12 14:00:01

回答

2

你需要让你的方法实际上返回的东西尼尔指出。

所以只要改变你的方法签名看起来像

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"; 
    } 
+0

感谢您的回应。我试图通过HTTP发送一个空白页面,其中包含的字符串。这是否适用于此目的?谢谢 – Nick 2011-04-12 14:40:13