1

しばらくの間、あるケースで立ち往生しています。誰かが私を助けてくれることを願っています. MVC を使用して、Azure から EventHub からデータを受信し、これらのデータを ASP.NET ページに (リアルタイムで) 表示しようとしています。

イベントの受信を待機する非同期メソッドを作成する方法を見つけ、このメソッドと部分ビューを jQuery で毎秒更新しましたが、時間が経つにつれて不安定になりすぎて、無限ループで終わることがわかりました。エラーの。

これが私のコードです:

public async Task<ActionResult> Index()
    {
        time = DateTime.Now;
        list = new List<TestEntity>();
        TestEntity t = new TestEntity();
        list.Add(t);
        await Initialize();
        return View(list);
    }

public async Task<ActionResult> RefreshLatest()
    {
        try
        {
            var message = await consumer.ReceiveAsync();
            if (message != null)
            {
                TestEntity t = JsonConvert.DeserializeObject<TestEntity>(Encoding.UTF8.GetString(message.GetBytes()));
                list.Add(t);
            }
        }
        catch (Exception exception)
        {
            Console.WriteLine("exception on receive {0}", exception.Message);
        }

        return PartialView("Latest", list);
    }

 private static async Task Initialize()
    {

        EventHubClient eventHubClient = getEventHubClient(SharedAccessKeyName, SharedAccessKey, NamespaceURI, EventHubName, ConnectionString);
        EventHubConsumerGroup consumerGroup = eventHubClient.GetDefaultConsumerGroup();


        consumer = await consumerGroup.CreateReceiverAsync(partitionId, DateTime.Now, receiverEpoch); // All messages

    }

    public static EventHubClient getEventHubClient(string SharedAccessKeyName, string SharedAccessKey, string NamespaceURI, string EventHubName, string ConnectionString)
    {
        //Create EventHub
        TokenProvider td = TokenProvider.CreateSharedAccessSignatureTokenProvider(SharedAccessKeyName, SharedAccessKey);
        NamespaceManager manager = new NamespaceManager(NamespaceURI, td);
        var description = manager.CreateEventHubIfNotExists(EventHubName);

        //Create EventHubClient
        EventHubClient client = EventHubClient.CreateFromConnectionString(ConnectionString + ";EntityPath=" + EventHubName);
        return client;
    }

そして、ここに私のインデックスビューがあります:

http://puu.sh/gDQUj/cb816b8870.png

私が達成しようとしていることと、これがどのように失敗するかを理解していただければ幸いです。

4

0 に答える 0