Hi All,
We have a problem with our Web API. When we try to run locally the Web API it is working but when we deploy it on the server it will throw an exception message which is
Object reference not set to an instance of an object
This is the StackTrace.
StackTrace: " at lambda_method(Closure , Object , Object[] )\r\n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass6_2.b__2(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.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Controllers.ApiControllerActionInvoker.d__1.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Controllers.ActionFilterResult.d__5.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Filters.AuthorizationFilterAttribute.d__3.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Controllers.AuthenticationFilterResult.d__5.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Dispatcher.HttpControllerDispatcher.d__15.MoveNext()"
Controller:
[HttpPost]
[Route("GetEmployeeNameByUserId")]
public dynamic GetEmployeeNameByUserId()
{
List userinfo = AuthHandler.GetUserInfo();
return _UnitOfWork.UserLogRepo().GetEmployeeNameByUserId(userinfo[0]);
}
Repository:
public dynamic GetEmployeeNameByUserId(string WD)
{
try
{
if (WD != string.Empty)
{
int userid = CheckWD(WD);
if (userid != 0)
{
var GetEmployeeName = GetEmployee()
.Where(x => x.UserId == userid && x.isActive == true)
.Select(n => new
{
firstName = n.FirstName,
lastName = n.LastName,
isActive = n.isActive
})
.ToList();
if (GetEmployeeName.Count != 0)
{
return GetEmployeeName;
}
else
{
return "";
}
}
else
{
return "";
}
}
else
{
return "";
}
}
catch (Exception ex)
{
return ex.Message.ToString();
}
What I have tried:
We already tried to insert this line of code.
GlobalConfiguration.Configuration.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
And also we try to replace the parameter with a null value to check if it will throw an exception message.