none
WCF Self hosted application and filtering base addresses RRS feed

  • Question

  • Hi,

    We have an application which hosts a WCF service. In the configuration file we have two end points defined - one for http and one for https. We want to have all the configuration in this file.

    It is a requirement for us to be able to filter the base addresses from this file. For example, one instance of the application may support HTTP, another HTTPS  and another both.

    So what is the best way for me to filter base addresses in the application so that it only loads the base addresses that it is allowed to support? I was hoping to have a config entry that defined what was supported and then the code would filter out those base addresses which were supported.

    Thanks

    Ian

    Monday, April 4, 2016 11:38 AM

Answers

  • Hi lan,

    In order to filter base addresses in the application and let it only load the base addresses that it is allowed to support, we can add the following xml in the configuration file:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
        <startup> 
            <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6" />
        </startup>
      <appSettings>
       <!--http, https, both-->
        <add key="service_type" value="http" />
        <add key="base_address" value="baseUrl" />
        <add key="secure_address" value="secureUrl" />
      </appSettings>
    ... </configuration>

    After that we can filter base address based on the type of the service_type as following:

               // Step 1 Create a URI to serve as the base address.
                string address1 = ConfigurationSettings.AppSettings["service_type"];
                Uri baseAddress = new Uri(ConfigurationSettings.AppSettings["base_address"]);
                Uri secureAddress = new Uri(ConfigurationSettings.AppSettings["secure_address"]);
    
                // Step 2 Create a ServiceHost instance
                ServiceHost selfHost = null;
                string hostType = ConfigurationSettings.AppSettings["service_type"]; 
                switch(hostType)
                {
                    case "http":
                        selfHost = new ServiceHost(typeof(MyService), baseAddress);
                        break;
                    case "https":
                        selfHost = new ServiceHost(typeof(MyService), secureAddress);
                        break;
                    case "both":
                        selfHost = new ServiceHost(typeof(MyService), baseAddress, secureAddress);
                        break;
                    default:
                        selfHost = new ServiceHost(typeof(MyService), baseAddress);
                        break;
                }            
                try
                {
                    // Step 3 Add a service endpoint.
                    if (hostType == "http" || hostType == "both") 
                    { 
                        selfHost.AddServiceEndpoint(typeof(IMyService), new BasicHttpBinding(), "BasicService");
                    }
                    if (hostType == "https" || hostType == "both") 
                    {
                        selfHost.AddServiceEndpoint(typeof(IMyService), new WSHttpBinding(), "WSHttpBinding");
                    }
    
                    // Step 4 Enable metadata exchange.
                    ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
                    smb.HttpGetEnabled = true;
                    selfHost.Description.Behaviors.Add(smb);
    
                    // Step 5 Start the service.
                    selfHost.Open();
                    Console.WriteLine("The service is ready.");
                    Console.WriteLine("Press <ENTER> to terminate service.");
                    Console.WriteLine();
                    Console.ReadLine();
    
                    // Close the ServiceHostBase to shutdown the service.
                    selfHost.Close();
                }
                catch (CommunicationException ce)
                {
                    Console.WriteLine("An exception occurred: {0}", ce.Message);
                    selfHost.Abort();
                }
    
    

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, April 5, 2016 3:14 AM
    Moderator
  • Hi lan,

    In my mind the way which I have showed in my first post to implement filtering base addresses is the easy one.

    The other way is that you can try to implement the authentication for your http and https WCF Services, in this way only the clients know the correct credential can access your http or https services.

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, April 11, 2016 5:35 AM
    Moderator

All replies

  • Hi lan,

    In order to filter base addresses in the application and let it only load the base addresses that it is allowed to support, we can add the following xml in the configuration file:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
        <startup> 
            <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6" />
        </startup>
      <appSettings>
       <!--http, https, both-->
        <add key="service_type" value="http" />
        <add key="base_address" value="baseUrl" />
        <add key="secure_address" value="secureUrl" />
      </appSettings>
    ... </configuration>

    After that we can filter base address based on the type of the service_type as following:

               // Step 1 Create a URI to serve as the base address.
                string address1 = ConfigurationSettings.AppSettings["service_type"];
                Uri baseAddress = new Uri(ConfigurationSettings.AppSettings["base_address"]);
                Uri secureAddress = new Uri(ConfigurationSettings.AppSettings["secure_address"]);
    
                // Step 2 Create a ServiceHost instance
                ServiceHost selfHost = null;
                string hostType = ConfigurationSettings.AppSettings["service_type"]; 
                switch(hostType)
                {
                    case "http":
                        selfHost = new ServiceHost(typeof(MyService), baseAddress);
                        break;
                    case "https":
                        selfHost = new ServiceHost(typeof(MyService), secureAddress);
                        break;
                    case "both":
                        selfHost = new ServiceHost(typeof(MyService), baseAddress, secureAddress);
                        break;
                    default:
                        selfHost = new ServiceHost(typeof(MyService), baseAddress);
                        break;
                }            
                try
                {
                    // Step 3 Add a service endpoint.
                    if (hostType == "http" || hostType == "both") 
                    { 
                        selfHost.AddServiceEndpoint(typeof(IMyService), new BasicHttpBinding(), "BasicService");
                    }
                    if (hostType == "https" || hostType == "both") 
                    {
                        selfHost.AddServiceEndpoint(typeof(IMyService), new WSHttpBinding(), "WSHttpBinding");
                    }
    
                    // Step 4 Enable metadata exchange.
                    ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
                    smb.HttpGetEnabled = true;
                    selfHost.Description.Behaviors.Add(smb);
    
                    // Step 5 Start the service.
                    selfHost.Open();
                    Console.WriteLine("The service is ready.");
                    Console.WriteLine("Press <ENTER> to terminate service.");
                    Console.WriteLine();
                    Console.ReadLine();
    
                    // Close the ServiceHostBase to shutdown the service.
                    selfHost.Close();
                }
                catch (CommunicationException ce)
                {
                    Console.WriteLine("An exception occurred: {0}", ce.Message);
                    selfHost.Abort();
                }
    
    

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, April 5, 2016 3:14 AM
    Moderator
  • Hi,

    Thanks for that. I was hoping that there would be some hook that I could put in to filter but use all the underlying configuration loading code. What you have done above is do it all "by hand" but if this is the only way to do it then I could do with this. It is a shame that is nothing built in to allow you to do this.

    Thanks

    Ian

    Tuesday, April 5, 2016 10:52 AM
  • Hi lan,

    In my mind the way which I have showed in my first post to implement filtering base addresses is the easy one.

    The other way is that you can try to implement the authentication for your http and https WCF Services, in this way only the clients know the correct credential can access your http or https services.

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, April 11, 2016 5:35 AM
    Moderator