问题描述
.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一直在抱怨:
有关如何修复的任何想法。
这是到目前为止我在启动中所拥有的:
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),s.Length);
return Task.Fromresult(0);
}
private Task AuthenticationTokenValidated(TokenValidatedContext arg)
{
// For debugging purposes only!
var s = $"AuthenticationTokenValidated: {arg.Result}";
return Task.Fromresult(0);
}
}
}
解决方法
您正在使用System.Configuration命名空间,这会导致歧义。
我建议删除使用System.Configuration。并尝试为配置指定完全限定名称。如果您已经在项目中添加了所有必需的引用,Visual Studio会建议可能的候选对象(在要限定的班级名称上按Ctrl。)。
,如果您有
using System;
然后C#编译器与Configuration
混淆,并认为它引用了命名空间System.Configuration
。您可以使用显式命名空间Jaeger
解决此问题:
var config = new Jaeger.Configuration(
context.HostingEnvironment.ApplicationName,loggerFactory);