小编Bry*_*yan的帖子

c#Threadpool - 限制线程数

我正在开发一个控制台应用程序

我想使用Threadpool来执行Web下载.这是一些假代码.

 for (int loop=0; loop< 100; loop++)
 {
     ThreadPool.QueueUserWorkItem(new WaitCallback(GetPage), pageList[loop]);
 }


snip

private static void GetPage(object o)
{
    //get the page
}
Run Code Online (Sandbox Code Playgroud)

如何防止我的代码启动两个以上(或十个或其他)同步线程?

我试过了

    ThreadPool.SetMaxThreads(1, 0);
    ThreadPool.SetMinThreads(1, 0);
Run Code Online (Sandbox Code Playgroud)

但他们似乎没有任何影响.

c# multithreading threadpool

20
推荐指数
4
解决办法
5万
查看次数

ASP.NET Core 2中相同类型的多个实例的依赖注入

在ASP.NET的Core 2网络API,我想使用依赖注入注入httpClientA的情况下HttpClient,以ControllerA和实例httpClientBHttpClientControllerB.

DI注册码看起来像:

HttpClient httpClientA = new HttpClient();
httpClientA.BaseAddress = endPointA;
services.AddSingleton<HttpClient>(httpClientA);

HttpClient httpClientB = new HttpClient();
httpClientB.BaseAddress = endPointB;
services.AddSingleton<HttpClient>(httpClientB);
Run Code Online (Sandbox Code Playgroud)

我知道我可以子类化为HttpClient每个控制器创建一个唯一的类型,但这不能很好地扩展.

什么是更好的方法?

更新 特别是关于HttpClient微软似乎有一些工作正在进行中

https://github.com/aspnet/HttpClientFactory/blob/dev/samples/HttpClientFactorySample/Program.cs#L32 - 感谢@ mountain-traveler(Dylan)指出这一点.

c# dependency-injection asp.net-core

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

ASP.NET MVC URL解码

我有这样的动作:

<%=Html.ActionLink("My_link", "About", "Home", new RouteValueDictionary { 
    { "id", "Österreich" } }, null)%>
Run Code Online (Sandbox Code Playgroud)

这会产生以下链接:http:// localhost:1855/Home/About /%C3%96sterreich

我想要一个看起来像这样的链接 - localhost:1855/Home/About /Österreich

我试过了.

Server.HtmlDecode("Österreich")
HttpUtility.UrlDecode("Österreich") 
Run Code Online (Sandbox Code Playgroud)

似乎都没有帮助.还有什么可以尝试获得我想要的结果?

url asp.net-mvc decode

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

比较法国字符Î的问题

当比较"Île"和"Ile"时,C#并不认为它们是相同的.

    string.Equals("Île", "Ile", StringComparison.InvariantCultureIgnoreCase)
Run Code Online (Sandbox Code Playgroud)

对于我遇到的所有其他重音字符,比较工作正常.

我应该使用另一种比较功能吗?

c# string culture compare

8
推荐指数
1
解决办法
1025
查看次数

c#ToDictionary with ContainsKey check

我有一个列表,我想放在字典中,为简单起见,插入的值将是相同的.

我可以使用foreach循环.

    List<string> list = new List<string>();
    list.Add("Earth");
    list.Add("Wind");
    list.Add("Fire");
    list.Add("Water");
    list.Add("Water"); // Will NOT BE INSERTED using the foreach loop

    var myDictionary= new Dictionary<string, int>();
    foreach (string value in list)
    {
        if (!myDictionary.ContainsKey(value))
        {
        myDictionary.Add(value, 1);
        }
    }
Run Code Online (Sandbox Code Playgroud)

以上工作.

但我想用ToDictionary以下列方式做同样的事情 -

    Dictionary<string, int> myDictionary2 = list.ToDictionary(i => i, i => 1);
Run Code Online (Sandbox Code Playgroud)

当然这失败了,因为我加了两次"水".

使用ToDictionary时检查重复条目的正确方法是什么?

c# linq lambda dictionary todictionary

8
推荐指数
2
解决办法
9212
查看次数

关闭Application Insights

我正在开发一个Web应用程序,它通过应用程序洞察发送数千个请求.

Application Insights在哪里开启和关闭?

