Jony shah
Jony shah

Reputation: 21

baseAddresses in CreateServiceHost for WCF service

there is parameter called Uri[] baseAddresses when we create service host using CreateServiceHost method of ServiceHostFactory class.

when this method will be called ? what will be the value in baseaddresses array? from where it will be set ?

can we set it from our end ?

Thanks

Upvotes: 1

Views: 990

Answers (1)

Andriy Drozdyuk
Andriy Drozdyuk

Reputation: 61111

If you host your service on IIS or another container, it will be passed in automatically (from your config files and such).

If you host your service programmatically, it will be passed in by you. I am currently having trouble with the CreateServiceHost method itself, but if you use the standard ServiceHost class, here is how it would look (the array of Uri is what you are looking for):

using System;
using System.ServiceModel;
using System.ServiceModel.Description;
using MyApp.DatabaseService.Contracts; // IDatabase interface
using MyApp.DatabaseService.Impl; //DatabaseService

 public class Program
    {

        static void Main(string[] args)
        {
            using (ServiceHost host = new ServiceHost(
                typeof(DatabaseService),
                new Uri[]{          // <-- baseAddresses
                    new Uri("http://localhost:8111/DatabaseService"),
                    new Uri("net.pipe://localhost/DatabaseService")
                }))
            {
                host.AddServiceEndpoint(typeof(IDatabase),
                    new BasicHttpBinding(),
                    "");

                host.AddServiceEndpoint(typeof(IDatabase),
                    new NetNamedPipeBinding(),
                    "");

                // Add ability to browse through browser
                ServiceMetadataBehavior meta = new ServiceMetadataBehavior();
                meta.HttpGetEnabled = true;
                host.Description.Behaviors.Add(meta);

                host.Open();

                Console.WriteLine("IDatabase: DatabaseService");
                Console.WriteLine("Service Running. Press Enter to exit...");
                Console.ReadLine();

                host.Close();
            }
        }
    }

Upvotes: 1

Related Questions