简体   繁体   English

使用Visual Studio 2017进行调试时,“请求在此上下文中不可用”

[英]“Request is not available in this context” when debugging with Visual Studio 2017

I upgraded from Visual Studio 2015 to Visual Studio 2017. The following code runs when it is deployed to Azure App Services and when I debug it with Visual Studio 2015. But when debugging in Visual Studio 2017, it throws the exception: 我从Visual Studio 2015升级到Visual Studio2017。当将其部署到Azure App Services以及使用Visual Studio 2015对其进行调试时,将运行以下代码。但是在Visual Studio 2017中进行调试时,它将引发异常:

Request is not available in this context 在这种情况下请求不可用

For VS2015 and VS2017 IIS Express is used for debugging. 对于VS2015和VS2017,IIS Express用于调试。 Now with VS2017 IIS 10.0 Express (10.0.1737) is installed, I do not remember which version was used with VS2015. 现在安装了VS2017 IIS 10.0 Express(10.0.1737),我不记得VS2015使用哪个版本。

The code is C# based on the ASP.NET 4.5.2 Azure API App template. 该代码是基于ASP.NET 4.5.2 Azure API App模板的C#。 It registers a receiver for subscribing events from Azure Event Hub. 它注册了一个接收器,用于从Azure Event Hub订阅事件。

using System;
using System.Web.Http;
using System.Web.Http.Cors;
using System.Configuration;
using Microsoft.Azure.EventHubs;
using Microsoft.Azure.EventHubs.Processor;
using Microsoft.ApplicationInsights;
using System.Collections.Generic;

namespace ....

public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services
            config.EnableCors();
            // Web API routes
            config.MapHttpAttributeRoutes();

            try
            {
                Console.WriteLine("Registering EventProcessor...");

                ...

                var eventProcessorHost = new EventProcessorHost(
                        EhEntityPath,
                        EhConsumerGroup,
                        EhConnectionString,
                        StorageConnectionString,
                        StorageContainerName
                        );

                // Registers the Event Processor Host and starts receiving messages
                eventProcessorHost.RegisterEventProcessorAsync<Helper.EventProcessor>(); 
            }
            catch (Exception ex)
...

The exception is thrown at the line 抛出异常

eventProcessorHost.RegisterEventProcessorAsync<Helper.EventProcessor>(); 

But this issue seems not to be related to Azure Event Hub. 但是,此问题似乎与Azure事件中心无关。 Because I get this exception also in global.asax.cs when trying to create an Application Insights event: 因为在尝试创建Application Insights事件时,我也在global.asax.cs也遇到了此异常:

new TelemetryClient().TrackEvent("Application started");

I know there are some other posts regarding the exception "Request is not available in this context" but this seems to be a special behavior of VS2017. 我知道还有其他一些关于“请求在这种情况下不可用”的例外的帖子,但这似乎是VS2017的特殊行为。

My local environment is: 我的本地环境是:

Microsoft Visual Studio Enterprise 2017 
Version 15.2 (26430.16) Release
VisualStudio.15.Release/15.2.0+26430.16
Microsoft .NET Framework
Version 4.7.02046

Installed Version: Enterprise

Visual C# 2017   00369-60000-00001-AA377
Microsoft Visual C# 2017

Application Insights Tools for Visual Studio Package   8.6.00404.2
Application Insights Tools for Visual Studio

ASP.NET and Web Tools 2017   15.0.30503.0
ASP.NET and Web Tools 2017

ASP.NET Web Frameworks and Tools 2017   5.2.50303.0

Azure App Service Tools v3.0.0   15.0.30209.0
Azure App Service Tools v3.0.0

Common Azure Tools   1.9

According to your description, I created the ASP.NET 4.5.2 Azure API App application on my side and debugged it on VS2015 (Enterprise, Version 14.0.25431.01 Update 3) and VS2017 (Enterprise Preview, Version 15.3.0 Preview 7.1) with IIS Express (10.0.14358.100), both could debug successfully and the custom events and metrics could be sent to my application insights. 根据您的描述,我一边创建了ASP.NET 4.5.2 Azure API App应用程序,然后使用以下命令在VS2015(企业版14.0.25431.01更新3)和VS2017(企业预览版15.3.0预览版7.1)上对其进行了调试IIS Express(10.0.14358.100),都可以成功调试,并且可以将自定义事件和指标发送给我的应用程序见解。

Based on your issue, I would suggest you starting your application via VS without debugging to narrow this issue or you could use the cmd.exe and run iisexpress /path:"c:\\myapp\\" /port:80 to check whether your application could work as expected. 根据您的问题,我建议您通过VS启动应用程序而不进行调试来缩小此问题,或者您可以使用cmd.exe并运行iisexpress /path:"c:\\myapp\\" /port:80来检查您的应用程序是否可以按预期工作。

I deinstalled VS2017 with all components and reinstalled the latest Version 15.3.3. 我卸载了VS2017的所有组件,然后重新安装了最新的版本15.3.3。 Now the code runs. 现在代码开始运行。 No idea what went wrong with the first installation. 不知道第一次安装出了什么问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM