ServiceTimeoutsBehavior.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. namespace System.ServiceModel.Description
  5. {
  6. using System.ServiceModel.Channels;
  7. using System.ServiceModel;
  8. using System.ServiceModel.Dispatcher;
  9. using System.ServiceModel.Diagnostics;
  10. using System.Runtime.Serialization;
  11. using System.Collections.ObjectModel;
  12. using System.Collections.Generic;
  13. internal class ServiceTimeoutsBehavior : IServiceBehavior
  14. {
  15. TimeSpan transactionTimeout = TimeSpan.Zero;
  16. internal ServiceTimeoutsBehavior(TimeSpan transactionTimeout)
  17. {
  18. this.transactionTimeout = transactionTimeout;
  19. }
  20. internal TimeSpan TransactionTimeout
  21. {
  22. get { return this.transactionTimeout; }
  23. set
  24. {
  25. if (value < TimeSpan.Zero)
  26. {
  27. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("value", value,
  28. SR.GetString(SR.ValueMustBeNonNegative)));
  29. }
  30. this.transactionTimeout = value;
  31. }
  32. }
  33. void IServiceBehavior.Validate(ServiceDescription description, ServiceHostBase serviceHostBase)
  34. {
  35. }
  36. void IServiceBehavior.AddBindingParameters(ServiceDescription description, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection parameters)
  37. {
  38. }
  39. void IServiceBehavior.ApplyDispatchBehavior(ServiceDescription description, ServiceHostBase serviceHostBase)
  40. {
  41. if (this.transactionTimeout != TimeSpan.Zero)
  42. {
  43. for (int i = 0; i < serviceHostBase.ChannelDispatchers.Count; i++)
  44. {
  45. ChannelDispatcher channelDispatcher = serviceHostBase.ChannelDispatchers[i] as ChannelDispatcher;
  46. if (channelDispatcher != null && channelDispatcher.HasApplicationEndpoints())
  47. {
  48. if ((channelDispatcher.TransactionTimeout == TimeSpan.Zero) ||
  49. (channelDispatcher.TransactionTimeout > this.transactionTimeout))
  50. {
  51. channelDispatcher.TransactionTimeout = this.transactionTimeout;
  52. }
  53. }
  54. }
  55. }
  56. }
  57. }
  58. }