我正在调用 WebAPI C# Get 方法,但出现了一个奇怪的错误,该错误仅当应用程序在
Production
上运行时才会发生。当应用程序在本地运行时,没有错误。这是错误:
在E:\Programming\BetFray\Application\Controllers\PostRelatedControllers\WallPostController.cs中的Application.Controllers.PostRelatedControllers.WallPostController.Get(Int32skip, Int32take,BooleanisProfile,Stringotherid):第35行
在 lambda_method( 闭包 , 对象 , 对象[] )
在 System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.b__9(对象实例,Object[] methodParameters)
在 System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(对象实例,Object[] 参数)
在 System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContextcontrollerContext, IDictionary`2 个参数, CancellationToken CancellationToken)从先前抛出异常的位置开始的堆栈跟踪结束
在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)
在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)
↵ 在 System.Web.Http.Controllers.ApiControllerActionInvoker.d__0.MoveNext() ↵--- 从先前抛出异常的位置开始的堆栈跟踪结束 --- ↵ 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务) ↵ 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务) ↵ 在 System.Web.Http.Controllers.ActionFilterResult.d__2.MoveNext() ↵--- 从先前抛出异常的位置开始的堆栈跟踪结束 --- ↵ 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务) ↵ 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务) ↵ 在 System.Web.Http.Filters.AuthorizationFilterAttribute.d__2.MoveNext() ↵--- 从先前抛出异常的位置开始的堆栈跟踪结束 --- ↵ 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务) ↵ 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务) ↵ 在 System.Web.Http.Controllers.AuthenticationFilterResult.d__0.MoveNext() ↵--- 从先前抛出异常的位置开始的堆栈跟踪结束 --- ↵ 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务) ↵ 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务) ↵ 在 System.Web.Http.Dispatcher.HttpControllerDispatcher.d__1.MoveNext()"
这是我怀疑引发错误的方法:
var query = Set.Where(x => x.IsGroupPost == false && ((x.IsPostPrivate.Value && frineds.Contains(x.UserID)) || !x.IsPostPrivate.Value)).OrderByDescending(x => x.PostId).AsNoTracking();
这是
WallPostCore
的方法:
public List<WallPostViewModel> GetWallPosts(int skip, int take, bool isProfile, string otherid, string userid)
{
WallPostViewRepository wallPostViewRepository = new WallPostViewRepository();
RelationshipsRepository relationshipsRepository = new RelationshipsRepository();
List<WallPostView> posts;
if (isProfile)
{
bool isOwnProfile = (string.IsNullOrWhiteSpace(otherid) && otherid != userid) ? true : false;
if (isOwnProfile)
{
posts = wallPostViewRepository.GetOwn(skip, take, userid).ToList();
}
else
{
var relationships = (userid == null) ? null : relationshipsRepository.FindFirstRelationShipIfExist(userid, otherid);
if (relationships != null)
posts = (relationships.AcceptedDate != null && relationships.Friend)
? wallPostViewRepository.GetOwn(skip, take, otherid).ToList()
: wallPostViewRepository.GetOtherProfileNoFriends(skip, take, otherid).ToList();
else
posts = wallPostViewRepository.GetOtherProfileNoFriends(skip, take, otherid).ToList();
}
}
else if(userid == null)
{
posts = wallPostViewRepository.GetLatestPublic(skip, take).ToList();
}
else
{
var relationships = relationshipsRepository.GetListOfFriends(userid).Where(x => x.AcceptedDate != null);
var friendsIds = relationships.AsParallel().Where(x => x.Friend).GetNotMe(userid).Select(x => x.UserID).ToList();
friendsIds.Add(userid);
posts = wallPostViewRepository.GetLatest(skip, take, friendsIds).ToList();
}
var userIds = posts.Select(x => x.UserID).Distinct().ToList();
var userStats = GenerateUserStats(wallPostViewRepository.Context, userIds);
var convertedPosts = posts.Select(x => x.SoftConvert(userStats, userid)).ToList();
var postIds = posts.Select(x => x.PostId);
GenerateWallPosts(wallPostViewRepository.Context, convertedPosts, postIds, userIds, userid);
return convertedPosts.ToList();
}
这是 WallPostController
[Authorize]
public class WallPostController : ApiController
{
[AllowAnonymous]
[HttpGet]
public List<WallPostViewModel> Get(int skip, int take, bool isProfile, string otherid)
{
WallpostCore core = new WallpostCore();
try
{
var userid = User.Identity.GetUserId();
var posts = core.GetWallPosts(skip, take, isProfile, otherid, userid);
return posts;
}
catch (Exception e)
{
var resp = new HttpResponseMessage(HttpStatusCode.NotFound)
{
Content = new StringContent(e.StackTrace + "" + e.Message + " " + e.StackTrace +
"inner exception " + e.InnerException.Message + "\n" +
"second inner" +
e.InnerException.InnerException.Message),
};
throw new HttpResponseException(resp);
}
}
第 35 行结束是:
var resp = new HttpResponseMessage(HttpStatusCode.NotFound)
首先,您永远不应该将完整的服务器异常堆栈跟踪返回给客户端。这是极其危险的。您应该始终将错误记录在数据库中。
其次,你不应该以这种方式重建异常消息。只需拨打
.ToString()
,它就会提供您想要做的一切
您的问题在
e.InnerException.InnerException.Message
如果异常没有第一级或第二级内部异常,则此调用将因空引用而出错