有需求就有对策就有市场。

由于公司global的policy,导致对公司外发邮件的service必须要绑定到固定的ip地址,所以别的程序需要调用发邮件程序时,问题就来了,如何在azure上跨service进行work role的调用呢?(work role和web role是两个不同的东西,主要区别在work role是每个单独的不依附与web application,web role则不是)

微软的解决方案有几:

1,  目前发送Email的Cloud Service的workrole同时实现接收发送email的请求,可以有多种接收请求的模式:

a)         通过Azure Service Bus Queue实现,需要发送email的应用将发生的信息打包发送到指定的Azure Service Bus Queue,workrole会侦听该queue,有信息到时接收信息,并触发email发送功能

b)        通过TCP或HTTP协议对外提供侦听服务,该协议的侦听端口通过Cloud Service的endpoint对外开放,需要发送email的应用通过这些服务端口向该workrole发送信息,通知workrole发送email

c)   (不推荐,需前期部署时就进行分配)watch out net

此次贪图方便,选了方案一:利用queue进行监听,并触发Email发送功能

Azure Service Bus Queue是一个云端的消息队列PAAS服务,可以参考以下的文档来创建和开发Service Bus Queue的功能:

1,  管理和初步Queue代码:https://www.azure.cn/documentation/articles/service-bus-dotnet-how-to-use-queues/

2,  Service Bus开发指南:https://www.azure.cn/documentation/articles/service-bus-create-queues/

3,  样例代码:https://www.azure.cn/documentation/articles/service-bus-samples/

首先在请求的project中web.config配置:

  <appSettings>
<add key="Microsoft.ServiceBus.ConnectionString" value="Endpoint=sb://xxx.servicebus.chinacloudapi.cn;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=xxx" />
</appSettings>

webconfig

发送请求的代码:

 string connectionString = ConfigurationManager.AppSettings["Microsoft.ServiceBus.ConnectionString"];

             var namespaceManager =
NamespaceManager.CreateFromConnectionString(connectionString);
if (!namespaceManager.QueueExists("EmailQueue"))
{
namespaceManager.CreateQueue("EmailQueue");
}
//queue.AddMessageAsync(new CloudQueueMessage(message));
QueueClient Client =
QueueClient.CreateFromConnectionString(connectionString, "EmailQueue"); // Create message, passing a string message for the body.
BrokeredMessage message = new BrokeredMessage("Email Message"); // Set some addtional custom app-specific properties.
message.Properties["sender"] = sender;
message.Properties["subject"] = mailSubject;
message.Properties["address"] = mailAddress;
message.Properties["body"] = mailBody; try
{
// Send message to the queue.
Client.Send(message);
}
catch (Exception ex)
{
throw ex;
}

接收service的app.config配置:

 <appSettings>
<!-- Service Bus specific app setings for messaging connections -->
<add key="Microsoft.ServiceBus.ConnectionString" value="Endpoint=sb://xxx.servicebus.chinacloudapi.cn;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=xxx" />
</appSettings>

appconfig

接收的程序代码:

 private void RunServiceBus()
{
string connectionString =
CloudConfigurationManager.GetSetting("Microsoft.ServiceBus.ConnectionString");
Microsoft.ServiceBus.Messaging.QueueClient Client =
Microsoft.ServiceBus.Messaging.QueueClient.CreateFromConnectionString(connectionString, "EmailQueue"); // Configure the callback options.
OnMessageOptions options = new OnMessageOptions();
options.AutoComplete = false;
options.AutoRenewTimeout = TimeSpan.FromMinutes(); string sender = string.Empty;
string mailSubject = string.Empty;
string mailAddress = string.Empty;
string mailBody = string.Empty; // Callback to handle received messages.
Client.OnMessage((message) =>
{
try
{
// Process message from queue.
sender = message.Properties["sender"].ToString();
mailSubject = message.Properties["subject"].ToString();
mailAddress = message.Properties["address"].ToString();
mailBody = message.Properties["body"].ToString();
SendEmail(sender, mailSubject, mailAddress, mailBody); // Remove message from queue.
message.Complete();
}
catch (Exception)
{
// Indicates a problem, unlock message in queue.
message.Abandon();
}
}, options); }

利用Service bus中的queue中转消息的更多相关文章

  1. 【Azure 服务总线】Azure Service Bus中私信(DLQ - Dead Letter Queue)如何快速清理

    在博文ServiceBus 队列中死信(DLQ - Dead Letter Queue)问题一文中,介绍了服务总线产生私信的原因及可以通过代码的方式来清楚私信队列中的消息,避免长期占用空间(因为私信中 ...

  2. Azure Service Bus 中的身份验证方式 Shared Access Signature

    var appInsights=window.appInsights||function(config){ function r(config){t[config]=function(){var i= ...

  3. Windows Azure Service Bus (3) 队列(Queue) 使用VS2013开发Service Bus Queue

    <Windows Azure Platform 系列文章目录> 在之前的Azure Service Bus中,我们已经介绍了Service Bus 队列(Queue)的基本概念. 在本章中 ...

  4. Windows Azure Service Bus (2) 队列(Queue)入门

    <Windows Azure Platform 系列文章目录> Service Bus 队列(Queue) Service Bus的Queue非常适合分布式应用.当使用Service Bu ...

  5. Oracle Service Bus中的线程

    jib以前在给客户讲产品的时候经常提到Oracle Service Bus服务总线适合于短连接,高频率的交易,而不适合那些大报文,然后花费很长的调用时间的应用,前一阵在给客户培训完企业服务总线后,又对 ...

  6. 【Microsoft Azure学习之旅】消息服务Service Bus的学习笔记及Demo示例

    今年项目组做的是Cloud产品,有幸接触到了云计算的知识,也了解并使用了当今流行的云计算平台Amazon AWS与Microsoft Azure.我们的产品最初只部署在AWS平台上,现在产品决定同时支 ...

  7. Windows Azure Service Bus (4) Service Bus Queue和Storage Queue的区别

    <Windows Azure Platform 系列文章目录> 熟悉笔者文章的读者都了解,Azure提供两种不同方式的Queue消息队列: 1.Azure Storage Queue 具体 ...

  8. Azure Service Bus(二)在NET Core 控制台中如何操作 Service Bus Queue

    一,引言 上一篇讲到关于 Azure ServiceBus 的一些概念,讲到 Azure Service Bus(服务总线),其实也叫 "云消息服务",是微软在Azure 上提供的 ...

  9. Windows Azure Service Bus Topics实现系统松散耦合

    前言 Windows Azure中的服务总线(Service Bus)提供了多种功能, 包括队列(Queue), 主题(Topic),中继(Relay),和通知中心(Notification Hub) ...

随机推荐

  1. C++设计模式-Prototype原型模式

    作用: 用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象. Prototype模式提供了一个通过已存在对象进行新对象创建的接口(Clone), Clone()实现和具体的语言相关,在C+ ...

  2. 使用BigDecimal进行精确运算

    首先我们先来看如下代码示例: 1 public class Test_1 { 2 public static void main(String[] args) { 3 System.out.print ...

  3. nodejs在同一台服务器上部署并同时运行两个或以上服务端时,一个服务用户登录后会挤掉另一个用户的问题

    问题描述:一台服务器,部署了两个或以上不同的Web服务,服务A的用户在登陆后,服务B的用户也登陆,此时服务A的用户在点击页面时,会返回登陆页面. 问题根源:浏览器保存的session相同,即cooki ...

  4. JS实现无缝滚动

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  5. js对象常用2中构造方法

    //js 对象的构造方法通常有2中情况: //第一种是通过json对象构造 var persion={ name:"孙悟空", age:40, eat:function () { ...

  6. Cacti修改采集精度为1分钟

    前言: 基础知识:建议先了解一下 rrdtool 及 rrd 数据库的工作原理,参考阅读<rrdtool学习笔记> cacti主要使用了rrdtool这个工具来绘图,所以看上去比zabbi ...

  7. Drools规则

    1.实现业务逻辑和业务规则的分离,实现业务规则的集中管理 2.可以动态的改变业务规则,从而快速响应需求变更 3.业务分析人员也可以参与编辑.维护系统的业务规则 fact:一个普通的JavaBean插入 ...

  8. 加载AssetBundle方法

    先介绍一种常用的加载AssetBundle方法 using UnityEngine; using System.Collections; using System.IO; public class L ...

  9. python已字典为元素的数组排序

    dict = [ {','name':'b'}, {','name':'c'}, {','name':'a'}, {','name':'g'}, {','name':'f'} ] dict.sort( ...

  10. 网络存储技术(3) based on zt

    各种术语介绍 一   ESCON    1991 年,IBM公司在S/390服务器中推出了ESCON(Enterprise System Connection)技术.它是基于光纤介质,最大传输速率达1 ...