Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 917437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T18:12:43+00:00 2026-05-15T18:12:43+00:00

I have a WCF Windows service with an endpoint specified in the config file

  • 0

I have a WCF Windows service with an endpoint specified in the config file for the service.

<baseAddresses>
    <add baseAddress="net.tcp://localhost:9000/MyEndpoint"/>
</baseAddresses>

All works ok. However, in some cases port 9000 might already be in use which causes the ServiceHost to fall over on Open().
I need to be able to OVERRIDE the default base address specified in the config file in code. e.g. assume an environment variable contains the port number to be used.

Is there any way to do this programmatically?

After the ServiceHost is constructed I can see the BaseAddresses property which returns the Uri list taken from the config file. However, this is a read only collection so can’t be used to change the defaults.

If I specify a replacement Uri in the ServiceHost constructor I get

This collection already contains an
address with scheme net.tcp. There
can be at most one address per scheme
in this collection. If your service is
being hosted in IIS you can fix the
problem by setting
‘system.serviceModel/serviceHostingEnvironment/multipleSiteBindingsEnabled’
to true or specifying
‘system.serviceModel/serviceHostingEnvironment/baseAddressPrefixFilters’.

If I create a CustomServiceHost and try and set the replacement base address I get the same error.

class CustomHost : ServiceHost
{
    public CustomHost(Type serviceType) : base (serviceType)         
    {
    }
    protected override void ApplyConfiguration()
    {
        base.ApplyConfiguration();

        this.AddBaseAddress(new Uri("net.tcp://localhost:9010/MyEndpoint"));
    }
}

I know that if I leave the config file base addresses blank and pass the base address into the ServiceHost constructor then that works fine – i.e. I can specify the new bases. However, I want to use the config file to specify the default (instead of hard coding).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T18:12:44+00:00Added an answer on May 15, 2026 at 6:12 pm

    Take a look at this example I posted. It has an entire working example of a WCF service configured completely via code. You should be able to then just grab the port number using Environment.GetEnvironmentVariable and pass it into the constructor of the ServiceHost:

    Possible to have same contract, same binding, same address, but different ports?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a WCF service embedded into windows service. It's bind to localhost but
I have a WCF service that I call from a windows service. The WCF
I have a wcf application hosted in a windows service running a local windows
I have a WCF service that is hosted in a windows application. The service
I have a WCF service that I have to reference from a .net 2.0
I current have a solution with an Azure WCF service and a Windows Phone
I have a WCF Web Service which is referenced from a class library. After
I have a WCF service hosted for internal clients - we have control of
I have a WCF service and I want to expose it as both a
I have a WCF service running on the IIS with a ServiceHostFactory. It's running

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.