asp.net azure-application-insights

8
推荐指数
1
解决办法
7612
查看次数

AutoFixture,创建电子邮件地址列表

我正在写一些单元测试,并有一个类称为Account具有

public Guid AccountId {get;set;}
public IEnumerable<string> EmailAddresses {get;set;}
etc...
Run Code Online (Sandbox Code Playgroud)

我想使用autofixture来创建帐户,但我无法获得电子邮件格式.

我试过了

fixture.Register<string>(() => string.Format("{0}@acme.com", fixture.Create<string>()));
Run Code Online (Sandbox Code Playgroud)

但那会导致循环问题.

我能做到这一点

fixture.Register<string>(() => string.Format("{0}@acme.com", fixture.Create<int>()));
Run Code Online (Sandbox Code Playgroud)

但我宁愿在地址的开头有一个字符串.

编辑 感谢这两个答案我在这里写了一个摘要和其他几个场景作为帖子 - http://nodogmablog.bryanhogan.net/2016/04/customizing-a-specific-string-inside-a-class-using -autofixture /

c# testing unit-testing autofixture

8
推荐指数
2
解决办法
3413
查看次数

使用find和sed将文件名添加到文件的开头

使用以下内容我将文件名添加到每行的前面,并将输出发送到单个文件.

ls | while read file; do sed -e "s/^/$file/g" $file > out; done
Run Code Online (Sandbox Code Playgroud)

我想执行相同的sed替换,但使用findexecxargs 命令 -

find . -type f -exec sed "s/^/{}/g" {} > out +
Run Code Online (Sandbox Code Playgroud)

但我收到一个错误 -

find:-exec ... +仅支持{}的一个实例

输入文件是这样的 -

fileA.txt

A1
A2
Run Code Online (Sandbox Code Playgroud)

fileB.txt

B1
B2
Run Code Online (Sandbox Code Playgroud)

期望的输出

fileA.txt A1
fileA.txt A2
fileB.txt B1
fileB.txt B2
Run Code Online (Sandbox Code Playgroud)

我知道如何用awk做这个,但我想用sed,find和exec或xargs来做.

shell sed find

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

ConcurrentDictionary AddOrUpdate列表

我正在尝试使用ConcurrentDictionary来帮助完成过滤任务.

如果列表中出现一个数字,那么我想将一个条目从一个字典复制到另一个字典.

但这部分AddOrUpdate不对 - v.Add(number)

我明白了

"无法将类型'void'隐式转换为'System.Collections.Generic.List'

还有两个错误.

class Program
{
    static void Main(string[] args)
    {
        Program p = new Program();
        List<int> filter = new List<int> {1,2};
        p.Filter(filter);
    }


    private void Filter(List<int> filter)
    {
        Dictionary<string, List<int>> unfilteredResults = new Dictionary<string, List<int>>();
        unfilteredResults.Add("key1", new List<int> { 1,2,3,4,5});

        ConcurrentDictionary<string, List<int>> filteredResults = new ConcurrentDictionary<string, List<int>>();

        foreach (KeyValuePair<string, List<int>> unfilteredResult in unfilteredResults)
        {
            foreach (int number in unfilteredResult.Value)
            {
                if (filter.Contains(number))
                {
                    filteredResults.AddOrUpdate(unfilteredResult.Key, new List<int> { number }, (k, v) => v.Add(number));
                } …
Run Code Online (Sandbox Code Playgroud)

c# lambda dictionary concurrentdictionary

7
推荐指数
1
解决办法
3485
查看次数

实体框架中的 LinqKit 和异步

在 Entity Framework Core 中使用谓词构建器时出现以下错误。

源 IQueryable 没有实现 IAsyncEnumerable。只有实现 IAsyncEnumerable 的源才能用于实体框架异步操作。

这是代码 -

 List<Member> results = await _context.Members.AsExpandable().Where(predicate).ToListAsync();
Run Code Online (Sandbox Code Playgroud)

这是我正在使用的 EF 的确切版本

  <package id="EntityFramework.Core" version="7.0.0-rc1-final" targetFramework="net451" />
Run Code Online (Sandbox Code Playgroud)

entity-framework predicatebuilder linqkit entity-framework-core

6
推荐指数
1
解决办法
1627
查看次数