从 WCF 消息检查器发送到服务总线事件中心

Sending to Service Bus Event Hubs from a WCF Message Inspector

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

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 添加端点,以便能够将数据发送到服务总线事件中心。如果可以,配置如何?

提前致谢

背景:

ServiceBus SDK 有 2 个主要接口:

  1. NamespaceManager:用于所有管理操作 (aka Control Plane),例如创建删除 Topics/EHubs 等
  2. EntityClients(TopicClient、EventHubClient 等):用于运行时操作 (aka Data Plane) - 从 Topics/EventHubs 到 Send/Recv。

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

您只使用 EventHub 名称创建了 EventHubClient,但没有在那里传递连接字符串。在这种情况下,上述错误是从我们的 ServiceBus 客户端 sdk 中抛出的 - 当连接字符串未通过 app.config 传递时。 要解决此问题,请更改此行(因为您直接为 NamespaceManager 使用 ConnectionString 而不是使用任何 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!斯里

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