Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.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
.net 当事件中心关闭或连接字符串错误时,EventHubClient.CreateFromConnectionString/SendAsync如何创建_.net_Azure_Azure Eventhub - Fatal编程技术网

.net 当事件中心关闭或连接字符串错误时,EventHubClient.CreateFromConnectionString/SendAsync如何创建

.net 当事件中心关闭或连接字符串错误时,EventHubClient.CreateFromConnectionString/SendAsync如何创建,.net,azure,azure-eventhub,.net,Azure,Azure Eventhub,我有一个.net应用程序通过以下方式写入事件中心: 在以下情况下,此代码的行为如何: 1.我的名字错了 2.connectionString具有错误的SharedAccessKeyName 3.事件中心不可用 文档相当稀少,在我的测试中,似乎没有什么失败。CreateFromConnectionString返回一个有效的“EventHubClient”对象,SendAsync仅返回一个 更新12/18:.NET framework 如果您按照使用事件中心的.NET framework,那么在我之

我有一个.net应用程序通过以下方式写入事件中心:

在以下情况下,此代码的行为如何: 1.我的名字错了 2.connectionString具有错误的SharedAccessKeyName 3.事件中心不可用


文档相当稀少,在我的测试中,似乎没有什么失败。CreateFromConnectionString返回一个有效的“EventHubClient”对象,SendAsync仅返回一个

更新12/18:.NET framework

如果您按照使用事件中心的.NET framework,那么在我之前的回答中,它仍然具有与事件中心的.NET核心相同的错误消息

在您发布的代码中:

var eventHubClient = EventHubClient.CreateFromConnectionString(connectionString, eventHubName);
EventHubEvent e = new EventHubEvent() { ... }; 
eventHubClient.SendAsync(new EventData(Encoding.UTF8.GetBytes(serializer.Serialize(e))));
当使用不正确的eventhub\u名称或不正确的SharedAccessKeyName时,您无法看到错误消息的原因是,当您使用方法
SendAsync
时,您没有指定async/Wait关键字

如果要在使用
SendAsync
方法时查看错误消息,应使用以下代码进行测试:

namespace Sender
{
    class Program
    {
        static string eventHubName = "your_eventhub_name";
        static string connectionString = "your_namespace_connection_string";
        static void Main(string[] args)
        {
            SendingRandomMessages();

            Console.WriteLine("**completed**");
            Console.ReadLine();
        }

        static async void SendingRandomMessages()
        {
            var eventHubClient = EventHubClient.CreateFromConnectionString(connectionString, eventHubName);
            for(int i=0;i<5;i++)
            {
                try
                {
                    var message = Guid.NewGuid().ToString()+": ddd";
                    Console.WriteLine("{0} > Sending message: {1}", DateTime.Now, message);
                    //eventHubClient.Send(new EventData(Encoding.UTF8.GetBytes(message)));
                    await eventHubClient.SendAsync(new EventData(Encoding.UTF8.GetBytes(message)));
                }
                catch (Exception exception)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("{0} > Exception: {1}", DateTime.Now, exception.Message);
                    Console.ResetColor();
                }

                Thread.Sleep(200);
            }
        }

    }
}
名称空间发送器
{
班级计划
{
静态字符串eventHubName=“您的\u eventhub\u name”;
静态字符串connectionString=“您的\u名称空间\u连接\u字符串”;
静态void Main(字符串[]参数)
{
SendingAndommissages();
Console.WriteLine(“**已完成**”);
Console.ReadLine();
}
静态异步void sendingAndomMessages()
{
var eventHubClient=eventHubClient.CreateFromConnectionString(connectionString,eventHubName);

对于(int i=0;iThanks Ivan,但这是针对.net核心应用程序的。我的应用程序是一个旧的.net framework应用程序。@GregBalajewicz,请查看帖子中我的更新部分,如果您还有更多问题,请告诉我。谢谢Ivan,好的,所以当使用async时,它将自动失败,除非我等待任务。谢谢您的确认
namespace Sender
{
    class Program
    {
        static string eventHubName = "your_eventhub_name";
        static string connectionString = "your_namespace_connection_string";
        static void Main(string[] args)
        {
            SendingRandomMessages();

            Console.WriteLine("**completed**");
            Console.ReadLine();
        }

        static async void SendingRandomMessages()
        {
            var eventHubClient = EventHubClient.CreateFromConnectionString(connectionString, eventHubName);
            for(int i=0;i<5;i++)
            {
                try
                {
                    var message = Guid.NewGuid().ToString()+": ddd";
                    Console.WriteLine("{0} > Sending message: {1}", DateTime.Now, message);
                    //eventHubClient.Send(new EventData(Encoding.UTF8.GetBytes(message)));
                    await eventHubClient.SendAsync(new EventData(Encoding.UTF8.GetBytes(message)));
                }
                catch (Exception exception)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("{0} > Exception: {1}", DateTime.Now, exception.Message);
                    Console.ResetColor();
                }

                Thread.Sleep(200);
            }
        }

    }
}