ServiceInfo.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. namespace System.ServiceModel.Administration
  5. {
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Collections.ObjectModel;
  9. using System.Globalization;
  10. using System.IO;
  11. using System.Runtime;
  12. using System.ServiceModel;
  13. using System.ServiceModel.Description;
  14. sealed class ServiceInfo
  15. {
  16. KeyedByTypeCollection<IServiceBehavior> behaviors;
  17. EndpointInfoCollection endpoints;
  18. ServiceHostBase service;
  19. string serviceName;
  20. internal ServiceInfo(ServiceHostBase service)
  21. {
  22. this.service = service;
  23. this.behaviors = service.Description.Behaviors;
  24. this.serviceName = service.Description.Name;
  25. this.endpoints = new EndpointInfoCollection(service.Description.Endpoints, this.ServiceName);
  26. }
  27. public string ConfigurationName
  28. {
  29. get { return this.service.Description.ConfigurationName; }
  30. }
  31. public string DistinguishedName
  32. {
  33. get { return this.serviceName + "@" + this.FirstAddress; }
  34. }
  35. public string FirstAddress
  36. {
  37. get
  38. {
  39. Fx.Assert(null != this.Service.BaseAddresses, "");
  40. string address = "";
  41. if (this.Service.BaseAddresses.Count > 0)
  42. {
  43. Fx.Assert(null != this.Service.BaseAddresses[0], "");
  44. address = this.Service.BaseAddresses[0].ToString();
  45. }
  46. else if (this.Endpoints.Count > 0)
  47. {
  48. Uri addressUri = this.Endpoints[0].Address;
  49. if (null != addressUri)
  50. {
  51. address = addressUri.ToString();
  52. }
  53. }
  54. return address;
  55. }
  56. }
  57. public string Name
  58. {
  59. get { return this.serviceName; }
  60. }
  61. public string Namespace
  62. {
  63. get { return this.service.Description.Namespace; }
  64. }
  65. public string ServiceName
  66. {
  67. get { return this.serviceName; }
  68. }
  69. public ServiceHostBase Service
  70. {
  71. get
  72. {
  73. return this.service;
  74. }
  75. }
  76. public KeyedByTypeCollection<IServiceBehavior> Behaviors
  77. {
  78. get
  79. {
  80. return this.behaviors;
  81. }
  82. }
  83. public CommunicationState State
  84. {
  85. get
  86. {
  87. return this.Service.State;
  88. }
  89. }
  90. public EndpointInfoCollection Endpoints
  91. {
  92. get
  93. {
  94. return this.endpoints;
  95. }
  96. }
  97. public string[] Metadata
  98. {
  99. get
  100. {
  101. string[] result = null;
  102. ServiceMetadataExtension metadataExtension = service.Extensions.Find<ServiceMetadataExtension>();
  103. if (null != metadataExtension)
  104. {
  105. Collection<string> metadataStrings = new Collection<string>();
  106. try
  107. {
  108. foreach (MetadataSection section in metadataExtension.Metadata.MetadataSections)
  109. {
  110. using (StringWriter sw = new StringWriter(CultureInfo.InvariantCulture))
  111. {
  112. if (section.Metadata is System.Web.Services.Description.ServiceDescription)
  113. {
  114. System.Web.Services.Description.ServiceDescription metadata = (System.Web.Services.Description.ServiceDescription)section.Metadata;
  115. metadata.Write(sw);
  116. metadataStrings.Add(sw.ToString());
  117. }
  118. else if (section.Metadata is System.Xml.XmlElement)
  119. {
  120. System.Xml.XmlElement metadata = (System.Xml.XmlElement)section.Metadata;
  121. using (System.Xml.XmlWriter xmlWriter = System.Xml.XmlWriter.Create(sw))
  122. {
  123. metadata.WriteTo(xmlWriter);
  124. metadataStrings.Add(sw.ToString());
  125. }
  126. }
  127. else if (section.Metadata is System.Xml.Schema.XmlSchema)
  128. {
  129. System.Xml.Schema.XmlSchema metadata = (System.Xml.Schema.XmlSchema)section.Metadata;
  130. metadata.Write(sw);
  131. metadataStrings.Add(sw.ToString());
  132. }
  133. else
  134. {
  135. metadataStrings.Add(section.Metadata.ToString());
  136. }
  137. }
  138. }
  139. }
  140. catch (InvalidOperationException e)
  141. {
  142. metadataStrings.Add(e.ToString());
  143. }
  144. result = new string[metadataStrings.Count];
  145. metadataStrings.CopyTo(result, 0);
  146. }
  147. return result;
  148. }
  149. }
  150. }
  151. }