标签: .net-core

如何在 F# 中为枚举指定基础类型

我们可以在 C# 中为 enum 指定一个底层类型,如下所示:

[Flags]
public enum MyKinds : ushort
{
    None  = 0,
    Flag1 = 1 << 0,
    Flag2 = 1 << 1,
    // ...
}
Run Code Online (Sandbox Code Playgroud)
  1. 我怎样才能使用 F# 做到这一点?
type MyKinds = 
    | None  = 0 
    | Flag1 = 1
    | Flag2 = 2

    // inherit ushort  // error FS0912
Run Code Online (Sandbox Code Playgroud)
  1. 如何像1 << 0F# 中那样使用按位运算符定义枚举值?
type MyKinds = 
    | None = 0 
    | Flag1 = 1 << 0    // error FS0010
    | Flag2 = 1 << 1 …
Run Code Online (Sandbox Code Playgroud)

c# f# .net-core

0
推荐指数
1
解决办法
210
查看次数

客户端在尝试执行连接时已断开连接

当尝试连接到本地运行的 Mosquitto MQTT 队列时,出现以下错误。

Unhandled exception. System.AggregateException: One or more errors occurred. (The client  has been disconnected while trying to perform the connection)
 ---> System.Net.Mqtt.MqttClientException: The client  has been disconnected while trying to perform the connection
   at System.Net.Mqtt.Sdk.MqttClientImpl.ConnectAsync(MqttClientCredentials credentials, MqttLastWill will, Boolean cleanSession)
Run Code Online (Sandbox Code Playgroud)

我在设置时使用默认选项System.Net.Mqtt.MqttClient

