在创建变量后,变量在当前上下文中不存在

时间:2016-04-06 09:54:46

标签: c# dnx

我有以下课程:

/// <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);上时,我确实知道该变量在当前上下文中不存在。

直接在监视窗口中创建变量实例时可以正常工作。

任何人都有线索?

P.S。这是我在xUnit中测试的DNX项目。代码在“调试”模式下运行。

1 个答案:

答案 0 :(得分:0)

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

此问题的另一个出现在wcf服务客户端中。创建一个新的服务客户端Client并尝试在观察窗口中显示client.InnerChannel。它不会起作用。但是,您可以创建一个临时对象(bool,string等)并将所需的值写入其中以查看您的值。

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

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