【问题标题】:"Configuration config = new Configuration": 'Configuration' is a namespace but is used like a type"Configuration config = new Configuration": 'Configuration' 是一个命名空间,但用作类型
【发布时间】:2021-01-18 07:53:48
【问题描述】:

对 .net 非常陌生,这似乎是一个简单的问题。

尝试从 Jaeger 网站获取此代码:https://ocelot.readthedocs.io/en/latest/features/tracing.html

services.AddSingleton<ITracer>(sp =>
{
    var loggerFactory = sp.GetService<ILoggerFactory>();
    Configuration config = new Configuration(context.HostingEnvironment.ApplicationName, loggerFactory);

    var tracer = config.GetTracer();
    GlobalTracer.Register(tracer);
    return tracer;
});

services
    .AddOcelot()
    .AddOpenTracing();

在ConfigureServices 下的startup.cs 文件中。我已经添加了正确的引用,但出现以下错误:

Configuration config = new Configuration(context.HostingEnvironment.ApplicationName, loggerFactory);

Visual Studio 一直在抱怨:

'Configuration' 是一个命名空间,但用作类型

关于如何修复的任何想法。

到目前为止,这是我在启动时所拥有的:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Configuration;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Ocelot;
using Ocelot.DependencyInjection;
using Ocelot.Middleware;
using Ocelot.Provider.Consul;
using Microsoft.Identity.Web;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.Identity.Web.TokenCacheProviders.InMemory;
using System.Text;
using Microsoft.IdentityModel.Tokens;
using Microsoft.OpenApi.Models;
using Ocelot.Logging;
using OpenTracing.Noop;
using OpenTracing.Propagation;
using OpenTracing.Util;
using Ocelot.Tracing.OpenTracing;

namespace Ocelot.Demo1
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            _Configuration = configuration;
        }

        public ILoggerFactory _loggerFactory;

        public IConfiguration _Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {

            services.AddCors(options =>
            {
                options.AddDefaultPolicy(
                    builder =>
                    {
                        // builder.WithOrigins("http://localhost:5200");
                        builder.WithOrigins("http://localhost:5200")
                    .AllowAnyMethod()
                    .AllowAnyHeader()
                    .AllowCredentials();
                    });
            });

            // Azure AD
            services.Configure<JwtBearerOptions>(JwtBearerDefaults.AuthenticationScheme, jwtOptions => {
                jwtOptions.Audience = "RANDOM TOKEN VALUE";
                jwtOptions.Authority = "https://login.microsoftonline.com/YOUR_MS_DOMIAN/";
                jwtOptions.RequireHttpsMetadata = false;
                jwtOptions.Events = new JwtBearerEvents { OnAuthenticationFailed = AuthenticationFailed, OnTokenValidated = AuthenticationTokenValidated };
                jwtOptions.TokenValidationParameters.ValidIssuer = "https://login.microsoftonline.com/RANDOM TOKEN VALUE/v2.0";
            });


            services.Configure<JwtBearerOptions>(JwtBearerDefaults.AuthenticationScheme, jwtOptions => {
                jwtOptions.Events = new JwtBearerEvents { OnAuthenticationFailed = AuthenticationFailed, OnTokenValidated = AuthenticationTokenValidated };
            });

            services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer();
            //.AddMicrosoftIdentityWebApi(Configuration,"AzureAD");
            // services.AddProtectedWebApi(Configuration).AddProtectedApiCallsWebApis(Configuration).AddInMemoryTokenCaches();


            services.AddSingleton<ITracer>(sp =>
            {
                var loggerFactory = sp.GetService<ILoggerFactory>();
                Configuration config = new Configuration("Ocelot API Gateway", loggerFactory);

                var tracer = config.GetTracer();
                GlobalTracer.Register(tracer);
                return tracer;
            });


            // Call Ocelot
            services.AddOcelot(_Configuration)
            .AddOpenTracing();
            services.AddControllers();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseHttpsRedirection();
            app.UseRouting();

            app.UseCors();

            app.UseAuthorization();
            app.UseAuthentication();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
            app.UseOcelot().Wait();

        }

        private Task AuthenticationFailed(AuthenticationFailedContext arg)
        {
            // For debugging purposes only!
            var s = $"AuthenticationFailed: {arg.Exception.Message}";
            arg.Response.ContentLength = s.Length;
            //arg.Response.Body.Write(System.Text.Encoding.UTF8.GetBytes(s), 0, s.Length);
            return Task.FromResult(0);
        }

        private Task AuthenticationTokenValidated(TokenValidatedContext arg)
        {
            // For debugging purposes only!
            var s = $"AuthenticationTokenValidated: {arg.Result}";
            return Task.FromResult(0);
        }
    }
}

【问题讨论】:

    标签: c# visual-studio .net-core jaeger ocelot


    【解决方案1】:

    您正在使用 System.Configuration 命名空间,这会导致歧义。

    我建议删除 using System.Configuration。并尝试为配置指定完全限定名称。如果您已经在项目中添加了所有必需的引用,Visual Studio 会建议可能的候选人(在您想要限定的类名称上按 Ctrl 键)。

    【讨论】:

    • 我删除了,还是一样的问题
    【解决方案2】:

    如果你有

    using System;
    

    然后 C# 编译器会与 Configuration 混淆,并认为它指的是命名空间 System.Configuration。你可以通过使用显式命名空间Jaeger来解决它:

    var config = new Jaeger.Configuration(
        context.HostingEnvironment.ApplicationName, loggerFactory);
    

    【讨论】:

    • 当我将其更改为以下代码时,我得到:无法将类型“OpenTracing.ITracer”隐式转换为“Ocelot.Logging.ITracer”。存在显式转换(您是否缺少演员表?)
    • 我不知道这些库,但它们似乎是两种不同的类型,它们碰巧有相同的名称(同时位于不同的命名空间中)。如果你想混合两个不兼容的库,你可以通过为一个 ITracer 实现另一个 ITracer 编写一个包装器来解决这个问题。
    • 有什么想法吗?
    猜你喜欢
    • 1970-01-01
    • 2023-03-19
    • 2011-03-19
    • 2017-11-11
    • 1970-01-01
    • 2013-07-11
    • 2020-12-31
    • 2014-07-23
    • 1970-01-01
    相关资源
    最近更新 更多