Unhandled exception. System.AggregateException: One or more errors occurred. (The client  has been disconnected while trying to perform the connection)
 ---> System.Net.Mqtt.MqttClientException: The client  has been disconnected while trying to perform the connection
   at System.Net.Mqtt.Sdk.MqttClientImpl.ConnectAsync(MqttClientCredentials …
Run Code Online (Sandbox Code Playgroud)

c# mqtt mosquitto .net-core

0
推荐指数
1
解决办法
1103
查看次数

从字符串转换日期和/或时间时转换失败 [XAF ListViewFilter]

我使用 XAF 21.2.5 从框架升级到 .NetCore

现在,如果我尝试使用以下 ListViewFilter

 [ListViewFilter("Today",
        "[Created] >= LocalDateTimeToday()",
        "Today", true, Index = 0)]
Run Code Online (Sandbox Code Playgroud)

在我的具有属性的业务对象上

public DateTime Created { get; set; }
Run Code Online (Sandbox Code Playgroud)

我收到一个错误

  Microsoft.Data.SqlClient.SqlException
  HResult=0x80131904
  Message=Conversion failed when converting date and/or time from character string.
  Source=Core Microsoft SqlClient Data Provider
  StackTrace:
   at Microsoft.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
Run Code Online (Sandbox Code Playgroud)

文档表明这应该有效

[更新]

读完这个问题后我开始尝试

modelBuilder.Entity<MyEntity>().Property(x => x.Created).HasColumnType("datetime");
Run Code Online (Sandbox Code Playgroud)

我正在寻找可为空的属性,以便映射所有日期时间属性。

xaf entity-framework-core .net-core

0
推荐指数
1
解决办法
2055
查看次数

aks 集群上的 Azure 持久功能不起作用

我创建了一个azure持久函数并将其放入docker容器中,一切都在docker桌面中运行,azure持久函数正在使用和MQ兔子触发器。这是代码:

        public async Task<List<string>> RunOrchestrator(
            [OrchestrationTrigger] IDurableOrchestrationContext context
            )
        {
            var job = context.GetInput<Job>();
            var outputs = new List<string>();

            try
            {
                job.JobCreationIdResult = await context.CallActivityAsync<string>("JobExecutor_CreateJobSimple", job);
            }
            catch (Exception ex)
            {
                _logger.ForContext<JobExecutorSimple>().Error(ex.Message, ex);                
                return outputs;
            }

            return outputs;
        }

        [FunctionName("JobExecutor_CreateJobSimple")]
        public async Task<string> CreateJob([ActivityTrigger] Job job)
        {
            _logger.ForContext<JobExecutorSimple>().Information($"result JobId: {job.MktJobId}");
            return "done";
        }
        
        [FunctionName("JobExecutor_RabbitMQStartSimple")]        
        public async Task RabbitMQStart(
            [RabbitMQTrigger("mkt-executor-q-local", ConnectionStringSetting = "mkt-Executor-RabbitMqConnection")] Job job,
            [DurableClient] IDurableOrchestrationClient starter)
        {            
            string instanceId = await starter.StartNewAsync("JobExecutorSimple", job);
            _logger.ForContext<JobExecutorSimple>().Information($"Started orchestration with ID = '{instanceId}'."); …
Run Code Online (Sandbox Code Playgroud)

docker .net-core azure-functions azure-aks

0
推荐指数
1
解决办法
1535
查看次数

如何使用 .NET 内置 DI 容器“向下许多级别”配置依赖项注入

我有一个控制台 .NET 核心应用程序,它使用该Microsoft.Extensions.DependencyInjection库作为依赖项注入框架。

我想使用这个框架来注入一个“向下”两个级别的依赖项,而不必在中间层多余地提及这个依赖项。我该怎么做呢?

目前,我注入依赖项的唯一方法是将其向下传递,直到需要为止。这是我的独立控制台应用程序,它演示了该要求。它是一个简单的程序,可以根据示例资产和负债金额计算一个人的净资产。(它实际上只是减去两个金额)。

Program.cs文件包含程序主入口点并注册依赖项。

程序.cs:

public class Program
{
    private static IServiceProvider _serviceProvider;
    public static void Main(string[] args)
    {
        RegisterServices();
        IServiceScope scope = _serviceProvider.CreateScope();
        scope.ServiceProvider.GetRequiredService<ConsoleApplication>().Run();
        DisposeServices();
    }

    private static void RegisterServices()
    {
        var services = new ServiceCollection();
        services.AddSingleton<ICalculator, Calculator>();
        services.AddSingleton<ConsoleApplication>();
        _serviceProvider = services.BuildServiceProvider(true);
    }

    private static void DisposeServices()
    {
        if (_serviceProvider == null)
        {
            return;
        }
        if (_serviceProvider is IDisposable)
        {
            ((IDisposable)_serviceProvider).Dispose();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

设置依赖项注入后,Program.cs运行该ConsoleApplication.cs Run方法。

ConsoleApplication.cs:

internal class …
Run Code Online (Sandbox Code Playgroud)

c# design-patterns dependency-injection di-containers .net-core

0
推荐指数
1
解决办法
905
查看次数

在获取 Count 之前检查 List 是否为 Null 会导致 -&gt; Cannot implicitly conversion type 'int?' 到'int

我正在尝试查找列表中存在的项目数。为了防止 null 异常。我使用运算符在获取其计数之前?检查是否 myBenchmarkMappings为 null。

 int benchmarkAccountCount =portfolioWrapper.myBenchmarkMappings?.Count;
Run Code Online (Sandbox Code Playgroud)

但这会导致Cannot implicitly convert type 'int?' to 'int'.异常

我究竟做错了什么 ?

c# .net-core asp.net-core

0
推荐指数
1
解决办法
493
查看次数

在 C#/dotnet 中将可空变量转换为不可空变量

这听起来微不足道,但我面临挑战。我无法将可为空值转换为不可为空值:

if (caseObj.SyncDate != null)
    caseDTO.SyncDate = DateTimeHelper.getFormattedDateTime(caseObj.SyncDate);
Run Code Online (Sandbox Code Playgroud)

我的 caseObj.SyncDate 定义为:

public DateTimeOffset? SyncDate {get;set;}
Run Code Online (Sandbox Code Playgroud)

我的 getFormattedDateTime 是:

    public static string getFormattedDateTime(DateTimeOffset dateTimeOffset) {

        string returnDate = null;

        if (dateTimeOffset != null) {
            returnDate = dateTimeOffset.ToString("yyyy-MM-dd HH:mm:ss.fffzzz", CultureInfo.InvariantCulture);
        }
        return returnDate;
    }
Run Code Online (Sandbox Code Playgroud)

我读过这篇文章并尝试过:

        if (caseObj.SyncDate != null)
            caseDTO.SyncDate = DateTimeHelper.getFormattedDateTime(caseObj.SyncDate!):
Run Code Online (Sandbox Code Playgroud)

这看起来很奇怪,因为我看到了Microsoft 文档的链接,其中指出:

有时,当您知道变量不为 null,但编译器确定其 null 状态为 Maybe-null 时,您必须覆盖警告。您使用了 null-forgiving 运算符!跟随变量名称以强制空状态为非空。例如,如果您知道 name 变量不为 null,但编译器发出警告,则可以编写以下代码来覆盖编译器的分析:

但这似乎不起作用。我已经阅读了这篇文章(我认为这篇文章足够集中),而这篇文章的标记解决方案不是从可为空转换为空。

我也尝试过这个:

        if (caseObj.SyncDate != null) {
            DateTimeOffset dto …
Run Code Online (Sandbox Code Playgroud)

c# .net-core

0
推荐指数
1
解决办法
6736
查看次数

Dapper 无法正确映射使用 LEFT JOIN (splitOn) 选择的行

我有两个模型供应商和用户。每个用户可能只有一个供应商,也可能没有。供应商可能有多个用户。

楷模:

public class AppUser
{
    public int Id { get; set; }
    public string? FullName { get; set; }
    public string? Position { get; set; }
    public int StatusId { get; set; }
    public int VendorId { get; set; }
}


public class Vendor
{
    public int VendorId { get; set; }
    public string? VendorCode { get; set; }
    public string? VendorName { get; set; }
    public int StatusId { get; set; }
    public List<AppUser> CompanyUsers { get; set; …
Run Code Online (Sandbox Code Playgroud)

.net c# dapper .net-core

0
推荐指数
1
解决办法
910
查看次数

如何避免在.NET Core中的异步调用中释放上下文

我的 api 中有一些缓慢的调用,因此为了不阻塞我的 UI,我按照本教程实现了一个后台工作服务。在我里面_backgroundWorkerQueue我有

_backgroundWorkerQueue.QueueBackgroundWorkItem(async token =>
{
    await client.ExecuteAsync(request, CancellationToken.None);
    await _projectRepository.Update(id, "Update", "unlock");
});
Run Code Online (Sandbox Code Playgroud)

第二行,await _projectRepository.Update向我抛出一个错误,表明上下文已被处理并且更新失败。我将服务设置为瞬态,并将上下文设置为瞬态,以便以这种方式进行测试,但我仍然遇到相同的错误。如果可能的话,在不使用 Hangfire 等其他库的情况下,如何避免和解决这个问题的任何想法和想法。

c# backgroundworker async-await .net-core

0
推荐指数
1
解决办法
1157
查看次数

如何在 EF core 中使用 String.Split()

我在数据库中有一个名称列表,我想从某个字段中过滤这些名称。但它必须匹配单词的开头,而不是中间。

假设我在数据库中有这个:

"foobar"
"bar foo"
"bar fos foo"
"barfoo"
"bar basfoo"
Run Code Online (Sandbox Code Playgroud)

如果我输入“foo”,我应该得到:

"foobar"
"bar foo"
"bar fos foo"
Run Code Online (Sandbox Code Playgroud)

我已经尝试过这个:

"foobar"
"bar foo"
"bar fos foo"
"barfoo"
"bar basfoo"
Run Code Online (Sandbox Code Playgroud)

但显然 .Split() 无法转换为 SQL。我也不能使用 .IndexOf() 和 .Substring() 的组合,因为名称可以有多个应该拆分的位置。

我不想为此切换到客户端评估。

有什么方法可以将 Split() 转换为 SQL 吗?

c# linq entity-framework-core .net-core

0
推荐指数
1
解决办法
2378
查看次数