none
How to enable the ServiceHost to generate the web page for the web service?

    Question

  • If I use the following traditional way of seting up a web service, a web page can be shown when I navigate the URI in the browser.

        ServiceHost host = new ServiceHost(typeof(MyService), new Uri("http://temp.org"));
        host.Open();
    
        <system.serviceModel>
        <behaviors>
         <serviceBehaviors>
           <behavior name="metadataBehavior">
             <serviceMetadata httpGetEnabled="true"/>
           </behavior>
         </serviceBehaviors>
        </behaviors>
        <services>
         <service behaviorConfiguration ="metadataBehavior" name="MyNamespace.MyService">
          <endpoint binding="basicHttpBinding" contract="MyNamespace.IMyService"/>
         </service>
        </services>
        </system.serviceModel>
    However, when I cutomized the ServiceHost in order to load WCF configuration from another file (as the following ConfigurableServiceHost), and I open the cutomized host and navigate the URI, the web page is not shown in the browser. It shows the WSDL instead.

        public class ConfigurableServiceHost : ServiceHost
        {
            private string _configurationPath;
    
            public ConfigurableServiceHost(string configurationPath, Type serviceType, params Uri[] baseAddresses)
            {
                _configurationPath = configurationPath;
                base.InitializeDescription(serviceType, new UriSchemeKeyedCollection(baseAddresses));
            }
    
            protected override void ApplyConfiguration()
            {
                ExeConfigurationFileMap filemap = new ExeConfigurationFileMap();
                filemap.ExeConfigFilename = _configurationPath;
    
                Configuration config = ConfigurationManager.OpenMappedExeConfiguration(filemap, ConfigurationUserLevel.None);
                ServiceModelSectionGroup serviceModel = ServiceModelSectionGroup.GetSectionGroup(config);
    
                foreach (ServiceElement se in serviceModel.Services.Services)
                {
                    if (se.Name == this.Description.ConfigurationName)
                    {
                        base.LoadConfigurationSection(se);
                    }
                }
            }
        }

    I use the same configuration as the one I used the ServiceHost case before, and I use the following code to open the ConfigurableServiceHost.

        ConfigurableServiceHosthost = new ConfigurableServiceHost("MyConfigFile.xml", typeof(MyService), new Uri("http://temp.org"));
        host.Open();

    The ConfigurableServiceHost works well, except not generating the web page. Does anyone here can explain why? Thanks a lot.

    Friday, November 20, 2009 7:47 AM

Answers

  • Please clarify your scenario. Based on your custom Service Host, I can see the web page which supplies the link to view service metadata.


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.ServiceModel;
    using System.ServiceModel.Description;
    using System.Configuration;
    using System.ServiceModel.Configuration;
    namespace ConsoleApplication31
    {
        [ServiceContract]
        public interface ISimpleService
        {
            [OperationContract]
            string SimpleMethod(string msg);
        }
        class SimpleService : ISimpleService
        {
            public string SimpleMethod(string msg)
            {
                Console.WriteLine("The caller passed in " + msg);
                return "Hello " + msg;
            }
        }
        class Program
        {
            static void Main(string[] args)
            {
                ServiceHost host = new ConfigurableServiceHost("e:\\myconfig.xml", typeof(SimpleService), new Uri[] { new Uri("http://localhost:8006/MetadataSample") });
                try
                {
                    // Open the service host to accept incoming calls
                    host.Open();
                    // The service can now be accessed.
                    Console.WriteLine("The service is ready.");
                    Console.WriteLine("Press <ENTER> to terminate service.");
                    Console.WriteLine();
                    Console.ReadLine();
                    // Close the ServiceHostBase to shutdown the service.
                    host.Close();
                }
                catch (CommunicationException commProblem)
                {
                    Console.WriteLine("There was a communication problem. " + commProblem.Message);
                    Console.Read();
                }
            }
        }
        public class ConfigurableServiceHost : ServiceHost
        {
            private string _configurationPath;
            public ConfigurableServiceHost(string configurationPath, Type serviceType, params Uri[] baseAddresses)
            {
                _configurationPath = configurationPath;
                base.InitializeDescription(serviceType, new UriSchemeKeyedCollection(baseAddresses));
            }
            protected override void ApplyConfiguration()
            {
                ExeConfigurationFileMap filemap = new ExeConfigurationFileMap();
                filemap.ExeConfigFilename = _configurationPath;
                Configuration config = ConfigurationManager.OpenMappedExeConfiguration(filemap, ConfigurationUserLevel.None);
                ServiceModelSectionGroup serviceModel = ServiceModelSectionGroup.GetSectionGroup(config);
                foreach (ServiceElement se in serviceModel.Services.Services)
                {
                    if (se.Name == this.Description.ConfigurationName)
                    {
                        base.LoadConfigurationSection(se);
                    }
                }
            }
        }
    }

    e:\myconfig.xml

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <system.serviceModel>
        <services>
          <service
              name="ConsoleApplication31.SimpleService"
              behaviorConfiguration="SimpleServiceBehavior">     

            <endpoint address=""
                      binding="wsHttpBinding"
                      contract="ConsoleApplication31.ISimpleService" />

            <endpoint address="mex"
                      binding="mexHttpBinding"
                      contract="IMetadataExchange" />
          </service>
        </services>

        <behaviors>
          <serviceBehaviors>
            <behavior name="SimpleServiceBehavior">
              <serviceMetadata httpGetEnabled="True" policyVersion="Policy12" />
              <serviceDebug includeExceptionDetailInFaults="False" />
            </behavior>
          </serviceBehaviors>
        </behaviors>

      </system.serviceModel>

    </configuration>
    Best regards,
    Riquel
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Wednesday, November 25, 2009 4:56 AM