Using web.config variables in web.config

I would like to have a variable defined in my web.config that I can use in several places in my web.config file (and other configuration files). This is probably easier to explain with an example ...

web.config

<?xml version="1.0"?> <configuration> <appSettings> <add key="AuthServiceEndPoint" value="any_old_name_i_like"/> </appSettings> <system.web> ... <system.serviceModel> <client> <endpoint address="net.tcp://localhost/AuthService" binding="netTcpBinding" contract="MyServices.Contracts.IAuthService" name="#{AppSettings.AuthServiceEndPoint}" bindingConfiguration="netTcpBindingConfig" /> </client> </system.serviceModel> </configuration> 

windsor.config

 <?xml version="1.0" encoding="utf-8" ?> <castle> <components> ... <component id="AuthProvider" service="MyServices.Client.IAuthProvider, MyServices.Client" type="MyServices.Client.AuthProvider, MyServices.Client" lifestyle="transient"> <parameters> <endpoint>#{AppSettings.AuthServiceEndPoint}</endpoint> </parameters> </component> </components> </castle> 

Is it possible?


Edit (a bit more info)

I already have the ability to access AppSettings from my windsor.config file (which is actually handled by the castle's windsor and the custom XmlInterpreter.

The real question is: can I do this in my web.config?

 <?xml version="1.0"?> <configuration> <appSettings> <add key="AuthServiceEndPoint" value="any_old_name_i_like"/> </appSettings> <system.web> ... <system.serviceModel> <client> <endpoint address="net.tcp://localhost/AuthService" binding="netTcpBinding" contract="MyServices.Contracts.IAuthService" name="#{AppSettings.AuthServiceEndPoint}" bindingConfiguration="netTcpBindingConfig" /> </client> </system.serviceModel> </configuration> 

ie - access the variable in my <appSettings> from other parts of my web.config file.

+4
source share
4 answers

Here I answer my question again: -S

I solved this by writing NetTcpServiceLocator ...

 public interface INetTcpServiceLocator { EndpointAddress GetAddress(Type serviceType); } 

... along with a custom configuration section handler that also implements the above interface and reads in the next configuration section ...

 <services> <service contract="My.Services.TestService.Contracts.ITestService" address="net.tcp://localhost/TestService" /> </services> 

Then I created a proxy for each service ...

 public class TestServiceProxy : ITestService { public SomeInformation GetSomeInformation(SomeParams @params) { using (var factory = new NetTcpServiceFactory<ITestService>()) { var service = factory.Service; return service.GetSomeInformation(@params); } } } 

My controller has a dependency on a Service that has a dependency on ITestService. All of this is glued together with Castle Windsor and with an injection of dependency property.

So, my controller calls this Service, which in turn calls ITestService (in this case, the proxy that receives the endpoint from the user section handler).

The user section handler (which is also an INetTcpServiceLocator) has the "perWebRequest" windsor style, so it is called by the framework, and web.config is read into an array in memory. When a service proxy is called, it simply pulls the corresponding endpoint based on the type of contract.

It all depends on the type of contract, so there is no longer any need for any variables in web.config.

I went for a code-based solution, since I do not use the build process locally, only when I submit my code to subversion, the build process runs on our build server.

0
source

Not that I could think. You can do your configuration in C # in the global.asax.cs file instead of the xml file.

Alternatively, modify your web.config during the build process to replace all of these values. FinalBuilder has a neato "Edit XML File" file that XPath uses pretty well for this, and FinalBuilder has variables. The problem is solved. This is how I do my builds at work.

0
source

Above my head, I wonder if you can do it with the T4? I think that perhaps you could define a template that parses Web-Template.config and outputs Web.config? Of course, this only works for one file.

0
source

You can use NAnt or MSbuild for this. You need separate configuration files for both, but when you create a project, they can automatically perform conversions in your Web.config file and other configuration files.

0
source

All Articles