2016-10-06 65 views
0

我創建WCF服務,我用DoSomething()方法是這樣的:如何接收WCF狀態響應

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

    } 
} 

而且,我需要看在我的代碼「OK」狀態時,我使用這個服務:

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

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

我發現_ctx

我想問你怎麼在我的讀取狀態。

回答

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