"Configuration config = new Configuration": 'Configuration' is a namespace but is used like a type

523 views Asked by At

very new to .net and this seems like a simple questions.

Trying to get this code to work from the Jaeger website: 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();

in the startup.cs file under the ConfigureServices. I have added the proper references, but am getting an error with the:

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

Visual studio keeps complaining:

'Configuration' is a namespace but is used like a type

Any ideas on how to fix.

Here is what I have in startup so far:

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);
        }
    }
}
2

There are 2 answers

1
arif On

you are using System.Configuration namespace which causes ambiguity.

i would suggest remove the using System.Configuration. And try specifying fully qualified name for the Configuration. visual studio would suggest possible candidates (press Ctrl . on the Class name you want to qualify) provided you have added all required references in project already.

4
Olivier Jacot-Descombes On

If you have a

using System;

then the C# compiler gets confused with Configuration and thinks it refers to the namespace System.Configuration. You can solve it by using the explicit namespace Jaeger:

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