小编BKS*_*BKS的帖子

尽管有 4.8.1 最新版本可用,为什么仍推荐使用 .NET 4.8 版本

查看可用的 .NET 框架,4.8.1这是最新的。不过4.8recommended微软这边的。我很好奇这是为什么。是4.8.1测试版还是什么?我找不到任何相关信息。

在此输入图像描述

.net .net-4.8

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

在.NET 4中使用等待SemaphoreSlim.WaitAsync

我的应用程序使用的是.NET 4.我正在使用nuget包等待异步

在我的应用程序中,我想对sempahore WaitAsync调用进行等待,如下所示.

SemaphoreSlim semphore = new SemaphoreSlim(100);
await semphore.WaitAsync();
Run Code Online (Sandbox Code Playgroud)

但是我遇到了编译错误.

'System.Threading.SemaphoreSlim'不包含'WaitAsync'的定义,并且没有可以找到接受类型'System.Threading.SemaphoreSlim'的第一个参数的扩展方法'WaitAsync'(您是否缺少using指令或程序集引用?)

无论如何在.NET 4.0中都可以使用uisng WaitAsync吗?

c# semaphore .net-4.0 async-await

5
推荐指数
1
解决办法
5477
查看次数

在 Dictionary 和 ConcurrentDictionary 之间修改集合时的不同行为

使用下面列出的普通字典代码,我得到了例外

集合被修改;枚举操作可能无法执行。

Dictionary<int, int> dict2 = new Dictionary<int, int>();
dict2.Add(1, 10);
dict2.Add(2, 20);
dict2.Add(3, 30);
dict2.Add(4, 40);

foreach (var d in dict2)
{
    if (dict2.ContainsKey(2))
        dict2.Remove(2);

    if (dict2.ContainsKey(3))
        dict2.Remove(3);
}
Run Code Online (Sandbox Code Playgroud)

但是对于 ConcurrentDictionary,这可以正常工作。

ConcurrentDictionary<int, int> dict1 = new ConcurrentDictionary<int, int>();
dict1.AddOrUpdate(1, 10, (k,v)=> 10);
dict1.AddOrUpdate(2, 20, (k, v) => 20);
dict1.AddOrUpdate(3, 30, (k,v)=> 30);
dict1.AddOrUpdate(4, 40, (k,v)=> 40);

foreach (var d in dict1)
{
    int x;
    if (dict1.ContainsKey(2))
        dict1.TryRemove(2, out x);

    if (dict1.ContainsKey(3))
        dict1.TryRemove(3, out x);
}
Run Code Online (Sandbox Code Playgroud)

为什么会有行为差异?

c# dictionary enumeration .net-4.0 concurrentdictionary

4
推荐指数
1
解决办法
1340
查看次数

Parallel.For不使用我的主线程

在我的应用程序中,我希望我的主线程不被其他任何东西使用.我必须做一些并行处理,我想通过不同的线程来完成.为此,我使用Parallel.For如下

static void SomeMethod()
{
    Console.WriteLine(string.Format("Main Thread ID  before parallel loop ->>>>>>> {0} ", System.Threading.Thread.CurrentThread.ManagedThreadId));
    Parallel.For(0, 10, i =>
    {
        Console.WriteLine(string.Format("Output ->>>>>>> {0} ", System.Threading.Thread.CurrentThread.ManagedThreadId));
    }); 
    Thread.Sleep(100);
    Console.WriteLine(string.Format("Main Thread ID  after parallel loop ->>>>>>> {0} ", System.Threading.Thread.CurrentThread.ManagedThreadId));
}
Run Code Online (Sandbox Code Playgroud)

从输出主线程可以看出使用ThreadID 1和Parallel.For中的一些线程也使用相同的线程.

Main Thread ID  before parallel loop ->>>>>>> 1
Output ->>>>>>> 1
Output ->>>>>>> 1
Output ->>>>>>> 3
Output ->>>>>>> 4
Output ->>>>>>> 4
Output ->>>>>>> 4
Output ->>>>>>> 4
Output ->>>>>>> 5
Output ->>>>>>> 3
Output ->>>>>>> 1
Main Thread …
Run Code Online (Sandbox Code Playgroud)

c# task-parallel-library parallel.foreach

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

如何从dbcontext获取当前事务

