首页 文章

Azure服务器,“ExceptionType”:“System.NullReferenceException”C#,SQL Server,适用于localhost,不适用于服务器[重复]

提问于
浏览
0

这个问题在这里已有答案:

我正在调用WebAPI C#Get方法,但是我遇到了一个奇怪的错误,只有当应用程序在 Production 上运行时才会发生 . 当应用程序在本地工作时没有错误 . 这是错误:

在E:\ Programming \ BetFray \ Application \ Controllers \ PostRelatedControllers \ WallPostController.cs中的Application.Controllers.PostRelatedControllers.WallPostController.Get(Int32 skip,Int32 take,Boolean isProfile,String otherid):lambda_method上的第35行(Closure,Object ,Object [])在System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor . <> c__DisplayClass10.b__9(Object instance,Object [] methodParameters)at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext,IDictionary`2 arguments,CancellationToken cancellationToken)中的Object [] arguments)从System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess抛出异常的上一个位置的堆栈跟踪结束(System任务)System.Web.Http.Contro上的System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)( llers.ApiControllerActionInvoker.d__0.MoveNext()↵---从抛出异常的上一个位置开始的堆栈跟踪结束---↵在System.Runtime.CompilerServices上的System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)↵ . System.Web.Http.Controllers.ActionFilterResult.d__2.MoveNext()中的TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)↵---从抛出异常的上一个位置开始的堆栈跟踪---在System.Runtime.CompilerServices上 . SystemAweiter.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.We b.Http.Controllers.AuthenticationFilterResult.d__0.MoveNext()↵---从抛出异常的上一个位置开始的堆栈跟踪结束---在系统中的System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)↵ . System.Web.Http.Dispatcher.HttpControllerDispatcher.d__1.MoveNext()中的Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)“”

这是我怀疑抛出错误的方法:

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)

1 回答

  • 0

    首先,您永远不应该将完整的服务器异常堆栈跟踪返回给客户端 . 这非常危险 . 您应该始终在数据库中记录错误 .

    其次,你不应该那样重建异常消息 . 只需拨打 .ToString() ,它就会提供您要做的所有事情

    你的问题在于

    e.InnerException.InnerException.Message
    

    如果异常没有第一级或第二级内部异常,则此调用将使用Null Ref错误输出

相关问题