为什么使用WCF和MSMQ从邮件队列中删除邮件?

时间:2019-09-20 11:25:53

标签: c# .net wcf msmq msmq-wcf

我一直在开发WCF服务( basicHttpBinding WS绑定)和( NetMsmqBinding MSMQ ))。两者都托管在单独的Windows服务中。两种服务将执行相同的工作,但具有优先权。即HTTP服务具有优先级,该优先级将被称为( 1st Priority ),并且如果在客户端调用此方法会引发异常消息(如果HTTP服务已关闭或没有响应或在方法内引发错误)邮件将被处理到MSMQ端点(第二优先级)。

当前,HTTP和MSMQ端点工作正常。但是,如果它抛出一些错误而不是该消息应保留在同一队列中,但我希望在MSMQ中处理该消息,但将从队列中删除该消息。我该如何克服这种情况。这是我现在拥有的代码。我想念什么吗?

    [OperationBehavior(TransactionScopeRequired = true, TransactionAutoComplete = true)]
    public void ProcessToQueue(E.ProcessRequest processRequest, string QueueName)
    {
        try
        {
            //This is for testing purpose
            throw new Exception("Note: This is for testing if message remains in the queue");
            lock (lockObject)
            {
                MessageQueue messageQueue = new MessageQueue(QueueName);

                totalMessages = messageQueue.GetAllMessages();
                if (totalMessages.Length > 0)
                {
                    Logger.Logger.Instance.WriteEventLog(string.Concat("AlertFileUploadService.Queue >> Total messages remaining in queue ", QueueName, " : ", totalMessages.Length), LogType.Default);
                }                    
                if (processRequest != null && processRequest.AlertId != 1 && processRequest.Key != "Testing")
                {
                    E.ProcessToQueue processToQueue = FillProcessQueue(processRequest);
                    Logger.Logger.Instance.WriteEventLog(string.Concat("AlertFileUploadService.Queue >> Upload process started..."), LogType.Default);

                    Stopwatch stopwatch = Stopwatch.StartNew();

                    //Process to S3 server
                    E.UploadFileResponse response = M.MessageProcessor.ProcessToS3Server(processToQueue);
                    Logger.Logger.Instance.WriteEventLog(string.Concat("AlertFileUploadService.Queue >> File uploaded to server with ID >> ", processRequest.AlertId, " - success ", response.Success), LogType.Default);

                    stopwatch.Stop();

                    Logger.Logger.Instance.WriteEventLog(string.Concat("AlertFileUploadService.Queue >> Time Consumed in ProcessToS3Server >> ", stopwatch.Elapsed.TotalMilliseconds, " - success ", response.Success), LogType.Default);

                    stopwatch.Reset();
                    stopwatch = Stopwatch.StartNew();
                    //Update in Database
                    Task<long> updateId = M.MessageProcessor.UpdateProcessedData(processToQueue, response);
                    Logger.Logger.Instance.WriteEventLog(string.Concat("AlertFileUploadService.Queue >> Alert ID >> ", processRequest.AlertId, "\nKey >> ", processRequest.Key, "\nLink >>", response.PreSignedURL), LogType.Default);

                    stopwatch.Stop();

                    Logger.Logger.Instance.WriteEventLog(string.Concat("AlertFileUploadService.Queue >> Time Consumed in UpdateProcessedData >> ", stopwatch.Elapsed.TotalMilliseconds, " - success ", response.Success), LogType.Default);
                }
            }
        }
        catch (Exception ex)
        {
            Logger.Logger.Instance.WriteExceptionLog(string.Concat("AlertFileUploadService.Queue >> ", ex.Message), LogType.Default, ex);
            throw ex;
        }
    }

注意:抛出新的异常(“注意:这用于测试消息是否仍在队列中”); 此外,我已经尝试过上述 ProcessToQueue 消息,并进行了客户端的事务处理,但是没有收获。

这是我的配置。

<system.serviceModel>
<services>
  <!--Alert FileUploader(Queue) Service Start-->
  <service name="AlertFileUploadService.Queue.QueueService">
    <endpoint address="net.msmq://localhost/private/FileUpload"
              binding="netMsmqBinding"
              contract="AlertFileUploadService.IQueueService">
    </endpoint>
    <host>
      <baseAddresses>
        <add baseAddress="http://localhost:5813/FileUploadToMSMQ"/>
      </baseAddresses>
    </host>
  </service>
  <!--Alert FileUploader(Queue) Service End-->
</services>
<behaviors>
  <serviceBehaviors>
    <behavior>
      <serviceMetadata httpGetEnabled="true"/>
    </behavior>
  </serviceBehaviors>
</behaviors>
<bindings>
  <netMsmqBinding>
    <binding>
      <security mode="None"></security>
    </binding>
  </netMsmqBinding>
</bindings>

这是我从客户应用程序接听电话的方式。

queueProxyService = new QueueServiceClient();
string queueName = string.Concat(".\\Private$\\", AppConfigs.QueueInstanceName);
queueProxyService = new QueueServiceClient();
queueProxyService.ProcessToQueue(new QueueService.ProcessRequest()
{
  AlertId = result.AlertId,
  ImageBytes = req.ImageBytes,
  Key = RequestId,
  Link = ""
}, queueName);

0 个答案:

没有答案