2017-12-18 168 views
0

我創建了一個ASP.NET Web API,它將調用java web服務並在JSON中顯示一些結果。當java web服務未運行時,我收到錯誤消息:返回任務<IHttpActionResult>方法的JSON錯誤消息

{「Message」:「發生錯誤。」,「ExceptionMessage」:「方法或操作未實現。」, C:\ Users \ attsuap1 \ Desktop \ Server Project \ ApiClientJavaWebService \ ApiClientJavaWebService \ Models \ RestfulClient.cs中的ApiClientJavaWebService.Models.RestfulClient.InternalServerError(異常例外)中的「ExceptionType」:「System.NotImplementedException」,「StackTrace」在C:\ Users \ attsuap1 \ Desktop \ Server Project \ ApiClientJavaWebService \ ApiClientJavaWebService \ Models \ RestfulClient.cs中的ApiClientJavaWebService.Models.RestfulClient.d__3.MoveNext()中的第43行\ r \ n:第36行\ r \ n ---從拋出異常的上一個位置結束堆棧跟蹤--- System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任務任務)中的\ r \ n \ System.Runtim中的\ r \ n e.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任務任務)\ r \ n在System.Runtime.CompilerServices.TaskAwaiter 1.GetResult()\ r \ n在ApiClientJavaWebService.Controllers.AdditionController.d__1.MoveNext()在C:\ Users \ attsuap1 \ Desktop \ Server Project \ ApiClientJavaWebService \ ApiClientJavaWebService \ Controllers \ AdditionController.cs:第20行\ r \ n ---從以前位置拋出異常的堆棧跟蹤結束--- \ r \ n在System.Runtime.CompilerServices .TaskAwaiter.ThrowForNonSuccess(任務任務)\ r \ n在System.Threading.Tasks.TaskHelpersExtensions.d__3`1.MoveNext()\ r \ n System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任務任務)\ r \ n ---從以前位置拋出異常的堆棧跟蹤結束---在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任務任務)上的\ r \ n在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任務任務)\ r \ n在System.Web.Http.Controllers.ApiControl lerActionInvoker.d__0.MoveNext()\ r \ n ---從之前的位置拋出異常的堆棧跟蹤結束--- \ r \ n在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任務任務)\ r \ n System.Web.Http.Controllers.ActionFilterResult.d__2.MoveNext()\ r \ n下System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任務任務)\ r \ n ---以前位置的堆棧跟蹤結束,其中異常在System.Web的System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任務任務)\ r \ n處,System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任務任務)\ r \ n處拋出--- \ r \ n。 Http.Dispatcher.HttpControllerDispatcher.d__1.MoveNext()「}

我要如何改變這種去顯示爲{"The server is not running."}錯誤信息?

這裏是我的代碼:

AdditionClient.cs

public class RestfulClient 
{ 
    private static HttpClient client; 
    private static string BASE_URL = "http://localhost:8080/"; 

    static RestfulClient() 
    { 
     client = new HttpClient(); 
     client.BaseAddress = new Uri(BASE_URL); 
     client.DefaultRequestHeaders.Accept.Add(
      new MediaTypeWithQualityHeaderValue("application/json")); 
    } 

    public async Task<string> addition(int firstNumber, int secondNumber) 
    { 

     try 
     { 
      var endpoint = string.Format("addition/{0}/{1}", firstNumber, secondNumber); 
      var response = await client.GetAsync(endpoint); 
      return await response.Content.ReadAsStringAsync(); 
     } 

     catch (Exception e) 
     { 

     } 

     return null; 
    } 
} 

AdditionController.cs

public class AdditionController : ApiController 
{ 
    private RestfulClient restfulClient = new RestfulClient(); 
    public async Task<IHttpActionResult> Get(int firstNumber, int secondNumber) 
    { 
     var result = await restfulClient.addition(firstNumber, secondNumber); 
     var resultDTO = JsonConvert.DeserializeObject<Temp>(result); 
     return Json(resultDTO); 
    } 
} 

有人請你幫幫我。提前謝謝你。

+0

你得到'System.NotImplementedException'。只要找到,你扔它 – Backs

回答

0

我已經達到了我想要的。我意識到try catch方法應該在控制器類中而不是AdditionClient.cs類中。

這裏是我的編輯類:

AdditionClient.cs

public class RestfulClient 
{ 
    private static HttpClient client; 
    private static string BASE_URL = "http://localhost:8080/"; 

    static RestfulClient() 
    { 
     client = new HttpClient(); 
     client.BaseAddress = new Uri(BASE_URL); 
     client.DefaultRequestHeaders.Accept.Add(
      new MediaTypeWithQualityHeaderValue("application/json")); 
    } 

    public async Task<string> addition(int firstNumber, int secondNumber) 
    { 
      var endpoint = string.Format("addition/{0}/{1}", firstNumber, secondNumber); 
      var response = await client.GetAsync(endpoint); 
      return await response.Content.ReadAsStringAsync(); 
    } 
} 

AdditionController.cs(聚焦在嘗試捕捉部分)

 try 
     { 
      var result = await restfulClient.addition(firstNumber, secondNumber); 
      var resultDTO = JsonConvert.DeserializeObject<Temp>(result); 
      return Json(resultDTO); 
     } 

     catch (Exception e) 
     { 
      var result = @"Server is down"; 
      return Ok(new { ErrorMessage = result }); 
     } 

,這將是結果當服務器沒有運行時,在瀏覽器上顯示的是{"ErrorMessage":"Server is down"}

1

我想,你不能管理拋出你想要拉動數據的服務器的異常消息。那麼,爲什麼不過濾這個異常呢?

 try 
     { 
      var endpoint = string.Format("addition/{0}/{1}", firstNumber, secondNumber); 
      var response = await client.GetAsync(endpoint); 
      return await response.Content.ReadAsStringAsync(); 
     } 

     catch (Exception e) 
     { 
      if (e.InnerException.Message == "The method or operation is not implemented") 
      { 
       //Do something 
      } 
     } 
+0

嗨謝謝你幫助我,是的我不能管理拋出異常消息,我想從中拉數據的服務器。我應該做些什麼//做些什麼? –

+0

我對此很新,我以前沒有這樣做過。根據我研究的內容,我嘗試了代碼,如'返回內容(HttpStatusCode.BadRequest,「服務器不運行」);''返回ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.InternalServerError,「Server is not running」)); '但是所有這些都不起作用 –

+0

如果拋出的異常是「服務器沒有運行」,你應該怎麼做。您可以將它登錄到某個地方,或者將響應返回給客戶端。如果應用程序具有通用錯誤處理機制,則拋出一個帶有要顯示的消息的異常。 – lucky