我在 Asp.net Web API 中开发了一个应用程序。它在部署模式下发送和接收消息非常好。但是,当我在本地 IIS 上部署此应用程序时,ID 不会收到消息。 API 调用良好,但在 Azure Ques 上接收消息时绑定(bind)的接收消息事件未触发。
这是我的代码:
public IHttpActionResult ReadServiceBusMessage(string outId)
{
int _outId = Convert.ToInt32(outId);
OutletId = _outId;
var OutletObj = (from ou in db.tblOutlets where ou.Id == _outId select ou).FirstOrDefault();
CompanyId = OutletObj.CompanyId;
bool? IsOMSAvailable = OutletObj.IsOMSAvailable;
if (IsOMSAvailable != null && IsOMSAvailable == false)
{
return Ok("OMSAvailable is false");
}
string sbConnectionString = System.Configuration.ConfigurationManager.AppSettings["AzureReadPrimaryConnectionString"];
string sbQueueName = System.Configuration.ConfigurationManager.AppSettings["AzureReadSubQueName"];
try
{
queueClient = new QueueClient(sbConnectionString, sbQueueName);
var messageHandlerOptions = new MessageHandlerOptions(ExceptionReceivedHandler)
{
MaxConcurrentCalls = 1,
AutoComplete = false
};
Console.WriteLine("I am here before register");
queueClient.RegisterMessageHandler(ReceiveMessagesAsync, messageHandlerOptions);
Console.WriteLine("I am here after register");
//return Ok("I am here");
//Console.Read();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
LogError(ex);
return Ok(ex);
}
finally
{
//if (param != "login")
//{
Console.ReadLine();
//}
queueClient.CloseAsync();
}
return Ok();
}
static async Task ReceiveMessagesAsync(Message message, CancellationToken token)
{
Console.WriteLine("I am here");
Guid Ord = new Guid();
SendNotificationToClient(Ord);
//GenerateException(0);
try
{
var OutletObj = (from ou in db.tblOutlets where ou.Id == OutletId select ou).FirstOrDefault();
CompanyId = OutletObj.CompanyId;
bool? IsOMSAvailable = OutletObj.IsOMSAvailable;
if (IsOMSAvailable != null && IsOMSAvailable == false)
{
return;
}
await queueClient.CompleteAsync(message.SystemProperties.LockToken);
var obj = Encoding.UTF8.GetString(message.Body);
if (obj.Contains("ThirdPartyOms"))
{
AzureServiceBusThirdPartyMessageModel _objx = JsonConvert.DeserializeObject<AzureServiceBusThirdPartyMessageModel>(obj.ToString());
AddRecievedDataFromThirdPartyToRespectiveTables(_objx);
}
else
{
AzureServiceBusMessageModel[] _obj = Newtonsoft.Json.JsonConvert.DeserializeObject<AzureServiceBusMessageModel[]>(obj);
AddRecievedDataToRespectiveTables(_obj);
}
//var test = _obj.ToString();
//tblOrder ord = Newtonsoft.Json.JsonConvert.DeserializeObject<tblOrder>(obj);
Console.WriteLine($"Received message: {Encoding.UTF8.GetString(message.Body)}");
//UNCOMMENT BELOW CODE TO GENERATE EXCEPTION, SO THAT MESSSAGE WILL BE ADDED IN DEAD LETTER QUEUE
//int i = 0;
//i = i / Convert.ToInt32(message);
}
catch (Exception ex)
{
LogError(ex);
if (ex.Message == "Input string was not in a correct format.")
{
AzureServiceBusMessageResponseModel azureResponse = new AzureServiceBusMessageResponseModel
{
Code = MessageCode.IncorrectDataFormat,
Message = ex.Message
};
que.SendResponseMessageToServiceBus(azureResponse, OutletId);
}
if (ex.Message == "String was not recognized as a valid DateTime.")
{
AzureServiceBusMessageResponseModel azureResponse = new AzureServiceBusMessageResponseModel
{
Code = MessageCode.IncorrectDataFormat,
Message ="Order DateTime is in wrong format.Correct format is dd/mm/yyyy hh:mm:ss"
};
que.SendResponseMessageToServiceBus(azureResponse, OutletId);
}
await queueClient.AbandonAsync(message.SystemProperties.LockToken);
//var serviceListner = new AzureBusServiceQueListinerController();
//serviceListner.LogError(ex);
}
}
最佳答案
检查您的连接字符串是否有足够的权限来接收消息。将 TransportType=AmqpWebSockets
附加到连接字符串的末尾并且它起作用了。
更多详情,可以引用类似issue .
关于.net - 部署在本地 IIS 中时,azure 消息传递服务未接收消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64317893/