【问题标题】:Sending to Service Bus Event Hubs from a WCF Message Inspector从 WCF 消息检查器发送到服务总线事件中心
【发布时间】:2015-04-10 10:27:22
【问题描述】:

我有一个有效的网络服务和测试客户端,我可以截取它们之间的消息。但是当我添加要发送到我的事件中心的代码时,客户端显示错误:

An unhandled exception of type 'System.ServiceModel.FaultException`1' occurred in mscorlib.dll

Additional information: The argument Endpoints is null or empty.

Parameter name: Endpoints

更详细的异常:

System.ServiceModel.FaultException`1[System.ServiceModel.ExceptionDetail]:    The argument Endpoints is null or empty.
Parameter name: Endpoints (Fault Detail is equal to An ExceptionDetail,  likely created by IncludeExceptionDetailInFaults=true, whose value is:
System.ArgumentException: The argument Endpoints is null or empty.
Parameter name: Endpoints
at Microsoft.ServiceBus.ServiceBusConnectionStringBuilder.Validate()
at Microsoft.ServiceBus.ServiceBusConnectionStringBuilder.ToString()
at  Microsoft.ServiceBus.Messaging.Configuration.KeyValueConfigurationManager.
Initialize(String connection, Nullable`1 transportType)
at Microsoft.ServiceBus.Messaging.Configuration.KeyValueConfigurationManager.
.ctor(Nullable`1 transportType)
at Microsoft.ServiceBus.Messaging.EventHubClient.Create(String path)
at WCFInterceptor.MessageInspector.AfterReceiveRequest(Message& request,  ICli
entChannel channel, InstanceContext instanceContext)
at  System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.AfterReceiveReques
tCore(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(Me
ssageRpc& rpc)
at System.ServiceModel.Dispatc...).

这是我添加的代码:

try
        {
            NamespaceManager namespaceManager = NamespaceManager.CreateFromConnectionString(GetServiceBusConnectionString());
            Manage.CreateEventHub(hubName, 16, namespaceManager);
        }
        catch (Exception e)
        {
            Console.WriteLine("SetupEHError" + e);
        }
        EventHubClient client = EventHubClient.Create(hubName);
        Console.WriteLine("eventhubclient iniciado");
        EventData messageData = new EventData(Encoding.UTF8.GetBytes(serializedString));

        try
        {
            client.Send(messageData);
            Console.WriteLine("MessageData enviada");
        }
        catch (Exception e)
        {

            Console.WriteLine("ErrorMessage:" + e);
        }

这里是 CreateEventHub 方法:

public static void CreateEventHub(string eventHubName, int        numberOfPartitions, NamespaceManager manager)
    {
        try
        {
            // Create the Event Hub
            Console.WriteLine("Creating Event Hub...");
            EventHubDescription ehd = new EventHubDescription(eventHubName);
            ehd.PartitionCount = numberOfPartitions;
            manager.CreateEventHubIfNotExistsAsync(ehd).Wait();
            Console.WriteLine("Created");
        }
        catch (AggregateException agexp)
        {
            Console.WriteLine(agexp.Flatten());
        }
    }

WebService 控制台应用最多可打印

Creating Event Hub
Created

所以我想我可能需要为 WebService 中的 MessageInspector 添加端点,以便能够将数据发送到服务总线事件中心。如果有,配置如何?

提前致谢

【问题讨论】:

  • 您使用的是服务总线还是事件中心的密钥,您是否为事件中心添加了管理权限??
  • 这是我的事件中心的管理权限是的
  • 我遇到了同样的问题,但我发现我使用的是服务中心的密钥而不是我的事件中心,我放下了 SDK 并使用 The Rest API 实现了我自己的简单事件中心创建功能它奏效了
  • 如果您取消了 sdk 并使用 http 帖子将您的事件发送到集线器,您是如何处理 SAS 关键问题的?因为它有一个 TIL
  • 如果在我的情况下可以这样做(让检查员发送消息),我更喜欢使用 sdk。另外,您确定不需要端点,因为“端点参数为空或无效”?如果这有任何意义

标签: web-services wcf endpoints idispatchmessageinspector azure-eventhub


【解决方案1】:

背景

ServiceBus SDK 有 2 个主要接口:

  1. NamespaceManager:用于所有管理操作 (aka Control Plane),例如创建删除主题/EHub 等
  2. EntityClients(TopicClient、EventHubClient 等):用于运行时操作 (aka Data Plane) - 从 Topics/EventHubs 发送/接收。

这两个接口都需要自己的连接字符串来连接到 ServiceBus。例如:指定给 NamespaceManager 的连接字符串需要 ManageClaims,而 EntityClients 只需要 Send/Recv 声明。

您仅使用 EventHub 名称创建了 EventHubClient 并且没有在那里传递连接字符串。在这种情况下,我们的 ServiceBus 客户端 sdk 会抛出上述错误 - 当连接字符串未通过 app.config 传递时。 要解决此问题,请更改此行(因为您将 ConnectionString 直接用于 NamespaceManager 而没有使用任何 app.config):

EventHubClient client = EventHubClient.Create(hubName);

改成:

----edit-----
    var eHubConnStr = GetServiceBusConnectionString();
    eHubConnStr.EntityPath = eventHubName;
    // Evaluate here, if you have to populate the Security related properties from the ConnectionString
    // eHubConnStr.SasKey and SasKeyName to Send only or Recv only
----edit-----
    EventHubClient client = EventHubClient.CreateFromConnectionString(eHubConnStr); // this connection string should be the EventHub Send conn str.

HTH!斯里

【讨论】:

  • 我已经完成了这些更改,但现在我得到一个不同的错误:事件中心名称应在 connectionString 中指定为 EntityPath。参数名称:entityPath(Fault Detail 等于 An ExceptionDetail,可能由 IncludeExceptionDetailInFaults=true 创建,其值为:System.ArgumentNullException:EventHub 名称应指定为 connectionString 中的 EntityPath。参数名称:Microsoft.ServiceBus.Messaging.EventHubClient 处的 entityPath .CreateFromConnectionString(S string connectionString)
【解决方案2】:

实际上,我需要做的就是使用连接字符串编辑我的 Web 服务服务器的应用程序配置。似乎 eventthubclient 的 Create 方法采用 eventthub 名称,然后转到 appconfig 获取密钥,因此没有找到它。

【讨论】:

  • 这种方法的一个缺点是 - 用于创建 EventHub 的 NamespaceMgr 和用于运行时操作的 EventHubClient 使用相同的安全密钥!这对于小型示例服务通常很好,但对于任何生产就绪代码 - 我建议隔离安全角色。您可以查看最新的编辑 - 这有助于实现这一目标。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-01-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多