我已经制作了一个基本的扩展方法来为我添加重试功能HttpClient.PostAsync:
public static async Task<HttpResponseMessage> PostWithRetryAsync(this HttpClient httpClient, Uri uri, HttpContent content, int maxAttempts, Action<int> logRetry)
{
if (maxAttempts < 1)
throw new ArgumentOutOfRangeException(nameof(maxAttempts), "Max number of attempts cannot be less than 1.");
var attempt = 1;
while (attempt <= maxAttempts)
{
if (attempt > 1)
logRetry(attempt);
try
{
var response = await httpClient.PostAsync(uri, content).ConfigureAwait(false);
response.EnsureSuccessStatusCode();
return response;
}
catch (HttpRequestException)
{
++attempt;
if (attempt > maxAttempts)
throw;
}
}
}
Run Code Online (Sandbox Code Playgroud)
上面的代码给出了以下错误:
错误CS0161'HttpClientExtensions.PostWithRetryAsync(HttpClient,Uri,HttpContent,int,Action)':并非所有代码路径都返回一个值.
如果我throw new InvalidOperationException()在最后添加(或者 …
在我的Startup课程中,我使用该ConfigureServices(IServiceCollection services)方法设置我的服务容器,使用内置的DI容器Microsoft.Extensions.DependencyInjection.
我想在单元测试中验证依赖图,以检查是否可以构造所有服务,以便我可以修复在单元测试期间丢失的任何服务,而不是让应用程序在运行时崩溃.在之前的项目中,我使用了Simple Injector,它有一个.Verify()容器的方法.但我无法找到任何与ASP.NET Core类似的东西.
是否有任何内置(或至少推荐)的方法来验证是否可以构建整个依赖图?
(我能想到的最愚蠢的方式就是这样,但由于框架本身注入了开放的泛型,它仍然会失败):
startup.ConfigureServices(serviceCollection);
var provider = serviceCollection.BuildServiceProvider();
foreach (var serviceDescriptor in serviceCollection)
{
provider.GetService(serviceDescriptor.ServiceType);
}
Run Code Online (Sandbox Code Playgroud) 我想对我的MVC控制器应用一些横切关注点.目前,这是通过抽象基类实现的,但是当我们重构更多代码库以利用依赖注入时,我想知道这是否是Simple Injector可以帮助我通过它的装饰或拦截设施.
所以我试图创建一个非常基本的装饰器:
public class ControllerDecorator : IController
{
private readonly IController _controller;
public ControllerDecorator(IController controller)
{
_controller = controller;
}
public void Execute(RequestContext requestContext)
{
// Do something of a cross-cutting nature here...
_controller.Execute(requestContext);
}
}
Run Code Online (Sandbox Code Playgroud)
在我的作文根: container.RegisterDecorator<IController, ControllerDecorator>()
但是,我的装饰器Execute方法中的代码似乎永远不会被调用.是因为MVC框架直接解析我的控制器类而不是通过IController?在那种情况下,我该怎么办?我在这里错过了什么?
我在我的应用程序中提供了一堆静态文件app.UseStaticFiles()。我想在发送之前为特定 HTML 文件的响应注入一些额外的标记。我的第一次尝试是在静态文件中间件之前添加这样的中间件:
app.Use(async (context, next) => {
await next();
// Modify the response here
});
Run Code Online (Sandbox Code Playgroud)
但是,这不起作用,因为我实际上无法读取读取响应流 - 它在幕后使用 Kestrel FrameResponseStream,这是不可读的。
所以,我想我可以用MemoryStream我可以写入的响应正文流替换:
app.Use(async (context, next) => {
context.Response.Body = new MemoryStream();
await next();
// Modify the response here
});
Run Code Online (Sandbox Code Playgroud)
但这只会导致请求永远不会完成 - 它经历了所有管道阶段,但它甚至从未向浏览器返回任何标头。
那么,有什么方法可以修改由 产生的响应StaticFileMiddleware吗?
更新
由于所讨论的 HTML 文件很小(765 字节),因此不必担心内存消耗。但是,任何读取/修改响应的尝试仍会导致与以前相同的问题(不返回任何内容)。更明确地说,这是正在做的事情:
app.Use(async (context, next) => {
var originalStream = context.Response.Body;
var bufferStream = new MemoryStream();
context.Response.Body = bufferStream;
await next();
bufferStream.Seek(0, SeekOrigin.Begin);
if (/* some …Run Code Online (Sandbox Code Playgroud) 作为项目的一部分,我有一串0到3之间的数字,例如:
2030000000000000000030000000000000000003333212111221121301
我想通过一个URL传递这个字符串,所以我想我可以尝试使用一个位域,因为每个数字最多只使用2位.我在我的类中编写了以下函数,将这样的字符串转换为用作数据位域的整数数组:
makeBuildBitfield: function(build) {
var b = build.split('');
var f = [3, 0]; // the "3" is there for future purposes
var i = 1;
var j = 0;
for (var t in b) {
if (j < 14) {
f[i] |= b[t];
f[i] = f[i] << 2;
++j;
} else {
f[i] |= b[t];
++i;
f[i] = 0;
j = 0;
}
}
return f.join('.');
},
getBuildFromBitfield: function(bitfield) {
b = bitfield.split('.');
var ba = [];
for …Run Code Online (Sandbox Code Playgroud) 我有一个带有输入框的页面,以及一个处理此输入框的值并生成一段文本的函数.我希望这个文本总是与输入框的内容相关,所以我用jQuery附加了几个事件处理程序来捕获任何更改:
$('#input').bind('keyup cut paste', function(){...});
Run Code Online (Sandbox Code Playgroud)
这在大多数情况下效果很好.只要用户以任何方式使用键盘修改内容,或右键单击以使用剪切或粘贴功能,文本就会立即更新.但是,有两个事件我仍然没有弄清楚如何捕获,如果它甚至可能这样做:
当然,这两个都可以通过绑定change事件来检测,但是这种方法的问题是它不会在输入框丢失焦点之前触发.这些绑定的重点是在输入框的值发生变化时实时更新文本,因此change不好.
英语是我的第二语言,所以我可能只是在写我的谷歌搜索时表现不好,但到目前为止他们什么都没有.在挖掘了几个相关的SO页面后,我还没有找到任何解决方案,所以我在这里问.是否有一个我不知道的事件绑定?如果没有,我可以采取不同的方法吗?或者使用普通的Javascript这是不可能的?
有时我需要验证两个值中的一个,一个是 null而另一个不是.这有效:
(a != null && b == null) || (a == null && b != null)
但是当变量名称更长时,它会变得混乱,就像对象上的嵌套属性一样.创建一个辅助函数是一个很好的选择,但是有一个更简洁的语法来编写内联吗?
我有一个编译TypeScript槽Babel的安装程序。此外,我还设置了ESLint和Prettier进行掉毛/格式化。ESLint配置为使用解析器@typescript-eslint/parser-不涉及TSLint。
ESLint正确地应用了漂亮的规则,并向我显示了VS Code中针对常规JavaScript和TypeScript的波浪线。但是,只有常规JavaScript才能在VS Code工具提示的“快速修复...”选项下进行任何操作。对于TypeScript文件,对于更漂亮的问题,它始终显示“没有可用的代码操作”。有什么方法可以使Prettier的快速修复程序与TypeScript文件一起使用?
这是我的配置文件:
.eslintrc
{
"extends": [
"plugin:react/recommended",
"plugin:@typescript-eslint/recommended",
"plugin:prettier/recommended",
"prettier/@typescript-eslint",
"prettier/react"
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"sourceType": "module",
"ecmaVersion": 2018,
"ecmaFeatures": {
"jsx": true
}
},
"plugins": ["@typescript-eslint"],
"env": {
"browser": true,
"jest": true
}
}
Run Code Online (Sandbox Code Playgroud)
.prettierrc.js
{
"extends": [
"plugin:react/recommended",
"plugin:@typescript-eslint/recommended",
"plugin:prettier/recommended",
"prettier/@typescript-eslint",
"prettier/react"
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"sourceType": "module",
"ecmaVersion": 2018,
"ecmaFeatures": {
"jsx": true
}
},
"plugins": ["@typescript-eslint"],
"env": {
"browser": true,
"jest": true
}
}
Run Code Online (Sandbox Code Playgroud)
babel.config.js
module.exports = {
printWidth: …Run Code Online (Sandbox Code Playgroud) c# ×4
javascript ×3
asp.net-core ×2
.net ×1
asp.net-mvc ×1
bit-fields ×1
eslint ×1
jquery ×1
prettier ×1
typescript ×1