3

I'm implementing a WCF service hosted by IIS, that impersonates the caller. When I have the service endpoint configuration in the Web.config-file everything works as intended.

I want to set the service endpoint programmatically but I'm missing something because the caller is not impersonated (the endpoint works fine except for that little detail). Is there any way I can capture the service endpoint created from web.config in code so that when debugging I can find what the difference is between this one and the one I create programmatically?

Thanks,

Christian

Christian
  • 131
  • 1
  • 11

1 Answers1

1

You can use the default service host factory to access the endpoint from web.config in your code (and possibly attach a debugger to the IIS process to see what it contains).

    public class MyServiceHostFactory : ServiceHostFactory
    {
        protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
        {
            return new MyServiceHost(serviceType, baseAddresses);
        }
    }

    public class MyServiceHost : ServiceHost
    {
        public MyServiceHost(Type serviceType, Uri[] baseAddresses)
            : base(serviceType, baseAddresses)
        {
        }

        protected override void OnOpening()
        {
            // At this point you have access to the endpoint descriptions
            foreach (var endpoint in this.Description.Endpoints)
            {
                Console.WriteLine("Endpoint at {0}", endpoint.Address.Uri);
                Binding binding = endpoint.Binding;
                BindingElementCollection elements = binding.CreateBindingElements();
                foreach (var element in elements)
                {
                    Console.WriteLine("  {0}", element);
                }
            }

            base.OnOpening();
        }
    }

And in the .svc file, specify the Factory="YourNamespace.MyServiceHostFactory" attribute.

carlosfigueira
  • 85,035
  • 14
  • 131
  • 171
  • Thank you very much! I had no problem setting this up and it works like a charm - now I 'just' have to figure out what the difference is between my endpoints -) – Christian Oct 25 '12 at 12:53