gpt4 book ai didi

c# - 如何以编程方式从 XMS XMSException 检索原因代码

转载 作者:行者123 更新时间:2023-11-30 16:37:16 25 4
gpt4 key购买 nike

我有一个 XMS MQ 客户端应用程序,它从一系列 MQ 端点提取消息。有某些原因代码可以使进程继续,而对于某些原因代码应该中止。例如,一个端点的 MQRC_Q_MGR_NOT_AVAILABLE 2059 不应中止整个过程。因此,我想检查这个原因代码。

cf = factoryFactory.CreateConnectionFactory();
foreach (Endpoint e in env.GetEndpoints())
{
Console.WriteLine("Consuming messages from endpoint {0}({1})", e.host, e.port);

// Set the properties
SetConnectionProperties(cf, e);

try
{
ReceiveMessagesFromEndpoint(cf);
}
catch (XMSException ex)
{
Console.WriteLine("XMSException caught: {0}", ex);
Console.WriteLine("Error Code: {0}", ex.ErrorCode);
Console.WriteLine("Error Message: {0}", ex.Message);
}
}

问题是 XMSException 上唯一可用的属性是 ex.ErrorCodeex.Message,它们分别是:

Error Code: CWSMQ0006

Error Message: CWSMQ0006E: An exception was received during the call to the method ConnectionFactory.CreateConnection: CompCode: 2, Reason: 2059.

我可以在消息中看到原因,但找不到检索它的方法或属性。

最佳答案

大概有两种方法可以做到

1) 您可以使用 LinkedException

类似下面的内容

    try
{
}
catch (XMSException e)
{
if(e.LinkedException!=null)
Console.WriteLine(e.LinkedException.Message);
else
Console.WriteLine(e);
}

2) 将 amqmdnet.dll 也引用到项目中并使用 MQException。类似于

    try
{
}
catch (XMSException e)
{
if(e.LinkedException!=null)
{
IBM.WMQ.MQException inner = (IBM.WMQ.MQException)e.LinkedException;
Console.WriteLine("Reason:"+ inner.ReasonCode);
}
else
Console.WriteLine(e);
}

关于c# - 如何以编程方式从 XMS XMSException 检索原因代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58956819/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com