.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), 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);
}
}
}