简体   繁体   中英

Can multiple web.config files (in subdirs) refer to root code?

I feel like there's a really obvious answer to this question, but my Google-fu is proving too weak to find it.

I have a .NET application on a sole test server/environment, APPSVR for sake of argument. The webservices that are called by this app reside in four different environments (SVC1, SVC2, etc.). Every month I get an email from QA asking me to point the app to the correct services environment for that month's testing. It's a 15-second process (modifying the web.config file for the app, below) but it's annoying for me and it keeps them from doing their jobs until I see the email and get it done.

<applicationSettings>
  <CM_MVC.Properties.Settings>
    <setting name="CM_MVC_CmsService_cms_webservice" serializeAs="String">
      <value>http://SVC1/cms-webservice/cms-webservice</value>
    </setting>
  </CM_MVC.Properties.Settings>
 </applicationSettings>

What I want to do is set things up so they can go to http://APPSVR/app/SVC# to have the app pointing to the correct services environment. Ideally, though, I'd prefer not to have to actually publish the entire app to four different directories each time we make changes (which is infrequent, but still...).

Everything I can find about putting additional web.config files in subdirs also shows (or implies, as far as I can tell) those subdirs having distinct pages or workflows. I don't have any of that. I just need the one app, one set of pages and code, but a way to have that one setting different.

Is there a way to have a web.config file in a subdir refer to the app's root code?

Or, alternately, is there some other solution to this problem that doesn't require monthly micro-management or multiple copies/deploys of identical code?

There are a couple of solutions.

1) use a file server that is named the same across all environments and store the app settings there. This requires you to have dns entries in each environment that point to seperate file servers.

2) you can actually just have the service named the same, and again, use dns to route across the environments

3) you can deploy this part of the app settings in each environment once and stop deploying it as part of the solution every time you need to release

4) you can use a combination of custom code and/or configSource="c:\\..."

I am not exactly sure what you mean by refer to app's root code though.

If I understand correctly, I think you just need to write some code to decide on the URL based on some property... for now we'll say the querystring, although you might want to have a more secure method.

I would:

  1. Add a new Setting to the config
  2. Add the setting to the settings file
  3. Rename the existing webservice
  4. Override the renamed webservice with the old name
  5. Write some code to do the URL-switch.

  1. Add the setting to the config: http://SVC1/cms-webservice/cms-webservice http://SVC1/cms-webservice/cms-test-webservice

  2. Add the new setting into your project/Properties/Settings.settings file

  3. Rename the class generated by Visual Studio by highlighting it in Solution Explorer, pressing F2, and giving it a different name, eg CmsWebServiceBase

  4. Now override the web service class that was generated by Visual Studio:

     public class CmsWebService : CmsWebServiceBase{ } 
  5. Code switching logic in constructor:

     public CmsWebService(){ // figure out if we should use Test or Live if( HttpContext.Current != null ){ if( HttpContext.Current.Request.QueryString["test"] == "true" ){ HttpContext.Current.Session["test"] = "true"; } if( HttpContext.Current.Session["test"] == "true" ){ this.Url = MyProject.Properties.Settings.CM_MVC_CmsService_cms_webservice_test; } } } 

Note that this will cause problems if you do async processing, or anything else fancy where you use the WebService but Session is not available.

Hope that helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM