开发学院服务器云计算 云模式:为 Windows Azure 设计和部署服务 阅读

云模式:为 Windows Azure 设计和部署服务

 2010-03-26 00:00:00 来源:WEB开发网   
核心提示: publicTransactionResponseReliableInsertMoney(AccountTransactionRequestaccountTransactionrequest){//lastparameter(true)meansthatwewanttoserialize//me

public TransactionResponse ReliableInsertMoney( 
 AccountTransactionRequest accountTransactionrequest) { 
 
//last parameter (true) means that we want to serialize 
//message to the queue as XML (serializeAsXml=true) 
 return UserAccountHandler.ReliableInsertMoney( 
  accounttransactionRequest.UserId, 
  accounttransactionRequest.AccountId, 
  accounttransactionRequest.Amount, true); 
}

UserAccountHandler 是返回 IUserAccountAction 的属性,将注入到运行时中。这使将实现与约定分离继而更改实现变得更容易:

public IUserAccountAction<Models.UserAccount> UserAccountHandler 
 {get;set;} 
 
public UserAccountService( 
 IUserAccountAction<Models.UserAccount> action) { 
 
 UserAccountHandler = action; 
}

在将消息发送到其中一个可靠的操作之后,该消息将被置于队列中。图 9 中的第一种方法显示了如何将数据存储为可序列化的 XML,第二种方法显示了如何将数据存储为队列中的字符串。请注意,Windows Azure Queues 中存在消息大小限制,最大为 8KB。

图 9 存储数据

public TransactionResponse ReliableHandleMoneyInQueueAsXml( 
 UserAccountTransaction accountTransaction){ 
 
 using (MemoryStream m = new MemoryStream()){ 
  XmlSerializer xs = 
   new XmlSerializer(typeof(UserAccountTransaction)); 
  xs.Serialize(m, accountTransaction); 
 
  try 
  { 
   QueueManager.AccountTransactionsQueue.AddMessage( 
    new CloudQueueMessage(m.ToArray())); 
   response.StatusForTransaction = TransactionStatus.Succeded; 
  } 
  catch(StorageClientException) 
  { 
   response.StatusForTransaction = TransactionStatus.Failed; 
   response.Message = 
    String.Format("Unable to insert message in the account transaction queue userId|AccountId={0}, messageId={1}", 
    accountTransaction.PartitionKey, accountTransaction.RowKey); 
  } 
 } 
 return response; 
} 
 
public TransactionResponse ReliableHandleMoneyInQueue( 
 UserAccountTransaction accountTransaction){ 
 
 TransactionResponse response = this.CheckIfTransactionExists( 
  accountTransaction.PartitionKey, accountTransaction.RowKey); 
    
 if (response.StatusForTransaction == TransactionStatus.Proceed) 
 { 
  //userid|accountid is partkey 
  //userid|accountid|transactionid|amount 
  string msg = string.Format("{0}|{1}|{2}", 
   accountTransaction.PartitionKey, 
   accountTransaction.RowKey, 
   accountTransaction.Amount); 
 
  try 
  { 
   QueueManager.AccountTransactionsQueue.AddMessage( 
    new CloudQueueMessage(msg)); 
   response.StatusForTransaction = TransactionStatus.Succeded; 
  } 
  catch(StorageClientException) 
  { 
   response.StatusForTransaction = TransactionStatus.Failed; 
   response.Message = 
    String.Format("Unable to insert message in the account transaction queue userId|AccountId={0}, messageId={1}", 
    accountTransaction.PartitionKey, accountTransaction.RowKey); 
  } 
 } 
 return response; 
}

上一页  3 4 5 6 7 8 9 10  下一页

Tags:模式 Windows Azure

编辑录入:爽爽 [复制链接] [打 印]
[]
  • 好
  • 好的评价 如果觉得好,就请您
      0%(0)
  • 差
  • 差的评价 如果觉得差,就请您
      0%(0)
赞助商链接