C#更改WebMethod状态会导致连接重置

时间:2018-09-10 17:07:19

标签: c# wcf webmethod

我正在尝试为Web方法返回不同的状态-但似乎连接被重置或者我的状态没有改变(并保持200)

public interface IService
{
    [OperationContract]
    [WebInvoke(Method = "GET", UriTemplate ="MyMethod?val={val}",
        ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
     string MyMethod(int v);
}

public class MyService : IService 
{
    [WebMethod]
    public string MyMethod(int v) 
    {
        if (v > 10) 
        {
            var res = HttpContext.Current.Response;
            res.Clear();
            res.StatusCode = 400; // or whatever code is appropriate
            res.StatusDescription = "don't send anything over 10!";
            res.Write("nothing over 10");
            res.Flush();
            res.End();
            return null;                
        }
        return "good answer";
    }
}

如果我删除“ flush / end”,我将得到200和发送的所有内容,如果保留它们,则会在客户端获得连接重置。 有什么主意吗?

0 个答案:

没有答案
相关问题