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 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
|
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------
namespace System.ServiceModel.Dispatcher
{
using System.Collections.ObjectModel;
using System.ServiceModel.Channels;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.ServiceModel.Transactions;
class TransactionValidationBehavior : IEndpointBehavior, IServiceBehavior
{
static TransactionValidationBehavior instance;
internal static TransactionValidationBehavior Instance
{
get
{
if (instance == null)
instance = new TransactionValidationBehavior();
return instance;
}
}
TransactionValidationBehavior() { }
void ValidateTransactionFlowRequired(string resource, string name, ServiceEndpoint endpoint)
{
bool anOperationRequiresTxFlow = false;
for (int i = 0; i < endpoint.Contract.Operations.Count; i++)
{
OperationDescription operationDescription = endpoint.Contract.Operations[i];
TransactionFlowAttribute transactionFlow = operationDescription.Behaviors.Find<TransactionFlowAttribute>();
if (transactionFlow != null && transactionFlow.Transactions == TransactionFlowOption.Mandatory)
{
anOperationRequiresTxFlow = true;
break;
}
}
if (anOperationRequiresTxFlow)
{
CustomBinding binding = new CustomBinding(endpoint.Binding);
TransactionFlowBindingElement transactionFlowBindingElement =
binding.Elements.Find<TransactionFlowBindingElement>();
if (transactionFlowBindingElement == null || !transactionFlowBindingElement.Transactions)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(
String.Format(Globalization.CultureInfo.CurrentCulture, SR.GetString(resource), name, binding.Name)));
}
}
}
void IEndpointBehavior.Validate(ServiceEndpoint serviceEndpoint)
{
if (serviceEndpoint == null)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("serviceEndpoint");
ValidateTransactionFlowRequired(SR.ChannelHasAtLeastOneOperationWithTransactionFlowEnabled,
serviceEndpoint.Contract.Name,
serviceEndpoint);
EnsureNoOneWayTransactions(serviceEndpoint);
ValidateNoMSMQandTransactionFlow(serviceEndpoint);
ValidateCallbackBehaviorAttributeWithNoScopeRequired(serviceEndpoint);
OperationDescription autoCompleteFalseOperation = GetAutoCompleteFalseOperation(serviceEndpoint);
if (autoCompleteFalseOperation != null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(
SR.GetString(SR.SFxTransactionAutoCompleteFalseOnCallbackContract, autoCompleteFalseOperation.Name, serviceEndpoint.Contract.Name)));
}
}
void ValidateCallbackBehaviorAttributeWithNoScopeRequired(ServiceEndpoint endpoint)
{
// If the endpoint has no operations with TransactionScopeRequired=true, disallow any
// transaction-related properties on the CallbackBehaviorAttribute
if (!HasTransactedOperations(endpoint))
{
CallbackBehaviorAttribute attribute = endpoint.Behaviors.Find<CallbackBehaviorAttribute>();
if (attribute != null)
{
if (attribute.TransactionTimeoutSet)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(
SR.GetString(SR.SFxTransactionTransactionTimeoutNeedsScope, endpoint.Contract.Name)));
}
if (attribute.IsolationLevelSet)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(
SR.GetString(SR.SFxTransactionIsolationLevelNeedsScope, endpoint.Contract.Name)));
}
}
}
}
void IEndpointBehavior.AddBindingParameters(ServiceEndpoint serviceEndpoint, BindingParameterCollection bindingParameters)
{
}
void IEndpointBehavior.ApplyDispatchBehavior(ServiceEndpoint serviceEndpoint, EndpointDispatcher endpointDispatcher)
{
}
void IEndpointBehavior.ApplyClientBehavior(ServiceEndpoint serviceEndpoint, ClientRuntime behavior)
{
}
void IServiceBehavior.AddBindingParameters(ServiceDescription description, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection parameters)
{
}
void IServiceBehavior.ApplyDispatchBehavior(ServiceDescription service, ServiceHostBase serviceHostBase)
{
}
void IServiceBehavior.Validate(ServiceDescription service, ServiceHostBase serviceHostBase)
{
if (service == null)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("service");
ValidateNotConcurrentWhenReleaseServiceInstanceOnTxComplete(service);
bool singleThreaded = IsSingleThreaded(service);
for (int i = 0; i < service.Endpoints.Count; i++)
{
ServiceEndpoint endpoint = service.Endpoints[i];
ValidateTransactionFlowRequired(SR.ServiceHasAtLeastOneOperationWithTransactionFlowEnabled,
service.Name,
endpoint);
EnsureNoOneWayTransactions(endpoint);
ValidateNoMSMQandTransactionFlow(endpoint);
ContractDescription contract = endpoint.Contract;
for (int j = 0; j < contract.Operations.Count; j++)
{
OperationDescription operation = contract.Operations[j];
ValidateScopeRequiredAndAutoComplete(operation, singleThreaded, contract.Name);
}
ValidateAutoCompleteFalseRequirements(service, endpoint);
}
ValidateServiceBehaviorAttributeWithNoScopeRequired(service);
ValidateTransactionAutoCompleteOnSessionCloseHasSession(service);
}
void ValidateAutoCompleteFalseRequirements(ServiceDescription service, ServiceEndpoint endpoint)
{
OperationDescription autoCompleteFalseOperation = GetAutoCompleteFalseOperation(endpoint);
if (autoCompleteFalseOperation != null)
{
// Does the service have InstanceContextMode.PerSession or Shareable?
ServiceBehaviorAttribute serviceBehavior = service.Behaviors.Find<ServiceBehaviorAttribute>();
if (serviceBehavior != null)
{
InstanceContextMode instanceMode = serviceBehavior.InstanceContextMode;
if (instanceMode != InstanceContextMode.PerSession)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(
SR.GetString(SR.SFxTransactionAutoCompleteFalseAndInstanceContextMode,
endpoint.Contract.Name, autoCompleteFalseOperation.Name)));
}
}
// Does the binding support sessions?
if (!autoCompleteFalseOperation.IsInsideTransactedReceiveScope)
{
if (!RequiresSessions(endpoint))
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(
SR.GetString(SR.SFxTransactionAutoCompleteFalseAndSupportsSession,
endpoint.Contract.Name, autoCompleteFalseOperation.Name)));
}
}
}
}
OperationDescription GetAutoCompleteFalseOperation(ServiceEndpoint endpoint)
{
foreach (OperationDescription operation in endpoint.Contract.Operations)
{
if (!IsAutoComplete(operation))
{
return operation;
}
}
return null;
}
void ValidateTransactionAutoCompleteOnSessionCloseHasSession(ServiceDescription service)
{
ServiceBehaviorAttribute serviceBehavior = service.Behaviors.Find<ServiceBehaviorAttribute>();
if (serviceBehavior != null)
{
InstanceContextMode instanceMode = serviceBehavior.InstanceContextMode;
if (serviceBehavior.TransactionAutoCompleteOnSessionClose &&
instanceMode != InstanceContextMode.PerSession)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(
SR.GetString(SR.SFxTransactionAutoCompleteOnSessionCloseNoSession, service.Name)));
}
}
}
void ValidateServiceBehaviorAttributeWithNoScopeRequired(ServiceDescription service)
{
// If the service has no operations with TransactionScopeRequired=true, disallow any
// transaction-related properties on the ServiceBehaviorAttribute
if (!HasTransactedOperations(service))
{
ServiceBehaviorAttribute attribute = service.Behaviors.Find<ServiceBehaviorAttribute>();
if (attribute != null)
{
if (attribute.TransactionTimeoutSet)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(
SR.GetString(SR.SFxTransactionTransactionTimeoutNeedsScope, service.Name)));
}
if (attribute.IsolationLevelSet)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(
SR.GetString(SR.SFxTransactionIsolationLevelNeedsScope, service.Name)));
}
if (attribute.ReleaseServiceInstanceOnTransactionCompleteSet)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(
SR.GetString(SR.SFxTransactionReleaseServiceInstanceOnTransactionCompleteNeedsScope, service.Name)));
}
if (attribute.TransactionAutoCompleteOnSessionCloseSet)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(
SR.GetString(SR.SFxTransactionTransactionAutoCompleteOnSessionCloseNeedsScope, service.Name)));
}
}
}
}
void EnsureNoOneWayTransactions(ServiceEndpoint endpoint)
{
CustomBinding binding = new CustomBinding(endpoint.Binding);
TransactionFlowBindingElement txFlowBindingElement = binding.Elements.Find<TransactionFlowBindingElement>();
if (txFlowBindingElement != null)
{
for (int i = 0; i < endpoint.Contract.Operations.Count; i++)
{
OperationDescription operation = endpoint.Contract.Operations[i];
if (operation.IsOneWay)
{
TransactionFlowAttribute tfbp = operation.Behaviors.Find<TransactionFlowAttribute>();
TransactionFlowOption transactions;
if (tfbp != null)
{
transactions = tfbp.Transactions;
}
else
{
transactions = TransactionFlowOption.NotAllowed;
}
if (TransactionFlowOptionHelper.AllowedOrRequired(transactions))
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(
SR.GetString(SR.SFxOneWayAndTransactionsIncompatible, endpoint.Contract.Name, operation.Name)));
}
}
}
}
}
bool HasTransactedOperations(ServiceDescription service)
{
for (int i = 0; i < service.Endpoints.Count; i++)
{
if (HasTransactedOperations(service.Endpoints[i]))
{
return true;
}
}
return false;
}
bool HasTransactedOperations(ServiceEndpoint endpoint)
{
for (int j = 0; j < endpoint.Contract.Operations.Count; j++)
{
OperationDescription operation = endpoint.Contract.Operations[j];
OperationBehaviorAttribute attribute = operation.Behaviors.Find<OperationBehaviorAttribute>();
if (attribute != null && attribute.TransactionScopeRequired)
{
return true;
}
}
return false;
}
bool IsSingleThreaded(ServiceDescription service)
{
ServiceBehaviorAttribute attribute = service.Behaviors.Find<ServiceBehaviorAttribute>();
if (attribute != null)
{
return (attribute.ConcurrencyMode == ConcurrencyMode.Single);
}
// The default is ConcurrencyMode.Single
return true;
}
bool IsAutoComplete(OperationDescription operation)
{
OperationBehaviorAttribute attribute = operation.Behaviors.Find<OperationBehaviorAttribute>();
if (attribute != null)
{
return attribute.TransactionAutoComplete;
}
// The default is TransactionAutoComplete=true
return true;
}
bool RequiresSessions(ServiceEndpoint endpoint)
{
return endpoint.Contract.SessionMode == SessionMode.Required;
}
void ValidateScopeRequiredAndAutoComplete(OperationDescription operation,
bool singleThreaded,
string contractName)
{
OperationBehaviorAttribute attribute = operation.Behaviors.Find<OperationBehaviorAttribute>();
if (attribute != null)
{
if (!singleThreaded && !attribute.TransactionAutoComplete)
{
string id = SR.SFxTransactionNonConcurrentOrAutoComplete2;
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(
SR.GetString(id, contractName, operation.Name)));
}
}
}
void ValidateNoMSMQandTransactionFlow(ServiceEndpoint endpoint)
{
BindingElementCollection bindingElements = endpoint.Binding.CreateBindingElements();
if (bindingElements.Find<TransactionFlowBindingElement>() != null &&
bindingElements.Find<MsmqTransportBindingElement>() != null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(
SR.GetString(SR.SFxTransactionFlowAndMSMQ, endpoint.Address.Uri.AbsoluteUri)));
}
}
void ValidateNotConcurrentWhenReleaseServiceInstanceOnTxComplete(ServiceDescription service)
{
ServiceBehaviorAttribute attribute = service.Behaviors.Find<ServiceBehaviorAttribute>();
if (attribute != null && HasTransactedOperations(service))
{
if (attribute.ReleaseServiceInstanceOnTransactionComplete && !IsSingleThreaded(service))
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
new InvalidOperationException(SR.GetString(
SR.SFxTransactionNonConcurrentOrReleaseServiceInstanceOnTxComplete, service.Name)));
}
}
}
}
}
|