我有简单的 ASP.NET Core WebApi 模型
public class Model
{
public bool? Value {get; set;}
}
Run Code Online (Sandbox Code Playgroud)
和端点
[HttpPost]
public async Task<IActionResult> Create([FromBody] Model model)
Run Code Online (Sandbox Code Playgroud)
当我用 body 发出 POST 请求时
{
"Value" : 7676
}
Run Code Online (Sandbox Code Playgroud)
或者
{
"Value" : 2955454545645645645645645645654534534540
}
Run Code Online (Sandbox Code Playgroud)
然后 model.Value == true
如何避免这种情况?在这种情况下我需要一些错误,因为7676不是布尔值。
我发现了this question和this,但解决方案不适合我,因为我在不同的项目中有很多模型(因此,很难将JsonConverter属性从答案添加到所有属性)
此外,我正在寻找任何描述此行为的文档。
我有 ASP.NET WebApi 项目,它在每个请求上返回安全标头:
当我使用 Ingress NGINX 在 Kubernetes 集群中运行此应用程序时,我的标头丢失了
如何配置 NGINX 以使用来自我的应用程序的响应标头?为什么 Ingress NGINX 会删除我的响应标头?
我对 NGINX 配置没有任何经验。请建议如何在 k8s 集群中做到这一点。谢谢
在上一个问题之后,我有一个简单的实现IValueResolver
public class FileLinkResolver : IValueResolver<Configuration, ConfigurationDto, string>
{
private readonly IFileStorage _fileStorage;
public FileLinkResolver(IFileStorage fileStorage)
{
_fileStorage = fileStorage;
}
public string Resolve(Configuration source, ConfigurationDto destination, string destMember, ResolutionContext context)
{
return _fileStorage.GetShortTemporaryLink(source.Path);
}
}
Run Code Online (Sandbox Code Playgroud)
和简单的映射配置文件
public class MappingProfile : Profile
{
public MappingProfile()
{
CreateMap<Configuration, ConfigurationDto>()
.ForMember(dest => dest.FilePath, opt => opt.MapFrom<FileLinkResolver>());
}
}
Run Code Online (Sandbox Code Playgroud)
对于生产,当以下设置时,它按预期工作
services.AddTransient<IFileStorage>(...);
services.AddAutoMapper();
Run Code Online (Sandbox Code Playgroud)
使用然后在控制器IMapper中注入。
在单元测试中,我尝试存根映射器
var mapperStub = new Mapper(new MapperConfiguration(map => map.AddProfile(new MappingProfile())));
Run Code Online (Sandbox Code Playgroud)
当我为方法女巫运行测试时,应该返回映射的 dto,我得到了
AutoMapper.AutoMapperMappingException :映射类型错误。
Run Code Online (Sandbox Code Playgroud)Mapping …
我从来没有使用过 shell (bash),但在脚本中发现了一些错误,我用来增加版本。脚本工作正常,直到这种情况
version=1.27.9
echo $version | awk -F. -v OFS=. 'NF==1{print ++$NF}; NF>1{if(length($NF+1)>length($NF))$(NF-1)++; $NF=sprintf("%0*d", length($NF), ($NF+1)%(10^length($NF))); print}'
1.28.0 <-- result, but I need 1.27.10
Run Code Online (Sandbox Code Playgroud)
在这种情况下new_version将等于1.28.0。如何更改此脚本以避免增加 MINOR 编号?对于这种情况,我期待1.27.10
我没有shell经验,所以不知道从哪里开始。我在这里找到了这个脚本,在 SO 上并使用它。请帮我解决我的问题。先感谢您。
我有一个在 docker 容器中运行的简单 ASP.NET Core Web API 应用程序。
此应用程序应处理 HTTP 请求并使用来自 Kafka 的事件。因此,在 Startup.Configure 方法中,我使用无限循环运行专用线程来使用事件。
public void Consume()
{
Task.Factory.StartNew(async () =>
{
try
{
while (true)
{
var eventMsg = _consumer.Consume();
await Handle(eventMsg);
}
}
catch (Exception ex)
{
_consumer.Close();
// log error
throw;
}
}, TaskCreationOptions.LongRunning);
}
Run Code Online (Sandbox Code Playgroud)
然后在启动
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
}
app.UseAuthentication();
app.UseMvc();
var consumer = app.ApplicationServices.GetRequiredService<KafkaConsumer>();
consumer.Consume();
}
Run Code Online (Sandbox Code Playgroud)
但是当循环中发生错误时,我需要使用应用程序重新启动容器。在简单的应用程序中,当我添加时,throw;它会生成未处理的异常,关闭应用程序并在结果容器中重新启动。但在这种情况下throw;没有帮助,因为它在后台线程中抛出,而我不是在等待结果。我不是在等待结果,因为当我添加
await …Run Code Online (Sandbox Code Playgroud) 我正在尝试在我的 Kafka 消费者中使用 Rx。
public static event EventHandler<ConsumeResult<string, string>> GenericEvent;
Run Code Online (Sandbox Code Playgroud)
然后我有以下代码
var observable = Observable.FromEventPattern<ConsumeResult<string, string>>(
x => GenericEvent += x,
x => GenericEvent -= x)
.Select(x => x.EventArgs);
while (!cancellationToken.IsCancellationRequested)
{
ConsumeResult<string, string> consumeResult = _consumer.Consume(cancellationToken);
GenericEvent(consumeResult.Topic, consumeResult);
}
Run Code Online (Sandbox Code Playgroud)
然后在某处我使用它
var subscription = observable.Subscribe(message =>
{
Console.WriteLine($"{Thread.CurrentThread.ManagedThreadId} ** {message.Topic}/{message.Partition} @{message.Offset}: '{message.Value}'");
//_kafkaConsumer.Commit(messages);
});
Run Code Online (Sandbox Code Playgroud)
是否有可能按主题名称 ( consumeResult.Topic)运行单独的线程?当消费者收到一条消息时,它应该将其按主题重定向到相应的线程
c# ×4
asp.net-core ×3
async-await ×2
.net ×1
asynchronous ×1
automapper ×1
awk ×1
bash ×1
docker ×1
kubernetes ×1
nginx ×1
rx.net ×1
shell ×1