【问题标题】:Variable does not exist in current context directly after creating it变量在创建后不直接存在于当前上下文中
【发布时间】:2016-04-06 18:39:10
【问题描述】:

我有以下课程:

/// <summary>
///     Represents an implementation of the <see cref="IAspNetCoreLoggingConfigurationBuilder"/> to configure the ASP.NET Core Logging.
/// </summary>
public class AspNetCoreLoggingConfigurationBuilder : IAspNetCoreLoggingConfigurationBuilder
{
    #region Properties

    /// <summary>
    ///     Gets the <see cref="ILogSource"/> that's used to write log entries.
    /// </summary>
    public ILogSource LogSource{ get; private set; }

    #endregion

    #region IAspNetCoreLoggingConfigurationBuilder Members

    /// <summary>
    ///     Sets the log source that should be used to save log entries.
    /// </summary>
    /// <param name="logSource">The source </param>
    public void SetLogSource(ILogSource logSource)
    {
        LogSource = logSource;
    }

    #endregion
}

我还有一个创建此类实例的方法:

/// <summary>
///     Adds logging to the <see cref="IApplicationBuilder"/> request execution pipeline.
/// </summary>
/// <param name="app">The <see cref="IApplicationBuilder"/> to configure the application's request pipeline.</param>
/// <param name="configuration">Builder used to configure the ASP.NET Core Logging.</param>
/// <returns>A reference to this instance after the operation has completed.</returns>
public static IApplicationBuilder UseAspNetCoreLogging(this IApplicationBuilder app, Action<IAspNetCoreLoggingConfigurationBuilder> configuration)
{
    var aspNetLoggerConfiguration = new AspNetCoreLoggingConfigurationBuilder();

    configuration(aspNetLoggerConfiguration);

    // Add the registered ILogSource into the registered services.
    _services.AddInstance(typeof (ILogSource), aspNetLoggerConfiguration.LogSource);

    // The entire configuration for the middleware has been done, so return the middleware.
    return app.UseMiddleware<AspNetCoreLoggingMiddleware>();
}

注意这里的第一行,我正在创建一个类的实例。 但是,当我在手表中检查此变量时,当我的光标位于 configuration(aspNetLoggerConfiguration); 行时,我确实知道该变量在当前上下文中不存在。

直接在监视窗口中创建变量的实例确实有效。

有人知道吗?

附:这是我在 xUnit 中测试的 DNX 项目。代码正在“调试”模式下运行。

【问题讨论】:

标签: c# dnx


【解决方案1】:

没有运行时也没有编译错误。 这是 Visual Studio 无法在调试窗口中显示对象的问题,因为它是运行时对象(类似的东西)。

此问题的另一个发生是在 wcf-service 客户端中。创建一个新的服务客户端Client 并尝试在监视窗口中显示client.InnerChannel。它行不通。但是,您可以创建一个临时对象(布尔、字符串等)并将所需的值写入其中以查看您的值。

#if DEBUG
    var tmpLog = aspNetLoggerConfiguration.LogSource;
#endif

当您的鼠标悬停在 tmpLog 上时,您应该会在 LogSource 中看到它。

【讨论】:

  • 我刚刚在定义之后添加了上面的行,我确实得到“tmpLog 在当前上下文中不存在。
  • 用于测试。我将这些行添加为 xUnit 单元测试中的第一行:var loggingConfiguration = new AspNetCoreLoggingConfigurationBuilder(); var demo = loggingConfiguration;。你猜怎么着,这两个属性都是空的。这真的让我发疯了。如果我在AspNetCoreLoggingConfigurationBuilder中放置一个空的构造函数,代码被执行,我是在构造函数中单步执行,然而,当返回时,值为null。
  • 从你告诉我的情况来看,我只能猜测可能是另一个可能的问题,但老实说,我不知道你的程序为什么会这样。
  • 我认为这与我的应用程序无关。在 Visual Studio 中,我创建了一个新的 ASP.NET 5 网站(因此是 DNX 项目),我在“配置”中添加了以下行。 var demo = new Demo(); 其中Deom 是anmpty 类。但是,运行时,demo 为空。
猜你喜欢
  • 2023-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多