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

时间:2018-04-27 19:22:38

标签: c# .net sql-server api azure

我正在调用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):第35行   在lambda_method(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,Object [] arguments)
  在System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext,IDictionary`2 arguments,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)

1 个答案:

答案 0 :(得分:0)

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

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

你的问题在

e.InnerException.InnerException.Message

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

相关问题