c# - 向事件中心发送消息时 Windows 服务失败给出连接错误
问题描述
我有一个有趣的问题。
我已将 Windows 服务部署到 azure 虚拟机。这台机器拥有一个 Windows 服务,它将向 azure 事件中心发送消息。
此问题仅在此特定 VM 和她的姐妹上开始发生(因为我正在为水平扩展能力进行配置)。我刚刚配置了它,并在上面安装了服务。同样的服务,当从我的本地机器运行时工作正常(相同的代码库,相同的构建,相同的配置......实际上是一个复制和粘贴工作)并且我可以从 vm 访问互联网(我通过 Internet Explorer 检查)
同样的服务在我退役的旧 azure vm 上运行良好。这个旧的虚拟机不是我设置的,所以我可能在新的虚拟机上遗漏了一些我未能解释的东西。
我不确定在这里放什么,但这是我用于发送的相关代码。
public class MessageSender
{
private static readonly string EventHubName = ConfigurationManager.AppSettings["EventHubName_v2_0"];
private static EventHubClient _client = EventHubClient.CreateFromConnectionString(ConfigurationManager.AppSettings["EventHubConnectionString"], EventHubName);
/// <summary>
/// Send a message to the EventHub if the app configuration has cloud messaging enabled and the event hub connection strings present
/// </summary>
/// <param name="message">The contents of the message being sent.</param>
/// <param name="clientId">The unique id of the client associated with this message.</param>
public static void Send(string message, string clientId)
{
var eventProperties = new Dictionary<string, string>
{
{Constants.LoggingProperties.MO_MESSAGE_CONTENTS, message},
{Constants.LoggingProperties.MO_MESSAGE_DESTINATION, EventHubName},
{Constants.LoggingProperties.CLIENT, clientId}
};
var startTime = DateTime.UtcNow;
var endTime = null as DateTime?;
try
{
var data = new EventData(Encoding.UTF8.GetBytes(message));
_client.Send(data); // <-- fails here
endTime = DateTime.UtcNow;
}
catch (Exception eventHubEx)
{
Log.Instance.TrackException(eventHubEx);
Console.WriteLine(eventHubEx.Message);
...
}
相关的 app.config 东西
<add key="EventHubConnectionString" value="Endpoint=sb://xxxxx.servicebus.windows.net/;SharedAccessKeyName=xxxx;SharedAccessKey=xxxx" />
<add key="EventHubName_v2_0" value="xxxx" />
这些值与来自我本地计算机的值相同。我只是更改了文件名以匹配可执行文件名。
在这一点上,我唯一的想法是这可能是一个网络问题,但我不确定我需要检查什么来验证它,或者是否有一种方法(简单或其他方式)来检查以确保连接。
任何其他想法也将受到欢迎。
谢谢你尽你所能的帮助。
解决方案
推荐阅读
- python - model.predict 类与数据集类不匹配
- android - ionic cordova build android - 构建失败并出现异常
- json.net - JToken.DeepEquals 支持自动修剪字符串值吗?
- html - dom重新加载时防止默认值不起作用
- c# - Azure 函数上的 FixedDelayRetry 属性无法正常工作
- javascript - 如何滚动到特定元素
- laravel - 在 Laravel 8 中使用 Cloudinary
- java - 解析 JSONObject 时出错
- python - 随机改变调用函数的顺序
- excel - VBA:如何移动每个后续值的值和偏移量