【发布时间】:2022-01-01 09:33:59
【问题描述】:
我有一个 Windows 服务。我使用 Topshelf。启动时会运行cs.Start() 方法。
static void Main(string[] args)
{
var exitCode = HostFactory.Run(x =>
{
var assembly = new AssemblyInfo(Assembly.GetAssembly(typeof(Program)));
x.Service<Services.Producer>(s =>
{
s.ConstructUsing<Services.Producer>(cs => new Services.Producer(log));
s.WhenStarted(cs => cs.Start());
s.WhenStopped(cs => cs.Stop());
});
x.EnablePauseAndContinue();
x.EnableShutdown();
x.EnableServiceRecovery(r =>
{
r.OnCrashOnly();
r.RestartService(delayInMinutes: 0);
r.RestartService(delayInMinutes: 1);
r.RestartService(delayInMinutes: 5);
r.SetResetPeriod(days: 1);
});
x.SetServiceName(System.Text.RegularExpressions.Regex.Replace(assembly.Product, @"\s+", ""));
x.SetDisplayName(assembly.ProductTitle);
x.SetDescription(assembly.Description);
x.AfterInstall(p =>
{
log.Info("xxxxxxxxxxx.");
});
x.AfterUninstall(() =>
{
log.Info("xxxxxxxxxxxx.");
});
x.OnException(ex =>
{
log.Error($"Error.", ex);
});
});
}
Start() 方法调用另一个方法。实现此异步方法见下文:
public void StartAsync(INmsService nmsService)
{
try
{
if (!nmsService.IsStarted)
{
var worker = new BackgroundWorker();
worker.WorkerSupportsCancellation = true;
worker.DoWork += (e, o) =>
{
try
{
nmsService.Start();
}
catch (Exception ex)
{
log.Error($"xxxx", ex);
}
};
worker.RunWorkerCompleted += (e, o) =>
{
};
worker.RunWorkerAsync();
}
}
catch
{
throw;
}
}
现在nmsService.Start()代码:
public void Start()
{
try
{
factory = new NMSConnectionFactory(Settings.Endpoint);
connection = factory.CreateConnection(Settings.UserName, Settings.Password);
connection.ClientId = $"{Settings.Name}Service";
session = connection.CreateSession(AcknowledgementMode.AutoAcknowledge);
destination = SessionUtil.GetDestination(session, Settings.QueueName, DestinationType.Queue);
producer = session.CreateProducer(destination);
connection.Start();
}
catch (Exception ex)
{
throw ex;
}
}
但是,当运行实现时,这是一个例外:
“无法创建 IConnectionFactory 实现:Apache.NMS.ActiveMQ.ConnectionFactory 类型的 Apache.NMS.ActiveMQ 程序集上的 CreateConnectionAsync 方法,版本=1.8.0.0,文化=中性,PublicKeyToken=82756feee3957618 没有实现。”
感谢您的帮助!
【问题讨论】:
-
没有。这不是我的问题。我创建了一个实现 NMS Connect 的项目。父项目引用此项目。以这种方式完成时,将抛出通知的异常。
标签: c# activemq message-queue producer nms