none
overriding an endpoint address specified in the app.config file RRS feed

  • Question

  • I'm trying to override the endpoint address specified in my service's app.config file with the following code:

    URI uri = new Uri(txtUrl.Text);
    ServiceHost serviceHost = new ServiceHost(typeof(MyService), uri);
    serviceHost.Open();

    When I view my service's log in Service Trace Viewer, I'm seeing that the service listens at both the address I specify in app.config AND the address I specify from the code shown above.  How do I cause the service to listen ONLY at the code specified in code?

    Thanks in advance for any help.

    Tuesday, April 12, 2011 8:27 PM

Answers

  • You need to override ServiceHost otherwise default service host will pickup and register the service from your configuration file. 

    You need to something like the following 

    public class MyServiceHost: ServiceHost
    
    {
    protected override void ApplyConfiguration()
      {
        // skip this line to not apply default config 
        base.ApplyConfiguration();
    
        base.Description.Endpoints.Clear();
      }
    
      public MyServiceHost(object singletonInstance, params Uri[] baseAddresses) : base(singletonInstance, baseAddresses)
      {
    
      }
    }
    

    Now you need to add your endpoint 

    MyServiceHost m_SvcHost = new MyServiceHost(this);
        
    
          m_SvcHost.AddServiceEndpoint(typeof(IMobileMonitoringSvc),
            new BasicHttpBinding(),
            urit);
        
        // open the svchost and allow incoming connections
        m_SvcHost.Open();
    

     


    Tanvir Huda Application Architect/Consultant
    • Marked as answer by patluze Wednesday, April 13, 2011 4:27 PM
    Wednesday, April 13, 2011 12:43 AM

All replies

  • You need to override ServiceHost otherwise default service host will pickup and register the service from your configuration file. 

    You need to something like the following 

    public class MyServiceHost: ServiceHost
    
    {
    protected override void ApplyConfiguration()
      {
        // skip this line to not apply default config 
        base.ApplyConfiguration();
    
        base.Description.Endpoints.Clear();
      }
    
      public MyServiceHost(object singletonInstance, params Uri[] baseAddresses) : base(singletonInstance, baseAddresses)
      {
    
      }
    }
    

    Now you need to add your endpoint 

    MyServiceHost m_SvcHost = new MyServiceHost(this);
        
    
          m_SvcHost.AddServiceEndpoint(typeof(IMobileMonitoringSvc),
            new BasicHttpBinding(),
            urit);
        
        // open the svchost and allow incoming connections
        m_SvcHost.Open();
    

     


    Tanvir Huda Application Architect/Consultant
    • Marked as answer by patluze Wednesday, April 13, 2011 4:27 PM
    Wednesday, April 13, 2011 12:43 AM
  • That's just what I needed -- thanks.
    Wednesday, April 13, 2011 4:27 PM