小编hav*_*vij的帖子

如果消息处理失败,则再次使用相同的消息

我正在使用 Confluent.Kafka .NET 客户端版本 1.3.0。我正在关注文档

var consumerConfig = new ConsumerConfig
{
    BootstrapServers = "server1, server2",
    AutoOffsetReset = AutoOffsetReset.Earliest,
    EnableAutoCommit = true,
    EnableAutoOffsetStore = false,
    GroupId = this.groupId,
    SecurityProtocol = SecurityProtocol.SaslPlaintext,
    SaslMechanism = SaslMechanism.Plain,
    SaslUsername = this.kafkaUsername,
    SaslPassword = this.kafkaPassword,
};

using (var consumer = new ConsumerBuilder<Ignore, string>(consumerConfig).Build())
{
    var cancellationToken = new CancellationTokenSource();
    Console.CancelKeyPress += (_, e) =>
    {
        e.Cancel = true;
        cancellationToken.Cancel();
    };

    consumer.Subscribe("my-topic");
    while (true)
    {
        try
        {
            var consumerResult = consumer.Consume();
            // process message
            consumer.StoreOffset(consumerResult); …
Run Code Online (Sandbox Code Playgroud)

c# apache-kafka kafka-consumer-api

15
推荐指数
1
解决办法
1116
查看次数

更新中间件.NET Core中的响应主体

我的 .NET Core 3.1 应用程序中有一个自定义中间件,并尝试设置响应 StatusCode 和 Body,如下所示:

public async Task Invoke(HttpContext context)
{
    if ( <some condition on context.Request> )
    {
        context.Response.StatusCode = (int)HttpStatusCode.BadRequest;
        await context.Response.WriteAsync("my custom message");

        // Bad request, do not call next middleware.
        return;
    }

    // Call next middleware.
    await _requestDelegate(context);
}
Run Code Online (Sandbox Code Playgroud)

使用上面的代码,StatusCode 已正确设置,但是响应正文为空。如何将自定义消息写入正文?

更新1:

添加了await,但这并不能解决问题。感谢@Jonesopolis 提到这一点。

更新2

因此,我正在 Swagger 中测试响应(我还在查看开发人员的“网络”选项卡)。但是,当我在 Postman 中测试时,我得到了预期的响应正文。

所以问题实际上是为什么响应正文没有显示在 Swagger/网络选项卡中?

谢谢!

c# .net-core asp.net-core-middleware

9
推荐指数
1
解决办法
1万
查看次数

为什么shared_ptr实现中的ref_count是int*

我已经看到了几个shared_ptr的实现,例如这里.所有人都宣称ref_countint*.我不明白,如果只是一个,我们会失去什么int.谢谢!

template <class T>
class shared_ptr {
    T* ptr;
    int* ref_count;

    /**
     * Initializes the ref count used for tracking the usage.
     */
    void initialize_ref_count() {
        if (ref_count != nullptr)
            return;
        try {
            ref_count = new int;
            *ref_count = 1;
        }
        catch (std::bad_alloc& e) {
            std::cerr << "Memory allocation error: " << e.what();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

c++ shared-ptr

8
推荐指数
1
解决办法
851
查看次数

.NET Core 中的单元测试托管服务

我有一个由 .NET Core 中的托管服务实现的后台任务。这个类中的逻辑很少:

public class IndexingService : IHostedService, IDisposable
{
    private readonly int indexingFrequency;

    private readonly IIndexService indexService;

    private readonly ILogger logger;

    private bool isRunning;

    private Timer timer;

    public IndexingService(ILogger<IndexingService> logger, IIndexService indexService, IndexingSettings indexingSettings)
    {
        this.logger = logger;
        this.indexService = indexService;

        this.indexingFrequency = indexingSettings.IndexingFrequency;
    }

    public void Dispose()
    {
        this.timer?.Dispose();
    }

    public Task StartAsync(CancellationToken cancellationToken)
    {
        this.timer = new Timer(this.DoWork, null, TimeSpan.Zero, TimeSpan.FromSeconds(this.indexingFrequency));
        return Task.CompletedTask;
    }

    public Task StopAsync(CancellationToken cancellationToken)
    {
        this.timer?.Change(Timeout.Infinite, 0);
        return Task.CompletedTask;
    }

    private …
Run Code Online (Sandbox Code Playgroud)

c# unit-testing .net-core asp.net-core-hosted-services

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

C++如何自动调用析构函数?

在C++中,我们可以按对象管理资源,即在Ctor中获取资源,并在Dtor(RAII)中释放它.这依赖于C++的自动析构函数调用.但是如何在引擎盖下完成?例如,C++如何知道调用Dtor c1但不是c2.(我知道之前必须已经回答过,但我的所有搜索都以解释如何使用RAII的主题结束).谢谢!

class Cat;
Cat c1;
Cat* c2 = new Cat();
Run Code Online (Sandbox Code Playgroud)

编辑: 我知道我需要调用删除c2.我只是不明白当Dtor c1超出范围时如何被调用.

c++ raii

5
推荐指数
1
解决办法
683
查看次数

返回JSON错误消息,IActionResult

我有一个API控制器端点,如:

public IHttpActionResult AddItem([FromUri] string name)
{
    try
    {
        // call method
        return this.Ok();
    }
    catch (MyException1 e)
    {
        return this.NotFound();
    }
    catch (MyException2 e)
    {
        return this.Content(HttpStatusCode.Conflict, e.Message);
    }
}
Run Code Online (Sandbox Code Playgroud)

这将在正文中返回一个字符串"here is your error msg",有没有办法返回带有'Content'的JSON?

例如,

{
  "message": "here is your error msg"
}
Run Code Online (Sandbox Code Playgroud)

c# json asp.net-web-api

5
推荐指数
1
解决办法
862
查看次数

重新使用给定时间的 Kafka 消息

我正在使用 Confluence.Kafka .NET 客户端版本 1.3.0。我想从给定时间开始消费消息。

为此,我可以用来OffsetsForTimes获取所需的偏移量以及Commit该分区的偏移量:

private void SetOffset()
{
    const string Topic = "myTopic";
    const string BootstrapServers = "server1, server2";

    var adminClient = new AdminClientBuilder(
        new Dictionary<string, string>
        {
            { "bootstrap.servers", BootstrapServers },
            { "security.protocol", "sasl_plaintext" },
            { "sasl.mechanisms", "PLAIN" },
            { "sasl.username", this.kafkaUsername },
            { "sasl.password", this.kafkaPassword }
        }).Build();

    var consumer = new ConsumerBuilder<byte[], byte[]>(
        new Dictionary<string, string>
        {
            { "bootstrap.servers", BootstrapServers },
            { "group.id", this.groupId },
            { "enable.auto.commit", "false" },
            { "security.protocol", "sasl_plaintext" …
Run Code Online (Sandbox Code Playgroud)

c# apache-kafka kafka-consumer-api

5
推荐指数
2
解决办法
9003
查看次数