在Entity Framework 6中,在DbContext类下,您可以通过context.Database.CurrentTransaction获取当前事务.但是,这个API在EF.Core中似乎不可用.如何从EF.Core中的dbcontext检索当前事务对象?

entity-framework-core .net-core

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

对?的行为感到困惑。算子

这是我的代码

class Address
{
    public bool IsAppartment { get; set; }
}

class Employee
{
    public string Name { get; set; }
    public Address Address { get; set; }
}    
class Program
{
    static void Main(string[] args)
    {
        Employee employee = new Employee()
        {
            Name = "Charlie"
        };
        if (employee.Address?.IsAppartment ?? true)
        {
            Console.WriteLine("Its an apartment");
        }
        else
        {
            Console.WriteLine("No employee address or not an apartment");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

该程序的输出

它是一个公寓

根据的定义算子

如果条件成员或元素访问操作链中的一个操作返回null,则该链的其余部分将不执行。

在这种情况下,Address对象为null,我不明白为什么它不在代码的else分支中出现?

更新
什么是使用快捷运算符进行跟踪的等效代码? …

.net c# .net-4.6.1

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

在运行时使用连接字符串

我使用数据库第一种方法创建了我的模型.问题是我的连接字符串在运行时更改.因此,我为DBContext类添加了一个重载的构造函数,它接受连接字符串.

public partial class MyDataContext: DbContext
    {
        public MyDataContext: ()
            : base("name=DbEntities")
        {
        }

        // added this overloaded contructor
        public MyDataContext: (string connectionString)
            : base(connectionString)
        {
        }
Run Code Online (Sandbox Code Playgroud)

我在运行时获得的连接字符串如下所示.

Integrated Security=SSPI;Data Source=localhost\sqlexpress;initial catalog=MyDatabase;Max Pool Size=100;Min Pool Size=20;Connect Timeout=15;
Run Code Online (Sandbox Code Playgroud)

然而,当我使用使用重载构造函数创建的MyDataContext运行查询时,我得到以下错误.

System.Data.Entity.Infrastructure.UnintentionalCodeFirstException:在Code First模式下使用上下文,其中包含从EDMX文件生成的用于Database First或Model First开发的代码.这将无法正常工作.要解决此问题,请不要删除引发此异常的代码行.如果您希望使用Database First或Model First,请确保Entity Framework连接字符串包含在启动项目的app.config或web.config中.如果要创建自己的DbConnection,请确保它是EntityConnection而不是其他类型的DbConnection,并将其传递给采用DbConnection的基本DbContext构造函数之一.要了解有关Code First,Database First和Model First的更多信息,请参阅此处的Entity Framework文档:http: //go.microsoft.com/fwlink/?LinkId = 394715

知道如何实现这一目标吗?

c# entity-framework-6

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

docker镜像的磁盘大小

作为一个刚刚接触 docker 世界并具有虚拟机思维的人。我从 docker hub 下载了一个用于弹性搜索的 docker 镜像。我正在考虑我需要做的任何配置,因为大量数据将转发到该镜像。我需要考虑可用磁盘空间。在虚拟机世界中,我总是可以添加额外的 vhd 以增加磁盘大小等。在 docker 世界中类似的操作是什么?

virtual-machine elasticsearch docker

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

纠正未传递给函数的参数值

我有一个名为TestFunc.ps1的文件。其内容如下

Function TestFunc([string]$param1, [string]$param2)
{
    Write-Host "------------------"
    Write-Host $param1
    Write-Host $param2
    Write-Host "------------------"
}

TestFunc $param1 $param2 
Run Code Online (Sandbox Code Playgroud)

我这样称呼它

C:\Test\TestFunc.ps1 "Hello" "World"
Run Code Online (Sandbox Code Playgroud)

输出如下

------------------


------------------
Run Code Online (Sandbox Code Playgroud)

我期望输出为

------------------
Hello 
World 
------------------
Run Code Online (Sandbox Code Playgroud)

我在这里做错了什么?

powershell

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

.NET 5.0 发布后是否需要 .NET Standard

由于.NET 5.0 将统一.NET 和.NET Core 平台,是否需要构建.NET Standard 类库类型项目以针对不同版本的.NET 和.NET Core?

.net .net-core .net-standard

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