如何在响应中接收Wcf状态

时间:2016-10-06 12:45:07

标签: web-services wcf

我创建了WCF服务,我使用DoSomething()方法,如下所示:

 public class MyService : IMyService
{
    public void DoSomething()
    {
        WebOperationContext _ctx = WebOperationContext.Current;
        _ctx.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.OK;

    }
}

此外,我需要阅读"确定"我使用此服务时代码中的状态:

 ServiceClient _serviceClient = new ServiceClient();
            _serviceClient.DoSomething();

            WebOperationContext _ctx = WebOperationContext.Current;
            var statusObj = _ctx.IncomingResponse.StatusCode;

我发现_ctx null

我想问你如何阅读statusObj中的状态。

1 个答案:

答案 0 :(得分:1)

你可以尝试这个,

肥皂终点:

WCFServiceClient soapClient = new WCFServiceClient();
            using (OperationContextScope scope = new OperationContextScope(soapClient.InnerChannel))
            {
                    soapClient.DoSomething();
                    var response = (HttpResponseMessageProperty)OperationContext.Current.IncomingMessageProperties["httpResponse"];
                    var status = response.StatusCode;
            }

休息终点:

 var client = new RestClient("service-url/rest/DoSomething");
        {
            var request = new RestRequest(Method.GET);
            IRestResponse response = client.Execute(request);
            var status = response.StatusCode;
        }