Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 调用外部webapi会导致nullreference异常。如果代码本身在项目中,则该代码可以工作_C#_Asp.net_Asp.net Web Api - Fatal编程技术网

C# 调用外部webapi会导致nullreference异常。如果代码本身在项目中,则该代码可以工作

C# 调用外部webapi会导致nullreference异常。如果代码本身在项目中,则该代码可以工作,c#,asp.net,asp.net-web-api,C#,Asp.net,Asp.net Web Api,我有这个代码在它自己的应用程序中工作。但是如果我在最终将在中结束的项目中运行此代码,我会得到一个异常。这是相同的堆栈跟踪 发生未处理的异常,进程终止 申请编号:28507bb0 进程ID:14936 异常:System.NullReferenceException 消息:对象引用未设置为对象的实例 StackTrace:at System.Web.ThreadContext.AssociateWithCurrentThread(布尔setImpersonationContext) 位于Syste

我有这个代码在它自己的应用程序中工作。但是如果我在最终将在中结束的项目中运行此代码,我会得到一个异常。这是相同的堆栈跟踪

发生未处理的异常,进程终止

申请编号:28507bb0

进程ID:14936

异常:System.NullReferenceException

消息:对象引用未设置为对象的实例

StackTrace:at System.Web.ThreadContext.AssociateWithCurrentThread(布尔setImpersonationContext) 位于System.Web.HttpApplication.OnThreadEnterPrivate(布尔setImpersonationContext) 位于System.Web.LegacyAspNetSynchronizationContext.CallbackPossiblyUnderlock(SendOrPostCallback回调,对象状态) 位于System.Web.LegacySpNetSynchronizationContext.CallCallback(SendOrPostCallback回调,对象状态) 位于System.Web.LegacySpNetSynchronizationContext.Post(SendOrPostCallback,对象状态) 位于System.Threading.Tasks.SynchronizationContextAwaitTaskContinuation.PostAction(对象状态) 位于System.Threading.Tasks.AwaitTaskContinuation.RunCallback(ContextCallback回调、对象状态、任务和当前任务) ---来自引发异常的上一个位置的堆栈结束跟踪--- 在System.Threading.Tasks.AwaitTaskContinuation.b__1(对象s)中 位于System.Threading.QueueUserWorkItemCallback.WaitCallback_上下文(对象状态) 位于System.Threading.ExecutionContext.RunInternal(ExecutionContext ExecutionContext、ContextCallback回调、对象状态、布尔值preserveSyncCtx) 在System.Threading.ExecutionContext.Run(ExecutionContext ExecutionContext,ContextCallback回调,对象状态,布尔保存SyncCTX) 位于System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()处 在System.Threading.ThreadPoolWorkQueue.Dispatch()中 在System.Threading.\u ThreadPoolWaitCallback.PerformWaitCallback()中

旧的应用程序是.net 4.0,我在VS 2012中打开了它,并将该解决方案中的每个项目升级到4.5。这一错误没有改变

你知道为什么代码可以独立工作,但会在其他应用程序中抛出这个stacktrace吗

    private async Task<IList<InventoryItem>> GetPartsInfoAsync(string SearchText)
    {
        HttpClient client = new HttpClient();
        IList<InventoryItem> partList = new List<InventoryItem>();
        string address = ConfigurationManager.AppSettings["BaseUrl"] + ConfigurationManager.AppSettings["PartsSearch"] + "?login=" + ConfigurationManager.AppSettings["Login"] + "&apikey=" + ConfigurationManager.AppSettings["apiKey"] + "&search_token=" + SearchText + "&useExact=true";

        try
        {
            HttpResponseMessage responseMessage = await client.GetAsync(address); 
            responseMessage.EnsureSuccessStatusCode();
            //client.Timeout = System.TimeSpan.FromMilliseconds(50);

            Parts.RootObject ArrowPartData = JsonConvert.DeserializeObject<Parts.RootObject>(await responseMessage.Content.ReadAsStringAsync());

            if (ArrowPartData != null)
            {

            }
        }
        catch (HttpRequestException requestException)
        {
                        throw requestException;
        }
        catch (Exception ex)
        {
            throw ex;
        }
        return partList;
    }
专用异步任务GetPartsInfoAsync(字符串搜索文本)
{
HttpClient=新的HttpClient();
IList partList=新列表();
字符串地址=ConfigurationManager.AppSettings[“BaseUrl”]+ConfigurationManager.AppSettings[“PartsSearch”]+“?login=“+ConfigurationManager.AppSettings[“login”]+”&apikey=“+ConfigurationManager.AppSettings[“apikey”]+”&search_-token=“+SearchText+”&useExact=true”;
尝试
{
HttpResponseMessage responseMessage=await client.GetAsync(地址);
responseMessage.EnsureAccessStatusCode();
//client.Timeout=System.TimeSpan.fromMillistics(50);
Parts.RootObject ArrowPartData=JsonConvert.DeserializeObject(wait responseMessage.Content.ReadAsStringAsync());
if(ArrowPartData!=null)
{
}
}
捕获(HttpRequestException请求异常)
{
抛出请求异常;
}
捕获(例外情况除外)
{
掷骰子;
}
返回部件列表;
}

这看起来与你的上一个问题非常相似。@gunr217-是的,但没有人会犹豫,我不确定是否有人能看到我的上一个问题。我还想进一步澄清,这样我就可以得到一个基于清晰而不是混乱的答案。只有你可以通过编辑你的文章,使其符合本网站的规则,使其“暂停”。然后,您可以添加更多信息来帮助澄清。@gunr217我确实更改并澄清了我的帖子,并发布了要求的更多信息,以便我的问题更清楚。我还需要做些什么来解除暂停?阅读了解更多信息: