标签:window ret edm azure 加锁 tms init subscript proc
创建Service Bus能够參照:
https://azure.microsoft.com/en-gb/documentation/articles/service-bus-dotnet-how-to-use-topics-subscriptions/
Azure Service Bus做广播和消息通知功能非常合适,而且能够订阅不同的Topic(不同的消息类型或不同的聊天室)。
public class ChatClient { private string ConnStr { get { return CloudConfigurationManager.GetSetting("Microsoft.ServiceBus.ConnectionString"); } } private const string topicName = "TestTopic"; public OnMsgRecieved OnMsgRecieved; public void Subscribe(string subscriber) { var td = new TopicDescription(topicName) { MaxSizeInMegabytes = 5120, DefaultMessageTimeToLive = new TimeSpan(0, 1, 0) }; // Create a new Topic with custom settings string connectionString = CloudConfigurationManager.GetSetting("Microsoft.ServiceBus.ConnectionString"); var namespaceManager = NamespaceManager.CreateFromConnectionString(connectionString); if (!namespaceManager.TopicExists(topicName)) { namespaceManager.CreateTopic(td); } if (!namespaceManager.SubscriptionExists(topicName, subscriber)) { namespaceManager.CreateSubscription(topicName, subscriber); } SubscriptionClient Client = SubscriptionClient.CreateFromConnectionString (ConnStr, topicName, subscriber, ReceiveMode.PeekLock); // Configure the callback options OnMessageOptions options = new OnMessageOptions(); options.AutoComplete = false; options.AutoRenewTimeout = TimeSpan.FromMinutes(1); Task.Run(() => { Client.OnMessage((message) => { try { // Process message from subscription OnMsgRecieved(string.Format("{0}:{1}", message.Properties["name"], message.GetBody<string>())); } catch (Exception) { // Indicates a problem, unlock message in subscription message.Abandon(); } }, options); }); } private TopicClient _topicClient; public TopicClient TopicClient { get { if (_topicClient == null) { _topicClient = TopicClient.CreateFromConnectionString(ConnStr, topicName); } return _topicClient; } } public void SendMsg(string name, string msg) { var message = new BrokeredMessage(msg); message.Properties["name"] = name; TopicClient.Send(message); } }
1. 代码包括了Subscribe和SendMsg两个方法,注意name用于标识client的。假设两个client给一样的名字,仅仅有client1个client能收到消息。
2. 我Hard Code了Topic名称,假设须要实现多种消息广播。给不同的名字就能够了
SubscriptionClient Client = SubscriptionClient.CreateFromConnectionString (ConnStr, topicName, subscriber, ReceiveMode.PeekLock);
4. TopicClient不必每次都创建。是支持多线程环境的。
var td = new TopicDescription(topicName) { MaxSizeInMegabytes = 5120, DefaultMessageTimeToLive = new TimeSpan(0, 1, 0) };
Winform的UI逻辑代码相对简单:
public partial class Form1 : Form { ChatClient cc = new ChatClient(); public Form1() { InitializeComponent(); cc.OnMsgRecieved = (msg) => { SetText(msg); }; } private void btnSend_Click(object sender, EventArgs e) { cc.SendMsg(txtName.Text, txtMsg.Text); } delegate void SetTextCallback(string text); private void SetText(string text) { if (txtResult.InvokeRequired) { var d = new SetTextCallback(SetText); Invoke(d, new object[] { text }); } else { txtResult.Text += "\r\n" + text; } } private void btnSaveName_Click(object sender, EventArgs e) { cc.Subscribe(txtName.Text); } }
标签:window ret edm azure 加锁 tms init subscript proc
原文地址:http://www.cnblogs.com/yxysuanfa/p/7070931.html