无论是否应该,我们都可以IHostedService在 Azure Functions 应用程序中使用吗?
这是尝试将托管服务(特别是后台服务)注册为IHostedService:
internal sealed class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
builder.Services.AddHostedService<ExampleBackgroundService>();
}
}
Run Code Online (Sandbox Code Playgroud)
Functions App 然后抛出以下异常:
Microsoft.Azure.WebJobs.Script.InvalidHostServicesException: 'The following service registrations did not match the expected services:
[Invalid] ServiceType: Microsoft.Extensions.Hosting.IHostedService, Lifetime: Singleton, ImplementationType: ExampleBackgroundService'
Run Code Online (Sandbox Code Playgroud) c# azure background-service azure-functions asp.net-core-hosted-services
以下是基于的互锁方法的实现Interlocked.CompareExchange。
这段代码SpinWait在重复之前是否宜使用自旋?
public static bool AddIfLessThan(ref int location, int value, int comparison)
{
int currentValue;
do
{
currentValue = location; // Read the current value
if (currentValue >= comparison) return false; // If "less than comparison" is NOT satisfied, return false
}
// Set to currentValue+value, iff still on currentValue; reiterate if not assigned
while (Interlocked.CompareExchange(ref location, currentValue + value, currentValue) != currentValue);
return true; // Assigned, so return true
}
Run Code Online (Sandbox Code Playgroud)
我已经看到SpinWait在这种情况下使用过,但是我的理论是它应该是不必要的。毕竟,循环仅包含少量指令,并且总是有一个线程在进行中。
假设有两个线程竞相执行此方法,并且第一个线程立即成功执行,而第二个线程最初不做任何更改,必须重申。没有其他竞争者,第二个线程是否有可能在第二次尝试时失败 …
MySQL手册和几个StackOverflow答案清楚地表明varchar使用:
1 byte与0-255 characters其中的varchars2 bytes与more than 255 characters其中的varchars .第一部分是有道理的.单个字节可以存储256个不同的值,即0到255.
我想弄清楚的是MySQL 如何知道有多少字节表示长度.
想象一下从以下字节开始的255-char varchar: [255][w][o][r][d]~
根据手册,在这种情况下,只有第一个字节用于表示长度.在阅读该字段时,MySQL将以某种方式必须知道这是这种情况,并且第二个字节不是长度的一部分.
现在假设一个256-char varchar以下列字节开头: [255][1][w][o][r][d]~
现在MySQL奇迹般地知道它应该在读取字段时将前两个字节解释为长度.
它如何区分?我提出的唯一简单方法是将第一个字节解释为长度,然后确定文本长度是否匹配(在其当前编码中),如果不匹配,我们知道前两个字节必须是长度.
我在用DateTime.Now.ToString("d", new CultureInfo("nl-NL")).
在本地IIS Web应用程序中,输出是正确的:
22-7-2016
Run Code Online (Sandbox Code Playgroud)
在控制台应用程序中,框架选择国际/文化不变的日期符号:
2016-07-22
Run Code Online (Sandbox Code Playgroud)
这是在同一台机器上,代码完全相同.我已经放置了断点,检查了所有输入,甚至尝试在此时立即在窗口中运行命令.在任何情况下,控制台应用程序都会出错.
任何人都可以解释导致差异的原因吗?
在为iOS编程时遇到以下情况:
我有一个带有类方法的单例类+ (Store*)sharedStore.当我想在单例对象上调用实例方法时,我可以使用点语法来获取该对象,即[Store.sharedStore foo].
但是,在键入点后,Xcode不会自动完成'sharedStore'.另一方面,[[Store sharedStore] foo] 是自动完成的!
有没有'类属性'这样的东西?如果我可以sharedStore在类上变成只读属性,则点语法将获得自动完成.
更一般地说,即使这是一种调用(getter)方法的有效方法,Xcode也不会在点语法之后自动完成任何不属性的东西.
任何解决方案,变通方法或信息都表示赞赏.
IHostBuilder我在 .NET Core 2.1 控制台应用程序中使用。主要看起来像这样:
public static async Task Main(string[] args)
{
var hostBuilder = new HostBuilder()
.UseServiceProviderFactory(new AutofacServiceProviderFactory())
.ConfigureServices(services =>
{
// Register dependencies
// ...
// Add the hosted service containing the application flow
services.AddHostedService<RoomService>();
});
await hostBuilder.RunConsoleAsync();
}
}
Run Code Online (Sandbox Code Playgroud)
之前,使用IWebHostBuilder,我有Configure()让我这样做的方法:
public void Configure(IApplicationBuilder applicationBuilder, IHostingEnvironment environment)
{
// Resolve something unrelated to the primary dependency graph
var thingy = applicationBuilder.ApplicationServices.GetRequiredService<Thingy>();
// Register it with the ambient context
applicationBuilder.AddAmbientThingy(options => options.AddSubscriber(thingy));
// Use …Run Code Online (Sandbox Code Playgroud) c# dependency-injection ambientcontext .net-core .net-core-2.1
使用 C# 的Vector<T>,我们如何才能最有效地矢量化查找集合中特定元素的索引的操作?
作为约束,集合将始终是一个Span<T>整数基元,并且最多包含 1 个匹配元素。
我想出了一个看起来不错的解决方案,但我很好奇我们是否可以做得更好。这是方法:
Vector<T>在每个插槽中创建一个仅包含目标元素的对象。Vector.Equals()在输入集向量和上一步中的向量之间使用,以获取在单个匹配槽中包含 1 的掩码(如果没有匹配,则仅包含 0)。Vector.Dot()在该向量和上一步中的掩码之间进行调用。每个索引都将乘以 0,除了潜在匹配索引,它将乘以 1。我们得到的是这些乘法的总和,它要么是 0,要么是匹配元素的从 1 开始的索引。如果结果为 0,则返回 -1 表示不匹配。否则,从结果中减去 1 使其从 0 开始,然后返回该结果。
// One-time initialized vector containing { 1, 2, 3, 4, ... }
Vector<ushort> indexes = MemoryMarshal.Cast<ushort, Vector<ushort>>(Enumerable.Range(1, Vector<ushort>.Count).Select(index => (ushort)index).ToArray())[0];
// The input set and the element to search for
Span<ushort> set = stackalloc ushort[]{ 10, …Run Code Online (Sandbox Code Playgroud)KeyValuePair<int, int>: 8 bytes
KeyValuePair<long, long>: 16 bytes
KeyValuePair<long, int>: 16 bytes (!!)
KeyValuePair<int, long>: 16 bytes (!!)
Run Code Online (Sandbox Code Playgroud)
我希望后两对只需要8 (long) + 4 (int)= 12个字节.为什么他们占16?
使用通过ILGenerator发出的动态SizeOf确定大小,如下所述:通用结构的大小
如果我们==在的表达式ulong和的表达式之间使用运算符ulong?,则bool ulong(ulong left, ulong right)似乎会使用运算符重载。
换句话说,运算符认为两个表达式都不为空。
在此示例程序中,equal无例外,正确地变为false。
void Main()
{
var temp = new Temp(0);
object temp2 = null;
var equal = temp.Id == (temp2 as Temp)?.Id; // False :) but how?
}
public class Temp
{
public ulong Id {get;}
public Temp(ulong id)
{
this.Id = id;
}
}
Run Code Online (Sandbox Code Playgroud)
ulong?值从null 转换为的转换ulong。(ulong)(ulong?)null抛出:“可为空的对象必须具有一个值。”ulong?包括null)返回正确的值?如果是这样,怎么办?该类型ulong?具有比更大的可能值ulong,因此应该有一组两个值映射到相同的 ulong值,这将引入一个错误的肯定“ …在 C# 中是否可以在不分支的情况下bool将 a 转换为byteor int(或任何整数类型)?
换句话说,三元还不够好:
var myInt = myBool ? 1 : 0; // could compile to a branch
Run Code Online (Sandbox Code Playgroud)
我们可能会说我们希望将 a 重新解释bool为底层byte,最好使用尽可能少的指令。目的是避免分支预测失败,如下所示。
为了便于论证,我们假设 abool已经作为 0/1 存在于整数寄存器或内存中,而不仅仅是作为 FLAGS 中的比较结果。大多数现代 ISA 确实有一种无分支方式从 int 或 FP 比较中获取 0 或 1,因此我们希望 C# 使用它。或者,如果bool刚刚来自x<y,我们希望强制编译器将其具体化为整数,而不是变成count += myInt周围的分支count++。
c# ×8
.net-core ×1
asp.net-core-hosted-services ×1
autocomplete ×1
azure ×1
boolean ×1
branchless ×1
byte ×1
char ×1
class ×1
culture ×1
date ×1
datetime ×1
dot-product ×1
interlocked ×1
intrinsics ×1
keyvaluepair ×1
maxlength ×1
mysql ×1
nullable ×1
objective-c ×1
properties ×1
simd ×1
sizeof ×1
spinwait ×1
struct ×1
tostring ×1
type-punning ×1
varchar ×1