Search code examples
c#wcfwcf-configurationbase-address

WCF base address configuration for different Server implementations


I don't know if it is repeated question or not but due to my search I couldn't find anything related.

I have made a WCF service and it works fine. Now I want to deploy it and use it as a reference in my project which is going to be used in different places.

when I want to install my Software for the client in his server I need to change the base address to the appropriate IP Address in the server. also, there could be different IP Addresses in different servers for different client and I don't want to deploy a wcf service with its specific base address for each client.

Could you make any suggestions and tell me what is the best solution in this area.

Please

In my Service I use two base address like this :

<add baseAddress="localhost:8080/"/>;

And another one like the same but with net.tcp binding. I want to know instead of localhost how can I use IP address when my service is deploying in clients servers


Solution

  • In your application's project source code in your Visual Studio, you will have an app.config or a web.config or a similar .config file that you have added. I assume that in your deployment code (your Windows Service, PowerShell or whatever), you are creating a copy of this .CONFIG file on your target server or machine.

    Therefore, in your original (project source code version), you have tokens instead of the actual server names or IP addresses. For instance, if your actual hostname is "localhost", instead of having

    <add baseAddress="localhost:8080/" />
    

    You should have:

    <add baseAddress="[ServerName]:[Port]/" />
    

    Where "[ServerName]" and "[Port]" are tokens to be replaced later. In your deployment script, look for these tokens and replace them (simple String Replace) with the actual server's DNS name or IP address. You could get server name or IP address from whatever mechanism you are using to deploy to that server from your Windows Service.

    Hope it helps, let me know if you need more help.