我相信我已經找到了答案,我的兩個問題...
對於Transactional
操作,我不相信有一個「環境」的交易。我已經通過在數據庫操作之後拋出一個異常來證明這一點,並且確信無論如何都會提交數據。我想知道,如果有申報事務範圍的首選方法,即:
[OperationBehavior(TransactionScopeRequired = true)]
public void MyServiceOperation(){ ... }
//or using the TransactionScope
public void MyServiceOperation()
{
using(var transScope = new TransactionScope(...)){ ... }
}
重試功能,它看起來像你需要啓用ReceiveContext
,following this blog:
[ServiceContract]
public interface IMyService
{
[OperationContract(IsOneWay=true)]
[ReceiveContextEnabled(ManualControl = true)]
void MyServiceOperation();
// and in the service implementation:
[OperationBehavior]
public void MyServiceOperation()
{
var incomingProperties = OperationContext.Current.IncomingMessageProperties;
var property = incomingProperties[BrokeredMessageProperty.Name] as BrokeredMessageProperty;
//Complete the Message
ReceiveContext receiveContext;
if (ReceiveContext.TryGet(incomingProperties, out receiveContext))
{
//Do Something
receiveContext.Complete(TimeSpan.FromSeconds(10.0d));
}
else
{
throw new InvalidOperationException("...");
}
}
UPDATE:
挖得更深一些之後,我發現OperationContext`ç如果您使用普通香草,合同優先,IIS託管的WCF和Service Bus 1,ompletion並不是一個真正的選擇。0(不知道爲什麼,但我希望有人能提供一些線索在此)
我所發現的是有關交易行爲的唯一理智的選擇是:
[OperationBehavior]
public void MyServiceOperation()
{
using(var transScope = new TransactionScope(...))
{
DbWork();
transScope.Complete();
}
Client.SendToServiceBus(); // <-- Cannot be part of transaction, otherwise
// exceptions will be thrown!
}
的問題依然存在與MSMQ不同,這種模式不允許整個操作在向服務總線發送消息失敗時回滾。 (除非當然,有人知道更好...)
這也意味着你被迫推出自己的重試邏輯,並可能有一些機制在下一步驗證上一步已提交。 YUCK!
據我所知,工作流服務和直接處理中介消息爲您提供一些開箱即用的重試功能。但是,如果你是通過AppFabric通過IIS託管你的工作流服務的IIS,那麼微軟就會想出如何讓交易包括髮送到服務總線。 (如果有人知道該機制是什麼,請讓我知道!)
謝謝你的回答。我確實更新了我的問題,詢問開箱即用的內部機制可以事務性地處理客戶端發送部分,因此我不會將其標記爲答案。我研究了瞬態故障處理庫,但是當OperationContext和BrokeredMessages被利用時,它大部分似乎是相關的,但是我明確地問了如何做到這一點。 – Didaxis 2013-04-25 19:01:17