Wednesday, June 2, 2010

WCF Base Address

Many of us have seen baseaddresses tag in WCF configuration files, it plays a very important role in the entire service hosting scenario. Just by knowing how to use, you save lot of time

Base address comes as follows in config




BaseAddress define the root address for your service whether it is mexendpoint or client endpoint . In the above example if you look at the address attribute of first endpoint node it is blank, then how come you are able to access this end point surprised? Its all because of baseAddress (try removing baseAddress it will give you an error). So take from this is incase you do not specify any value inside endpoint address it takes the value of baseAddress.
But not in all cases you would love this scenario, you might want to define you own address for endpoint or you might want to have different endpoint address for different clients. For that you just need to write the address in endpoint as shown above in endpoint node 2, it will automatically be taken as relative URI which mean what ever you write will be appended at the end of base address ( so your new endpoint address = baseAddress + endpoint address).


Biggest advantage is maintainability, when ever you migrate your services from Dev - QA - Production it makes changes very simple and less error prone.

But always remember for one service you can only have one base address for that scheme ( http, net/tcp). For example


Invalid declaration you can not have two base addresses under same service with same scheme



Valid Declaration, you can have two base address for one service with different schemes

No comments: