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

由于公司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. Strint类成员

    String& String::operator=(const string& other){ if(this == &other) {  return *this; } de ...

  2. C++设计模式-Command命令模式

    Command命令模式作用:将一个请求封装为一个对象,从而使你可用不同的请求对客户进行参数化:对请求排队或记录请求日志,以及支持可撤销的操作. 由于“行为请求者”与“行为实现者”的紧耦合,使用命令模式 ...

  3. CRM客户关系管理系统 北京易信软科信息技术有限公司

    北京易信软科信息技术有限公司 推出大型erp系统,库存管理系统,客户关系管理系统,车辆登记管理系统,员工管理系统,采购管理系统,销售管理系统,为您的企业提供最优质的产品服务 北京易信软科您可信赖的北京 ...

  4. qt之mapx组件编程c2248和c2512错误

    mapx组件利用qt工具dumpcpp到处头文件和.cpp文件后将其加入到新建的qt项目中即可. 不过本人遇到问题知道今天偶然的解决了.记下来,以免忘记. demo的项目结构如下: 然后在.pro文件 ...

  5. spring资料

    spring的官方文档还是很全面的: http://link.zhihu.com/?target=http%3A//docs.spring.io/spring/docs/current/spring- ...

  6. 分享45个android实例源码,很好很强大

    分享45个android实例源码,很好很强大 http://www.apkbus.com/android-20978-1-1.html 分享45个android实例源码,很好很强大http://www ...

  7. CSS3 动画实现 animation 和 transition 比较

    在 CSS3 中有两种方式实现动画, 分别是 animation 和 transition, 他们都有以下功能 根据特定 CSS 属性进行动画 设定属性变化的 timing function 设定动画 ...

  8. SSH Tunneling

    把本地端口 local_port 转发到服务器 server2 的 remote_port 端口上, server1 和 server2可以是同一ip或者不同ip. ssh user@server1 ...

  9. Ninject之旅之一:理解DI

    摘要: DI(IoC)是当前软件架构设计中比较时髦的技术.DI(IoC)可以使代码耦合性更低,更容易维护,更容易测试.现在有很多开源的依赖反转的框架,Ninject是其中一个轻量级开源的.net DI ...

  10. CSV文件的规范

    CSV文件,全程Comma-separated values,就是逗号分隔的数据文件.常用于数据集成的数据交换部分标准部分. 最近看到一个项目组在讨论接口文件CSV的规范,真是替他们着急.讨论点: 文 ...