TransactedBatchingBehavior.cs 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //-----------------------------------------------------------------------------
  4. namespace System.ServiceModel.Description
  5. {
  6. using System.ServiceModel.Dispatcher;
  7. using System.ServiceModel.Channels;
  8. public class TransactedBatchingBehavior : IEndpointBehavior
  9. {
  10. int maxBatchSize;
  11. public TransactedBatchingBehavior(int maxBatchSize)
  12. {
  13. if (maxBatchSize < 0)
  14. {
  15. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("maxBatchSize", maxBatchSize,
  16. SR.GetString(SR.ValueMustBeNonNegative)));
  17. }
  18. this.maxBatchSize = maxBatchSize;
  19. }
  20. public int MaxBatchSize
  21. {
  22. get { return this.maxBatchSize; }
  23. set
  24. {
  25. if (value < 0)
  26. {
  27. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("value", value,
  28. SR.GetString(SR.ValueMustBeNonNegative)));
  29. }
  30. this.maxBatchSize = value;
  31. }
  32. }
  33. void IEndpointBehavior.Validate(ServiceEndpoint serviceEndpoint)
  34. {
  35. BindingElementCollection bindingElements = serviceEndpoint.Binding.CreateBindingElements();
  36. bool transactedElementFound = false;
  37. foreach (BindingElement bindingElement in bindingElements)
  38. {
  39. ITransactedBindingElement txElement = bindingElement as ITransactedBindingElement;
  40. if (null != txElement && txElement.TransactedReceiveEnabled)
  41. {
  42. transactedElementFound = true;
  43. break;
  44. }
  45. }
  46. if (! transactedElementFound)
  47. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SfxTransactedBindingNeeded)));
  48. }
  49. void IEndpointBehavior.AddBindingParameters(ServiceEndpoint serviceEndpoint, BindingParameterCollection bindingParameters)
  50. {
  51. }
  52. void IEndpointBehavior.ApplyDispatchBehavior(ServiceEndpoint serviceEndpoint, EndpointDispatcher endpointDispatcher)
  53. {
  54. if (endpointDispatcher.DispatchRuntime.ReleaseServiceInstanceOnTransactionComplete)
  55. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxNoBatchingForReleaseOnComplete)));
  56. if (serviceEndpoint.Contract.SessionMode == SessionMode.Required)
  57. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxNoBatchingForSession)));
  58. }
  59. void IEndpointBehavior.ApplyClientBehavior(ServiceEndpoint serviceEndpoint, ClientRuntime behavior)
  60. {
  61. if (serviceEndpoint.Contract.SessionMode == SessionMode.Required)
  62. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxNoBatchingForSession)));
  63. behavior.CallbackDispatchRuntime.ChannelDispatcher.MaxTransactedBatchSize = this.MaxBatchSize;
  64. }
  65. }
  66. }