3

I actually just created an NServiceBus self-hosted endpoint and bootstrapped owin self-hosted web api 2 by adding the Microsoft.AspNet.WebApi.OwinSelfHost nuget package. It's all up and running fine and I can hit the controller endpoints that I added, but the package didn't add the normal items like the web.config.

I'd like to have the normal web.config available to where I can also add in different build configurations (the transform files like web.debug.config, web.release.config, etc).

How do I add this into my project?

I tried just adding the file, but ConfigurationManager doesn't read it.

Sinaesthetic
  • 11,426
  • 28
  • 107
  • 176

1 Answers1

6

web.config is used for asp.net web application on a hosted server. As you are using a self-hosted server then web.config is no applicable. You would need app.config which would resolve to the executable name with the .config file extension.

Add app.config to the project and ConfigurationManager should be able to read it.

UPDATE:

It was indicated that the same config transformation was also needed for app.config

The following VS tool fills in the gap left between web.config transformations.

Configuration Transform

Automatically transform app.config or any other config during build process. Once the transformation is set, it will run on other build machines without the extension.

The link includes step by step instructions on how to use it in applying configuration transformations.

Nkosi
  • 235,767
  • 35
  • 427
  • 472
  • can I do transforms with app.config? – Sinaesthetic Mar 02 '17 at 04:00
  • The same config transformation can be applied to app.config as well. VS should include it when you add it via the IDE – Nkosi Mar 02 '17 at 04:01
  • But how? Normally, you right click on the web config and there's a option to add transformations, but in this case, there is not. – Sinaesthetic Mar 02 '17 at 04:02
  • Then I may have been mistaken with the transformation. I'll do some more checking to confirm – Nkosi Mar 02 '17 at 04:05
  • 1
    It is possible. http://gunnarpeipman.com/2013/11/using-web-config-transforms-with-app-config-files/ – Nkosi Mar 02 '17 at 04:07
  • 1
    Also http://stackoverflow.com/questions/3004210/app-config-transformation-for-projects-which-are-not-web-projects-in-visual-stud – Nkosi Mar 02 '17 at 04:10
  • @Sinaesthetic but apart from the transform, the app.config is what is needed. – Nkosi Mar 02 '17 at 04:26
  • Thanks for that part. Part of the question was the transform though. Take my upvote in the meanwhile. – Sinaesthetic Mar 02 '17 at 04:27
  • @Sinaesthetic, I totally understand. Just wanted to make sure you didn't abandon the `app.config` as the transform was not work yet. There is most probably a work around. – Nkosi Mar 02 '17 at 04:29
  • Yep. I just needed to be reminded that web.config is an IIS thing. At least I have threads to pull on. Thanks again! – Sinaesthetic Mar 02 '17 at 04:31
  • I don't think this path even exists: – Sinaesthetic Mar 02 '17 at 04:45
  • Ahhhh so it turns out this extension (not slow cheetah): https://marketplace.visualstudio.com/items?itemName=GolanAvraham.ConfigurationTransform&showReviewDialog=true applies the method shown in those other SO answers, but for whatever reason it actually works – Sinaesthetic Mar 02 '17 at 05:01
  • @Sinaesthetic yep saw that one as well. You found it before I could post it. – Nkosi Mar 02 '17 at 05:06
  • Add to your answer and I'll gladly accept :). Just wanna make sure it's in the forefront in case someone else comes looking for it – Sinaesthetic Mar 02 '17 at 05:20
  • The problem with multiple config files is that you won't see in the self-hosted unit test problems that may arise when deployed to actual IIS, such as issues with application redirects missing in the Web.config. To avoid differences between test and production behavior, instead of adding an app.config to the test project I reference linked the Web.config from the web app project and used post build event copy step to rename it to target config Web.config ... copy "...\Web.config" "$(TargetPath).config" – David Burg Oct 29 '19 at 19:45