小编sad*_*t75的帖子

ASP.net HttpRequest上的静态HttpClient线程安全

我们正在为HttpClient创建一个包装器.我们将遵循https://github.com/mspnp/performance-optimization的性能优化指南.我们希望避免反模式 - 该文档中提到的不正确的实例化.我将此指南提交给我的团队使用静态HttpClient.我得到的反馈是线程安全性.每个请求都有一个包含用户声明的标头.由于我有一个静态的HttpClient,它是否是线程安全的?如果我们同时有多个请求命中代码(例如GET),那么设置标题是否会出现竞争条件?我们的实施如下.

public class HttpClientHelper{
private static readonly HttpClient _HttpClient;
static HttpClientHelper() {
        HttpClient = new HttpClient();
        HttpClient.Timeout = TimeSpan.FromMinutes(SOME_CONFIG_VALUE);
}

public async Task<HttpResponseMessage> CallHttpClientPostAsync(string requestUri, HttpContent requestBody)
{
    AddHttpRequestHeader(httpClient);
    var response = await httpClient.PostAsync(requestUri, requestBody); //Potential thread synchronization issue???
    return response;
}

public HttpResponseMessage CallHttpClientGet(string requestUri)
{
    AddHttpRequestHeader(httpClient);
    var response = httpClient.GetAsync(requestUri).Result; //Potential thread synchronization issue???
    return response;
}

private void AddHttpRequestHeader(HttpClient client)
{
    string HeaderName = "CorrelationId";
    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(Properties.Settings.Default.HttpClientAuthHeaderScheme, GetTokenFromClaims()); //Race condition???
    if …
Run Code Online (Sandbox Code Playgroud)

asp.net httprequest static-libraries thread-safety dotnet-httpclient

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

代码优先参考一对多

我有以下两个表:

本土化

Id                     int
Text                   string
Run Code Online (Sandbox Code Playgroud)

DINER

Id                     int
Name                   string
Description            string
Name_LocalizationID    int
Description_LocationID int
Run Code Online (Sandbox Code Playgroud)

现在我希望我的POCO像这样:

public class Diner{
   public int Id{get;set;}
   public ICollection<Localization> NameLocalization{get;set;}
   public ICollection<Localization> DescriptionLocalization{get;set;} 
}

public class Localization{
   public int Id{get;set;}
   public string Text{get;set;}
}
Run Code Online (Sandbox Code Playgroud)

问题是:我们如何使用EF Fluent API将NameLocalization和DescriptionLocalization属性映射到Localization的Id?谢谢

entity entity-framework fluent-interface code-first

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