小编Ale*_*bak的帖子

具有范围托管服务的实体框架

我有以下界面

internal interface IScopedProcessingService
{
    Task DoWork(CancellationToken stoppingToken);
}
Run Code Online (Sandbox Code Playgroud)

和实施

public class ConsumeScopedServiceHostedService : BackgroundService
{
    private readonly ILogger<ConsumeScopedServiceHostedService> _logger;

    public ConsumeScopedServiceHostedService(IServiceProvider services, 
        ILogger<ConsumeScopedServiceHostedService> logger)
    {
        Services = services;
        _logger = logger;
    }

    public IServiceProvider Services { get; }

    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
        _logger.LogInformation(
            "Consume Scoped Service Hosted Service running.");

        await DoWork(stoppingToken);
    }

    private async Task DoWork(CancellationToken stoppingToken)
    {
        _logger.LogInformation(
            "Consume Scoped Service Hosted Service is working.");

        using (var scope = Services.CreateScope())
        {
            var scopedProcessingService = 
                scope.ServiceProvider …
Run Code Online (Sandbox Code Playgroud)

c# entity-framework-core .net-core asp.net-core-hosted-services

3
推荐指数
1
解决办法
585
查看次数

如何为控制台应用程序设置 ASPNETCORE_ENVIRONMENT?

我有以下包含托管服务的简单控制台应用程序:

    public static async Task Main(string[] args)
    {
        using (var host = Host.CreateDefaultBuilder(args)
            .ConfigureServices((hostContext, services) =>
            {
                // db context
                services.AddEntityFrameworkNpgsql()
                    .AddDbContext<ApplicationDbContext>();

                // hosted services
                services.AddHostedService<ConsumeScopedServiceHostedService>();
                services.AddScoped<ProcessManuallySendings>();

                // services
                services.AddHttpClient<ISendPushService, SendPushService>(x
                    =>
                {
                    x.Timeout = TimeSpan.FromSeconds(65);
                });
            })
            .Build())
        {
            // Start the host
            await host.StartAsync();

            // Wait for the host to shutdown
            await host.WaitForShutdownAsync();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

它适用于我的数据库,这意味着它需要连接字符串。我有三个appsettings.json文件:

在此处输入图片说明

在开发服务器上,我将使用Development环境,在生产服务器上 - Production. 在我的本地机器上,我将使用Local. 这很简单。我将在ASPNETCORE_ENVIRONMENT(操作系统环境变量)的帮助下获得它。

我使用 Linux,在我的 shell (zsh) 配置文件中,我有:

在此处输入图片说明

当我输入终端时,$ …

c# .net-core asp.net-core-hosted-services aspnetcore-environment

3
推荐指数
2
解决办法
3693
查看次数

异步自定义视图模型验证。无法使用 .net 核心?为什么?

我正在查看此文档示例。看起来很有用。对于 rxample,我有以下视图模型:

class CustomerViewModel
{
    public string Name {get; set;}
    public List<int> Roles {get; set;}
}
Run Code Online (Sandbox Code Playgroud)

我想检查Roles我的数据库中是否都存在。我正在尝试类似的东西:

class CustomerViewModel : IValidatableObject
{
    public string Name {get; set;}
    public List<int> Roles {get; set;}

    public async Task<IEnumerable<ValidationResult>> Validate(ValidationContext validationContext)
    {
        await Task.Delay(123); /// imagine that it's database call

        // and so on
    }

}
Run Code Online (Sandbox Code Playgroud)

和惊喜!不可能。这是行不通的。因为没有异步版本的Validate( 是否可以在模型中验证我的模型?或者我必须编写单独的验证服务?

c# validation asp.net-core-mvc .net-core

3
推荐指数
1
解决办法
723
查看次数

我可以在 Blade 模板中使用外观吗?

我已经安装了软件包https://github.com/ElForastero/Transliterate。我已经完成了指南中的所有步骤。

我的aliases

'aliases' => [

    'App' => Illuminate\Support\Facades\App::class,
    'Artisan' => Illuminate\Support\Facades\Artisan::class,
    'Auth' => Illuminate\Support\Facades\Auth::class,
    'Blade' => Illuminate\Support\Facades\Blade::class,
    'Broadcast' => Illuminate\Support\Facades\Broadcast::class,
    'Bus' => Illuminate\Support\Facades\Bus::class,
    'Cache' => Illuminate\Support\Facades\Cache::class,
    'Config' => Illuminate\Support\Facades\Config::class,
    'Cookie' => Illuminate\Support\Facades\Cookie::class,
    'Crypt' => Illuminate\Support\Facades\Crypt::class,
    'DB' => Illuminate\Support\Facades\DB::class,
    'Eloquent' => Illuminate\Database\Eloquent\Model::class,
    'Event' => Illuminate\Support\Facades\Event::class,
    'File' => Illuminate\Support\Facades\File::class,
    'Gate' => Illuminate\Support\Facades\Gate::class,
    'Hash' => Illuminate\Support\Facades\Hash::class,
    'Lang' => Illuminate\Support\Facades\Lang::class,
    'Log' => Illuminate\Support\Facades\Log::class,
    'Mail' => Illuminate\Support\Facades\Mail::class,
    'Notification' => Illuminate\Support\Facades\Notification::class,
    'Password' => Illuminate\Support\Facades\Password::class,
    'Queue' => Illuminate\Support\Facades\Queue::class,
    'Redirect' => Illuminate\Support\Facades\Redirect::class,
    'Redis' => …
Run Code Online (Sandbox Code Playgroud)

laravel laravel-blade

2
推荐指数
1
解决办法
2969
查看次数

I can't run migration (with .net core 3.0 and entity framework)

My .Net Core version is 3.0.100-preview6-012264. You can ask me why I use the preview version. The main reason is useful work with GRPC (scaffolding for this). Now I would like to use entity framework in my project. I have the following .csproj:

<Project Sdk="Microsoft.NET.Sdk.Web">

    <PropertyGroup>
        <TargetFramework>netcoreapp3.0</TargetFramework>
    </PropertyGroup>

    <ItemGroup>
        <Protobuf Include="Protos\dashboard.proto" GrpcServices="Server" Generator="MSBuild:Compile" />
        <Protobuf Include="Protos\users.proto" GrpcServices="Client" Generator="MSBuild:Compile" />
        <Protobuf Include="Protos\anal.proto" GrpcServices="Client" Generator="MSBuild:Compile" />
        <Content Include="@(Protobuf)" />
        <None Remove="@(Protobuf)" />
    </ItemGroup>

    <ItemGroup>
        <PackageReference Include="DimaRabbitLogger" Version="1.3.3" />
        <PackageReference Include="Grpc.AspNetCore.Server" …
Run Code Online (Sandbox Code Playgroud)

entity-framework-core entity-framework-migrations .net-core-3.0

2
推荐指数
1
解决办法
4034
查看次数