2014-10-21 70 views
2

我有一個WebAPI,我已啓用Cors。從WebAPI調用引用的dll在本地工作,但不在服務器上

我的WebAPI引用一個類庫,我做了很多的處理。在的WebAPI和類庫,也類庫裏面的一些電話是

引發此錯誤之間

電話...

{"Message":"An error has occurred.","ExceptionMessage":"Object reference not set to an instance of an object.", 
"ExceptionType":"System.NullReferenceException","StackTrace":" 
at RCIS.MappingServices.WebAPI.Utilities.SQL.SQLInsertObjects(Int32 TypeMapId, Int32 GrowerId, Int32 CropYear, String NAME, String SHAPE, String CREATED_BY, String CREATED_DATETIME, String MODIFIED_BY, String MODIFIED_DATETIME, String IsDELETED)\r\n 
at RCIS.MappingServices.WebAPI.Controllers.PostObjectsController.GET(String TypeMapId, String GrowerId, String CropYear, String NAME, String SHAPE)\r\n 
at lambda_method(Closure , Object , Object[])\r\n 
at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.<GetExecutor>b__9(Object instance, Object[] methodParameters)\r\n 
at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)\r\n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, 
IDictionary`2 arguments, CancellationToken cancellationToken)\r\n--- End of stack trace from previous location where exception was thrown ---\r\n 
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n 
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n 
at System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__0.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n 
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n 
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n 
at System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__2.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n 
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n 
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n 
at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()"} 

這似乎只調用從服務器(即)類庫,當我輸入網址到瀏覽器調用的WebAPI方法和得到這個錯誤回發生。

如果我在WebAPI中使用所有基元類型,並在類庫中獲得成功消息。

如果您有任何問題,請讓我知道。 任何幫助,將不勝感激我一直在試圖解決這個問題2天!

+0

您是否找到解決此問題的解決方案?我有一個類似的問題。 – 2017-05-27 00:07:43

回答

0

確保您唯一傳遞的是字符串值。

0

做一個ErrorHelper:

public class ErrorHelper : IHttpActionResult 
{ 
    private HttpRequestMessage Request { get; } 
    private HttpStatusCode statusCode; 
    private string message; 

    public ErrorHelper(HttpRequestMessage request, HttpStatusCode statusCode, string message) 
    { 
     this.Request = request; 
     this.statusCode = statusCode; 
     this.message = message; 
    } 

    public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken) 
    { 
     return Task.FromResult(Request.CreateErrorResponse(statusCode, message)); 
    } 
} 

然後調用它像這樣:

public IHttpActionResult Get(int id) 
{ 
    try 
    { 
     var person = _repository.GetPersonId(id); 
     if (person == null) 
     { 
      return NotFound(); 
     } 
     return Ok(person); 
    } 
    catch (Exception ex) 
    { 
     return new ErrorHelper(Request, HttpStatusCode.InternalServerError, ex.ToString()); 
    } 
} 

這會給你的異常詳細信息。您可能沒有在服務器上安裝組件,如數據庫驅動程序或類似的東西。

相關問題