1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
|
//-----------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
namespace System.ServiceModel.Channels
{
using System;
using System.Collections.Generic;
using System.ServiceModel;
using System.ServiceModel.Security;
using System.Transactions;
using System.Runtime.Remoting.Messaging;
using System.ServiceModel.Transactions;
using System.ServiceModel.Diagnostics;
sealed public class TransactionMessageProperty
{
TransactionInfo flowedTransactionInfo;
Transaction flowedTransaction;
const string PropertyName = "TransactionMessageProperty";
private TransactionMessageProperty()
{
}
public Transaction Transaction
{
get
{
if (this.flowedTransaction == null && this.flowedTransactionInfo != null)
{
try
{
this.flowedTransaction = this.flowedTransactionInfo.UnmarshalTransaction();
}
catch (TransactionException e)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(e);
}
}
return this.flowedTransaction;
}
}
static internal TransactionMessageProperty TryGet(Message message)
{
if (message.Properties.ContainsKey(PropertyName))
return message.Properties[PropertyName] as TransactionMessageProperty;
else
return null;
}
static internal Transaction TryGetTransaction(Message message)
{
if (!message.Properties.ContainsKey(PropertyName))
return null;
return ((TransactionMessageProperty)message.Properties[PropertyName]).Transaction;
}
static TransactionMessageProperty GetPropertyAndThrowIfAlreadySet(Message message)
{
if (message.Properties.ContainsKey(PropertyName))
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
new FaultException(SR.GetString(SR.SFxTryAddMultipleTransactionsOnMessage)));
}
return new TransactionMessageProperty();
}
static public void Set(Transaction transaction, Message message)
{
TransactionMessageProperty property = GetPropertyAndThrowIfAlreadySet(message);
property.flowedTransaction = transaction;
message.Properties.Add(PropertyName, property);
}
static internal void Set(TransactionInfo transactionInfo, Message message)
{
TransactionMessageProperty property = GetPropertyAndThrowIfAlreadySet(message);
property.flowedTransactionInfo = transactionInfo;
message.Properties.Add(PropertyName, property);
}
}
class TransactionFlowProperty
{
Transaction flowedTransaction;
List<RequestSecurityTokenResponse> issuedTokens;
const string PropertyName = "TransactionFlowProperty";
private TransactionFlowProperty()
{
}
internal ICollection<RequestSecurityTokenResponse> IssuedTokens
{
get
{
if (this.issuedTokens == null)
{
this.issuedTokens = new List<RequestSecurityTokenResponse>();
}
return this.issuedTokens;
}
}
internal Transaction Transaction
{
get { return this.flowedTransaction; }
}
static internal TransactionFlowProperty Ensure(Message message)
{
if (message.Properties.ContainsKey(PropertyName))
return (TransactionFlowProperty)message.Properties[PropertyName];
TransactionFlowProperty property = new TransactionFlowProperty();
message.Properties.Add(PropertyName, property);
return property;
}
static internal TransactionFlowProperty TryGet(Message message)
{
if (message.Properties.ContainsKey(PropertyName))
return message.Properties[PropertyName] as TransactionFlowProperty;
else
return null;
}
static internal ICollection<RequestSecurityTokenResponse> TryGetIssuedTokens(Message message)
{
TransactionFlowProperty property = TransactionFlowProperty.TryGet(message);
if (property == null)
return null;
// use this when reading only, consistently return null if no tokens.
if (property.issuedTokens == null || property.issuedTokens.Count == 0)
return null;
return property.issuedTokens;
}
static internal Transaction TryGetTransaction(Message message)
{
if (!message.Properties.ContainsKey(PropertyName))
return null;
return ((TransactionFlowProperty)message.Properties[PropertyName]).Transaction;
}
static TransactionFlowProperty GetPropertyAndThrowIfAlreadySet(Message message)
{
TransactionFlowProperty property = TryGet(message);
if (property != null)
{
if (property.flowedTransaction != null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new FaultException(SR.GetString(SR.SFxTryAddMultipleTransactionsOnMessage)));
}
}
else
{
property = new TransactionFlowProperty();
}
return property;
}
static internal void Set(Transaction transaction, Message message)
{
TransactionFlowProperty property = GetPropertyAndThrowIfAlreadySet(message);
property.flowedTransaction = transaction;
message.Properties.Add(PropertyName, property);
}
}
}
|