我试图弄清楚如何使用主机构建器模式来运行控制台应用程序(而不是 Windows 服务)。目的是保持流程与 WebApi 相似,以保持开发实践相似。我见过使用 HostedService 或 BackGroundService 的示例,他们希望将其作为 Windows 服务运行。但是,如果我想运行一个简单的控制台应用程序,我在哪里指定我的入口点类和方法?从 hostbuilder.Build() 中,我可以看到 Run() 和 RunAsync() 方法。但我无法弄清楚它会执行什么?
我已经看到了其他示例,您可以在其中创建 servicecollection,然后使用 serviceprovider.GetService().SomeMethod() 来启动该过程。但这有点偏离我们想要做的事情。所以请建议如何指定启动过程。我们正在使用 3.1 .Net Core。
class Program
{
static async void Main(string[] args)
{
var host = CreateHostBuilder(args).Build();
await host.RunAsync();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureServices((hostBuilderContext, serviceCollection) => new Startup(hostBuilderContext.Configuration).ConfigureServices(serviceCollection))
.UseSerilog()
;
}
Run Code Online (Sandbox Code Playgroud) 我正在关注https://dotnet.microsoft.com/learn/dotnet/hello-world-tutorial/create
\n当我尝试创建项目时,我得到以下信息:
\noskar@oskarslaptop:~/Programming/Resorvoir-CLI$ dotnet new console -o MyApp\nSegmentation fault (core dumped)\nRun Code Online (Sandbox Code Playgroud)\n我已经使用以下命令通过 snap 安装了 dotnet 5.0:
\nsudo snap install dotnet-sdk --classic --channel=5.0\nsudo snap alias dotnet-sdk.dotnet dotnet\nRun Code Online (Sandbox Code Playgroud)\n快照信息:
\noskar@oskarslaptop:~/Programming/Resorvoir-CLI$ sudo snap info dotnet-sdk \nname: dotnet-sdk\nsummary: Develop high performance applications in less time, on any platform.\npublisher: Microsoft .NET Core (dotnetcore\xe2\x9c\x93)\nstore-url: https://snapcraft.io/dotnet-sdk\ncontact: https://dot.net/core\nlicense: unset\ndescription: |\n .NET Core is the modular and high performance implementation of .NET for creating web applications\n and services that run on Windows, …Run Code Online (Sandbox Code Playgroud) 我有一个 ASP.NET Core Web API,其中使用 Swashbuckle 集成了 Swagger。我已经使用操作过滤器成功在 Swagger UI 上集成了授权,因为我不想显示匿名 API 的挂锁。
.OperationFilter<AuthorizeFilter>()
Run Code Online (Sandbox Code Playgroud)
在过滤器内,我已经注册了 Swagger UI 的基本身份验证安全要求。
我的问题是,即使在 Swagger UI 上的 API 中进行身份验证,我也不再看到单击挂锁图标时提供的漂亮的身份验证弹出窗口。
有人可以回答,为什么我现在看不到身份验证弹出窗口吗?
我是斯瓦格的新手。我正在使用 OpenAPI 3.0.2。
当我运行 Swagger UI 时,顶部和每个 API 都会出现授权按钮,但它们不起作用。当我单击它们时,我可以在 apiKey 框中输入任何文本,它会接受它并表示我已获得授权。但是没有API起作用,它们都返回401
这是Startup.ConfigureServices中的相关代码
services.AddAuthentication(
x =>
{
x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
}
)
.AddJwtBearer(
x =>
{
x.RequireHttpsMetadata = false;
x.SaveToken = true;
x.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = false,
ValidateAudience = false,
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(key),
ValidIssuer = Configuration["Tokens:Issuer"],
ValidAudience = Configuration["Tokens:Audience"],
};
}
);
services.AddSwaggerGen(
setupAction =>
{
setupAction.SwaggerDoc(
"LibraryOpenApiSpecification",
new Microsoft.OpenApi.Models.OpenApiInfo()
{
Title = "Library API",
Version = "2.0",
Description = "Text", …Run Code Online (Sandbox Code Playgroud) 我需要编写需要ConfigureServices完全执行的业务逻辑。
OnTokenValidated本身的位置很糟糕ConfigureServices。
中间件的替代方案是什么
AddOpenIdConnect,以便我可以Success完全灵活地调用以使用注入的服务?
services.AddAuthentication(options =>
{
options.DefaultScheme = "cookie";
//...
})
.AddOpenIdConnect("oidc", options =>
{
options.Authority = "https://localhost:5001";
/....
options.Scope.Add("offline_access");
options.Events.OnTokenValidated = async n =>
{
//Need other services which will only
//get injected once ConfigureServices method has fully executed.
};
}
Run Code Online (Sandbox Code Playgroud)
我是色盲。我正在尝试通过更改选项来更改 Visual Studio 社区文本编辑器的颜色。
你能告诉我在(很长的)列表中有哪些我可以改变的项目吗?
请只考虑我在 C# 中编码时考虑的那些。

我正在学习 docker 和容器。我正在尝试通过 Visual Studio 2019 在 Docker Hub 上发布我的 .Net Core 3.1 Web App。我在 Windows PC 上,但 Visual Studio 部署环境设置为 Linux,Docker 也设置为 Linux。我也安装了 WSL2(Linux 2 的 Windows 子系统)。此外,Web 应用程序运行良好,同时我可以看到容器和图像已成功创建到 Docker 仪表板(正在运行)中。但是,在尝试将其发布到 Docker Hub 时,出现以下错误。
你能帮我找出我的电脑(它是 azure windows 10 VM)上缺少哪个部分吗?
注意:我也可以毫无问题地运行 linux 容器。Ubuntu 20 也是从 Windows 商店安装的。
2021-07-01 10:19:58 AM
Microsoft.WebTools.Azure.Publish.Docker.DockerCommandException: Running the docker.exe tag command failed.
Current context "desktop-linux" is not found on the file system, please check your config file at C:\Users\kulkarnis009\AppData\Local\Temp\7961a412d485473395194d8ad39fe785\config.json
at Microsoft.WebTools.Azure.Publish.Docker.DockerOperations.ThrowDockerCommandError(String dockerCommand)
at Microsoft.WebTools.Azure.Publish.Docker.DockerOperations.<DockerTagAsync>d__3.MoveNext() …Run Code Online (Sandbox Code Playgroud) 我正在使用.NET 5.0开发一个asp.net项目;在这个项目中,我使用实体框架(代码优先)和 sql lite 文件数据库。
在startup.cs文件中,我使用以下代码以编程方式创建和更新数据库架构:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, DataContext dataContext)
{
// migrate any database changes on startup (includes initial db creation)
dataContext.Database.Migrate();
...
}
Run Code Online (Sandbox Code Playgroud)
然后我有一项服务,用于清除数据库某些表中的旧数据。它会在启动时并定期清理它们:
public class TimedDbCleanerService : IHostedService, IDisposable
{
...
}
Run Code Online (Sandbox Code Playgroud)
有了这些功能:
public Task StartAsync(CancellationToken stoppingToken)
{
_timer = new Timer(DoWork, null, TimeSpan.Zero,
TimeSpan.FromHours(_dbCleanerSettings.Hours));
return Task.CompletedTask;
}
private void DoWork(object state)
{
// create scoped dbcontext
using (var scope = _scopeFactory.CreateScope())
{
var dbContext = scope.ServiceProvider.GetRequiredService<DataContext>();
// check and remove …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 BS4 进行一些网络抓取。
到目前为止我已经提取了<a>使用
urls = [item for item in soup.select('h4 a')]
Run Code Online (Sandbox Code Playgroud)
但是,我只想获得 ID 开始哪个条目的 url。
<a href="http://www.sampleurl.com/static/welcome" id="entry_1">Lamborghini </a>
Run Code Online (Sandbox Code Playgroud)
我已经尝试过,item.id但它不起作用。
我缺少什么?
services.AddOData();
services.AddRouting();
services.AddCors(...);
Run Code Online (Sandbox Code Playgroud)
CS1920:“IServiceContainer”不包含“AddOData”的定义,并且最佳扩展方法重载“ODataMvcBuilderExtensions.AddOData(IMvcBuilder)”需要“IMvcBuilder”类型的接收器
我安装了所需的所有软件包,这里是我的项目中的软件包列表

asp.net-core ×7
c# ×4
.net-core ×2
swagger ×2
.net-5 ×1
azure ×1
colors ×1
docker ×1
dockerhub ×1
odata ×1
python ×1
swagger-ui ×1
text-editor ×1