ServiceActivationElement.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //-----------------------------------------------------------------------------
  4. namespace System.ServiceModel.Configuration
  5. {
  6. using System;
  7. using System.Configuration;
  8. using System.Diagnostics;
  9. using System.IO;
  10. using System.ServiceModel.Diagnostics.Application;
  11. using System.Text;
  12. public sealed partial class ServiceActivationElement : ConfigurationElement
  13. {
  14. const string PathSeparatorString = "/";
  15. const string ReversSlashString = @"\";
  16. class RelativeAddressValidator : ConfigurationValidatorBase
  17. {
  18. public override bool CanValidate(Type type)
  19. {
  20. return type == typeof(string);
  21. }
  22. // we support relativeAddress with formats as fileName.extension and ~/fileName.extension
  23. public override void Validate(object value)
  24. {
  25. string relativeAddress = value as string;
  26. // the size of relativeAddress cannot be smaller than 3 as it must have extension
  27. if (string.IsNullOrEmpty(relativeAddress) || string.IsNullOrEmpty(relativeAddress.Trim()) || relativeAddress.Length < 3)
  28. {
  29. throw FxTrace.Exception.AsError(new ArgumentException(SR.GetString(SR.Hosting_RelativeAddressFormatError, relativeAddress)));
  30. }
  31. //user gives an absolute address, throw, as we do not support absolute address
  32. if (relativeAddress.StartsWith(PathSeparatorString, StringComparison.CurrentCultureIgnoreCase)
  33. || relativeAddress.StartsWith(ReversSlashString, StringComparison.CurrentCultureIgnoreCase))
  34. {
  35. throw FxTrace.Exception.AsError(new ArgumentException(SR.GetString(SR.Hosting_NoAbsoluteRelativeAddress, relativeAddress)));
  36. }
  37. }
  38. }
  39. [AttributeUsage(AttributeTargets.Property)]
  40. sealed class RelativeAddressValidatorAttribute : ConfigurationValidatorAttribute
  41. {
  42. public override ConfigurationValidatorBase ValidatorInstance
  43. {
  44. get
  45. {
  46. return new RelativeAddressValidator();
  47. }
  48. }
  49. }
  50. public ServiceActivationElement()
  51. {
  52. }
  53. public ServiceActivationElement(string relativeAddress)
  54. : this()
  55. {
  56. if (relativeAddress == null)
  57. {
  58. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(ConfigurationStrings.RelativeAddress);
  59. }
  60. this.RelativeAddress = relativeAddress;
  61. }
  62. public ServiceActivationElement(string relativeAddress, string service)
  63. : this(relativeAddress)
  64. {
  65. this.Service = service;
  66. }
  67. public ServiceActivationElement(string relativeAddress, string service, string factory)
  68. : this(relativeAddress, service)
  69. {
  70. this.Factory = factory;
  71. }
  72. [ConfigurationProperty(ConfigurationStrings.RelativeAddress, Options = ConfigurationPropertyOptions.IsRequired | ConfigurationPropertyOptions.IsKey)]
  73. [RelativeAddressValidator()]
  74. public string RelativeAddress
  75. {
  76. get { return (string)base[ConfigurationStrings.RelativeAddress]; }
  77. set
  78. {
  79. base[ConfigurationStrings.RelativeAddress] = value;
  80. }
  81. }
  82. [ConfigurationProperty(ConfigurationStrings.Service, Options = ConfigurationPropertyOptions.None)]
  83. [StringValidator(MinLength = 0)]
  84. public string Service
  85. {
  86. get { return (string)base[ConfigurationStrings.Service]; }
  87. set
  88. {
  89. if (string.IsNullOrEmpty(value))
  90. {
  91. value = string.Empty;
  92. }
  93. base[ConfigurationStrings.Service] = value;
  94. }
  95. }
  96. [ConfigurationProperty(ConfigurationStrings.Factory, Options = ConfigurationPropertyOptions.None)]
  97. [StringValidator(MinLength = 0)]
  98. public string Factory
  99. {
  100. get { return (string)base[ConfigurationStrings.Factory]; }
  101. set
  102. {
  103. if (string.IsNullOrEmpty(value))
  104. {
  105. value = string.Empty;
  106. }
  107. base[ConfigurationStrings.Factory] = value;
  108. }
  109. }
  110. }
  111. }