Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 发送异步azure事件中心数据方法_C#_Azure_Asynchronous_Asp.net Web Api_Azure Eventhub - Fatal编程技术网

C# 发送异步azure事件中心数据方法

C# 发送异步azure事件中心数据方法,c#,azure,asynchronous,asp.net-web-api,azure-eventhub,C#,Azure,Asynchronous,Asp.net Web Api,Azure Eventhub,在我的api(c#)中,我目前正在回答很多请求(每秒15-20个)。现在,出于某些目的,我想将数据发送到事件中心。但是我不想延迟用户向azure事件中心发送数据。所以我需要向事件中心发出异步请求,因为当我的应用程序向azure发送数据时,我不想让用户等待我的回答。我需要尽快发送响应,azure可能会持续2-3秒 我怎样才能成功呢? 我做了很多,但没有得到我想要的。我的代码: public static async Task<string> SendEvents(List<obj

在我的api(c#)中,我目前正在回答很多请求(每秒15-20个)。现在,出于某些目的,我想将数据发送到事件中心。但是我不想延迟用户向azure事件中心发送数据。所以我需要向事件中心发出异步请求,因为当我的应用程序向azure发送数据时,我不想让用户等待我的回答。我需要尽快发送响应,azure可能会持续2-3秒

我怎样才能成功呢? 我做了很多,但没有得到我想要的。我的代码:

public static async Task<string> SendEvents(List<object> messages)
{
    string eventHubName = "rcmds";

    var connectionString = GetServiceBusConnectionString();

    CreateEventHub(eventHubName, connectionString);

    var eventHubClient = EventHubClient.CreateFromConnectionString(connectionString, eventHubName);

    try
    {
        List<Task> tasks = new List<Task>();

        for (int i = 0; i < messages.Count; i++)
        {
            var serializedMessage = JsonConvert.SerializeObject(messages[i]);

            EventData data = new EventData(Encoding.UTF8.GetBytes(serializedMessage));

            // Mesajları Event Hub a yolla
            tasks.Add(eventHubClient.SendAsync(data));
        }

        Task.WaitAll(tasks.ToArray());
        System.Threading.Thread.Sleep(7000);
    }
    catch (Exception ex)
    {
        new ExceptionHandler(ex, "Event Hub Library Sender - SendEvents");
    }
    finally
    {
        eventHubClient.CloseAsync().Wait();
    }
    return "";
}
公共静态异步任务SendEvents(列出消息)
{
字符串eventHubName=“rcmds”;
var connectionString=GetServiceBusConnectionString();
CreateEventHub(eventHubName,connectionString);
var eventHubClient=eventHubClient.CreateFromConnectionString(connectionString,eventHubName);
尝试
{
列表任务=新列表();
for(int i=0;i
我称这种方法为:

 static async void method()


 {
            List<object> list = new List<object>();
            list.Add("dogrudur");
            await Utilities.EventHub.Sender.SendEvents(list);
        }
静态异步void方法()
{
列表=新列表();
添加(“dogrudur”);
等待实用程序.EventHub.Sender.SendEvents(列表);
}

正如您所看到的,有“thread.sleed”代码,但我等了7秒钟://

我认为您需要了解更多关于如何以良好的方式真正使用async/await的信息。您正在混合异步代码和阻塞代码

正确的代码应该如下所示:

public static async Task<string> SendEvents(List<object> messages)
{
    string eventHubName = "rcmds";

    var connectionString = GetServiceBusConnectionString();

    CreateEventHub(eventHubName, connectionString);

    var eventHubClient = EventHubClient.CreateFromConnectionString(connectionString, eventHubName);

    try
    {
        List<Task> tasks = new List<Task>();

        for (int i = 0; i < messages.Count; i++)
        {
            var serializedMessage = JsonConvert.SerializeObject(messages[i]);

            EventData data = new EventData(Encoding.UTF8.GetBytes(serializedMessage));

            // Mesajları Event Hub a yolla
            tasks.Add(eventHubClient.SendAsync(data));
        }

        await Task.WhenAll(tasks.ToArray());
        System.Threading.Thread.Sleep(7000);
    }
    catch (Exception ex)
    {
        new ExceptionHandler(ex, "Event Hub Library Sender - SendEvents");
    }
    finally
    {
        await eventHubClient.CloseAsync();
    }

    return "";
}
static void method()
{
    List<object> list = new List<object>();
    list.Add("dogrudur");
    Utilities.EventHub.Sender.SendEvents(list);
}
该方法现在将不再等待继续,但作为回报,您将永远不知道事件发送何时完成。一般来说,应避免火灾和遗忘方法

提高性能的另一个重要步骤是成批发送事件。目前,每个事件都是使用
eventHubClient.sendsync
发送的,但也有一个
eventHubClient.SendBatchAsync
方法(),因此,与发送大量小消息相比,发送的大消息可以少于包含多个事件的消息。但请注意,存在最大消息大小

有关使用批处理发送事件的示例实现,请参阅此文件中的方法
private async Task SendAutoSizedBatchAsync(IEnumerable collection)

static void method()
{
    List<object> list = new List<object>();
    list.Add("dogrudur");
    Utilities.EventHub.Sender.SendEvents(list);
}