我注意到在一些.net核心示例中有调用 TryAddSingleton,有些是 AddSingleton在注册服务时.
如果服务类型尚未注册,则反编译器会显示TryAdd(由TryAddSingleton调用)将指定的参数"descriptor"添加到"集合".
这是否意味着使用TryAddSingleton总是更安全,以防某些其他方法/库已经注册了同一个类?
在Microsoft.Extensions.Primitives包lib中,有一个类StringSegment,注释表明它是:
子串的优化表示.
我没有意识到这个特殊的类,直到我发现了aspnet公告#244,声明:Microsoft.Net.Http.Headers转换为使用StringSegments.
仍然,看看StringSegment类的实现,我没有看到它实际服务的目的.我看到一个缓冲区,我想这可能表示对部分字符有更好的操作(也许是'段'部分?).我还看到几个辅助函数,它们与常规字符串中已有的行为密切相关(如果不相同),例如StartsWith/Endswith,Substring等.aspnet -core文档完整地列出了这些函数,但是这也缺乏上下文关于"为什么"它应该被使用.
那么该StringSegment课程的目的到底是什么以及适用于哪种情景呢?
当我操作字符串时,在我的应用程序代码中调用类是否有用?我们能有一个例子,它会有益吗?
我正在使用selenium创建数据验证框架.我正在努力解决的问题是我想在元素"tr"(html标签)中找到元素"td"(html标签).这是我写的代码.
Iterator<WebElement> i = rows.iterator();
While(i.hasnext()){
List<WebElement> columns = row.findElements(By.tagName("td"));
for(WebElement s:columns)
{
System.out.println("columnDetails : "+s.getText().toString());
}
if(columns.isEmpty())
{
ElementNotFoundException e = new ElementNotFoundException("No data in table");
throw e;
}
Iterator<WebElement> j = columns.iterator();// does some other work
ClusterData c = new ClusterData(); // does some other work
ClusterDataInitializer.initUI(c, j, lheaders); // does some other work
CUIData.put(c.getCN(), c); // does some other work
}
Run Code Online (Sandbox Code Playgroud)
现在问题是:

我试图从arraylist中的行(参见表数据)中获取数据并进一步使用该arraylist.目前发生的是列标题的数据是在开始时获取的,我没有用.我只想要行的数据.我无法确定仅收集表行数据的正确方法.如果表的xPath将帮助您正确理解它,那么这里是详细信息:
xPath群集名称列的表头:
/html/body/table/tbody/tr[2]/td[2]/div[2]/div/div/div[2]/div/div/div[2]/div/div/div[2]/div/div/div/div/div/div[2]/div/div/div[2]/div/div/div[2]/div[2]/div/table/tbody/tr/td[2]/div/div[2]
Run Code Online (Sandbox Code Playgroud)
xPath测试集群01的表行(表数据):
/html/body/table/tbody/tr[2]/td[2]/div[2]/div/div/div[2]/div/div/div[2]/div/div/div[2]/div/div/div/div/div/div[2]/div/div/div[2]/div/div/div[3]/div[2]/div/table/tbody/tr/td[2]/div/div/a
Run Code Online (Sandbox Code Playgroud)
如果您还有其他需要,请告诉我.
我使用以下代码从表中提取行数据.
List<WebElement> rows = getElement(driver,sBy,"table_div_id").findElements(By.tagName("tr"));
Run Code Online (Sandbox Code Playgroud)
其中sBy …
我正在使用PostSharp,我的项目文件中有以下目标描述:
<Target Name="EnsurePostSharpImported" BeforeTargets="BeforeBuild" Condition="'$(PostSharp30Imported)' == ''">
<Error Condition="!Exists('..\..\packages\PostSharp.3.1.33\tools\PostSharp.targets')" Text="This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://www.postsharp.net/links/nuget-restore." />
<Error Condition="Exists('..\..\packages\PostSharp.3.1.33\tools\PostSharp.targets')" Text="The build restored NuGet packages. Build the project again to include these packages in the build. For more information, see http://www.postsharp.net/links/nuget-restore." />
</Target>
Run Code Online (Sandbox Code Playgroud)
据我所知,当通过NuGet引用PostSharp时,这会添加到项目中,并且错误条件会检查以下内容:
但,如果我有以下配置NuGet.Config和.csproj file,是第二差错情况甚至是必要的?
NuGet.Config 文件:
<configuration>
<packageRestore>
<!-- Allow NuGet to …Run Code Online (Sandbox Code Playgroud) 我有一个非常奇怪的问题.我在dll中定义了一个接口,如下所示:
public interface IKreator2
{
string Name { get; set; }
string Description { get; set; }
INotifyPropertyChanged Settings { get; set; }
InfiniRenderJob Job { get; set; }
UserControl UI { get; set; }
void Init();
//void OnClose();
}
Run Code Online (Sandbox Code Playgroud)
如果我在我的WPF应用程序中链接到此dll,则调试器在加载时崩溃(内部错误:调试器中的未处理异常:: HandleIPCEvent,ID = 0x246).如果我用"debug unmanaged code"调试应用程序,我会收到以下错误:
在那一刻,我绝对不知道发生了什么.甚至没有接口的实现,也没有类使用它.如果我将方法"Init"注释掉,一切都按预期工作.有任何想法吗??
[编辑]这是接口init方法的MSIL:
.method public hidebysig newslot virtual
instance void Init() cil managed
{
// Code size 96 (0x60)
.maxstack 3
.locals init ([0] …Run Code Online (Sandbox Code Playgroud) 我有一个ArrayList类型的BookingData到List<BookingData>?
我正在使用.net 2.0所以我不能使用arrayList.Cast<int>().ToList(),我不想在这里做foreach循环,你有更好的想法吗?
谢谢.
我的朋友给我发了他在服务器2008中写的命令,他们没有遇到任何问题,但我从副本和过去没有用到2012年.有什么理由吗?这是代码:
Use Kudler_Database
SELECT AccountNumber, [Description], ShortDescription,Balance
FROM Chart_of_Accounts
ORDER BY left (AccountNumber, 2)
COMPUTE SUM(Balance) BY left (AccountNumber, 2)
COMPUTE SUM(Balance);
Run Code Online (Sandbox Code Playgroud)
这是错误:
消息156,级别15,状态1,行6关键字'COMPUTE'附近的语法不正确.
我考虑使用 Polly 来创建记录异常和重新抛出的策略.我没有找到允许它开箱即用的现有方法,但我看到的一些选项
倒退
// Specify a substitute value or func, calling an action (eg for logging) if the fallback is invoked.
Policy.Handle<Whatever>()
.Fallback<UserAvatar>(UserAvatar.Blank, onFallback: (exception, context) =>
{ _logger.Log(exception, context);
throw exception;
});
Run Code Online (Sandbox Code Playgroud)
问题:从Fallback抛出异常是否可以?
超时
Policy.Timeout(1, T30meoutStrategy.Pessimistic, (context, timespan, task) =>
{ task.ContinueWith(t =>
{ // ContinueWith important!: the abandoned task may very well still be executing, when the caller times out on waiting for it!
if (t.IsFaulted )
{
logger.Error(context,t.Exception);
throw exception;
} );
Run Code Online (Sandbox Code Playgroud)
或者重试
Policy.Handle<DivideByZeroException>().Retry(0, (exception, …Run Code Online (Sandbox Code Playgroud) 我正在使用FlurlHttp,并且想对某些API调用禁用AllowAutoRedirect。我知道如何使System.Net.Http.HttpClient不遵循302重定向?
WebRequestHandler webRequestHandler = new WebRequestHandler();
webRequestHandler.AllowAutoRedirect = false;
HttpClient httpClient = new HttpClient(webRequestHandler);
// Send a request using GetAsync or PostAsync
Task<HttpResponseMessage> response = httpClient.GetAsync("http://www.google.com")
Run Code Online (Sandbox Code Playgroud)
但是对于Flurl,我只找到了与C#Flurl中所述类似的方法 -将WebRequestHandler添加到FlurlClient(我尚未编译下面的代码,因此可能会有一些错误)
public class HttpClientFactoryWithWebRequestHandler : DefaultHttpClientFactory
{
private readonly WebRequestHandler _webRequestHandler;
public HttpClientFactoryWithWebRequestHandler (WebRequestHandler webRequestHandler )
{
_webRequestHandler = webRequestHandler ;
}
public override HttpMessageHandler CreateMessageHandler()
{
var handler =_webRequestHandler ;
//Or var handler = new WebRequestHandler(_webRequestHandler );
return handler;
}
}
Run Code Online (Sandbox Code Playgroud)
然后,我可以为新的FlurlClient传递设置:
WebRequestHandler webRequestHandler = new …Run Code Online (Sandbox Code Playgroud) 我正在使用LazyCache ,并且希望每小时刷新一次缓存,但理想情况下,我希望缓存项过期后的第一个调用者不要等待缓存重新加载。我写了以下内容
\n\npublic async Task<List<KeyValuePair<string, string>>> GetCarriersAsync()\n{\n\n var options = new MemoryCacheEntryOptions\n {\n AbsoluteExpirationRelativeToNow = new TimeSpan(1,0,0),// consider to config\n }.RegisterPostEvictionCallback(\n async (key, value, reason, state) =>\n {\n await GetCarriersAsync();//will save to cache\n _logger.LogInformation("Carriers are reloaded: " );\n });\n Func<Task<List<KeyValuePair<string, string>>>> cacheableAsyncFunc = () => GetCarriersFromApi();\n var cachedCarriers = await _cache.GetOrAddAsync($"Carriers", cacheableAsyncFunc, options);\n\n return cachedCarriers;\n}\nRun Code Online (Sandbox Code Playgroud)\n\n但是,当缓存项过期时,不会调用 RegisterPostEvictionCallback,而是仅在对该项的下一个请求发生时调用(并且调用者需要等待漫长的操作)。
\n\n线程 Expiration 几乎永远不会在后台自行发生\xc2\xa0#248解释说这是设计使然,并建议解决方法来指定 CancellationTokenSource.CancelAfter(TimeSpan.FromHours(1)) 而不是 SetAbsoluteExpiration。
\n\n不幸的是,LazyCache.GetOrAddAsync 没有\xe2\x80\x99t 有 CancellationToken 作为参数。\n在预定时间触发缓存重新加载且第一个用户的等待时间最短的最佳方法是什么?
\n