Can you share a web config from an ASP.NET application with a unit testing project - asp.net

I have a complete ASP.NET project and am now trying to use automatic testing methods to test the code.
So, I have calls to methods which use configurationManager to call settings from Web.config for things like API keys etc..
My question is, it possible to synchronise my app.config for my test project and my web.config, so that if I update either one of them it takes effect on both projects, or will I have to do something different?
I have currently just copied what I need from the web.config, but because I work for a website we always have changing variables and I can't guarantee that if we change one that my colleagues will remember to update the corresponding key in both projects.

In the unit test project properties, you can add a pre-build step to copy the main web.config to the unit test project, overwriting the existing unit test project config file. This will do the same as you do now manually, but gaurantees the two config files will be in sync.
Alternatively, and perhaps better, delete the existing unit test config file (and exclude from the project) the click 'Add Existing Item' within the unit test project, choose the main project config file, and select 'Add As Link' instead of 'Add' (click the right hand edge of the 'add' button to achieve this). There will only be one config file on your disc, but both projects will use it.

Related

Override publish folder (url) of publish profile during the build pipeline

We have one ASP.Net solution with several projects. each project have build.pubxml with unique folder path.
For example:
In project Test we have this line inside the build.pubxml:
<publishUrl>C:\publish\SolutionName\Test</publishUrl>
In project Exam we have this line inside the build.pubxml:
<publishUrl>C:\publish\SolutionName\Exam</publishUrl>
In the build pipeline (in TFS) with have MSBuild step with this argument:
/p:PublishProfile=build.pubxml
After the build we got 2 folders - Test and Exam in C:\publish\SolutionName.
So far so good.
The problem is we have few branches, and we want to separate the publish folder for each branch, so we added .pubxml for each branch and in the build pipeline we specified the correct one. but is make are a lot of work on each new branch created and can cause mistakes.
We tried to pass the /p:publishUrl=C:\publish\BranchName in the MSBuild but then we got a one folder with all the content of Test and Exam and not two folders.
The idea is to have only one .pubxml file for each project with a parameter and pass the value in the pipeline, for example:
<publishUrl>C:\publish\$(Parameter)\Test</publishUrl>
And in the build we will pass the parameter according to the branch.
It is possible to do something like this?
It is possible to do something like this?
The answer is Yes. Since msbuild accepts Global Properties in command-line. If we define one Property in build.pubxml like <publishUrl>C:\PublishFolders\$(BranchID)\xxx(Test,Exam...)</publishUrl>, then we can simply pass the value in msbuild arguments like this:
Then we'll get Test and Exam folders under C:\PublishFolders\NewTest. Also we can choose to pass the pipeline predefined variables to the command like: /p:BranchID=$(Build.SourceBranch)...
This works for build in local machine, tfs and Azure Devops pipeline. Hope all above helps :)

web.config transform not executing on publish

I use web.config transforms all the time and when I make a new site/project, I often copy a previous site, rename the *.csproj and rename oldsite to newsite inside the csproj as necessary. I do this often, but for some reason one of my projects will not run its transforms on publish. I don't even know what to show you in this post, so I'll update the question as necessary based on what you want to see. Googling around I didn't find much. But here is what I can tell you:
1) My web.config does not have a namespace on it, my web.release.pittsburgh.config has the 'transform' namespace on it.
2) I copied the contents of a working web.release.pittsburgh.config into the none working one and it still didn't work.
3) When I publish a working site, in the output, I see some 'web config transform' output after a line like 'Publish Pipeline Collect Files Phase', and I see a step 'Transformed Web.config using...' and finally a 'Publish Pipeline Transform Phase' output line. In my none working project, I go from 'Publish Pipeline Collect Files Phase' directly to 'Publish Pipeline Transform Phase' with nothing in between.
I tried comparing the *.csproj files to look for anything obvious and I didn't see anything.
Let me know if you have any ideas or would like any specific information.
Please make sure below things are configured properly.
1) "Release.Pittsburgh" configuration is available or not. If not click on configuration manager and add new configuration.
2) Once configuration is available in the list, click on "Configuration Manager" and make sure all project configurations are set appropriately for "Release.Pittsburgh". If not, set it.
3) This last step is just to make sure transformations are getting added properly or not (Right click on web.config).
Once all are good, go for publish website.

Transform web.config on azure

The question is a follow up to this one: Generate Web.Debug config which could be debugged](Generate Web.Debug.config which could be debugged)
I have defined a transformation for web.debug.config. During compilation I see the following:
Transformed Web.config using C:\data\Main\WebRole\Web.Debug.config into
C:\data\Main\obj\obj\x64\Debug\WebRole.csproj\TransformWebConfig\ [...]
transformed\Web.config.
Checked Web.config in the specified location - it is correct (transformation succeeded)
But when I start the service in the azure emulator I get an alert that
Why does it happen? Looks that incorrect web.config is taken. Where should I specify the location of correct (transformed) file?
The key thing to realise with web.config Transforms (and is mentioned in the answer to your linked question) is that they are only part of the story.
When you build your sources, the transformed web.config file is built into the /obj/ folder, ready for deployment.
It is only the act of deploying your solution somewhere that puts the transformed config file into use - as noted in the docs:
When you deploy the Web application by using the selected build configuration and by using either a deployment package or one-click publish, the Web.config file is transformed according to your specifications.
How are you running the application after you build it? You need to publish or deploy it using one of the built in mechanisms that support web transforms to see those changes on your site.
If you are running the emulator against the original source files, they won't see the transformed web.config file - which is why typically the debug build doesn't have any transforms and you then turn off debugging with your Release build which is then deployed to production.
As you're trying to test this in the emulator you should be able to do the following:
In the Solution Explorer, ensure you've selected a file within the project that runs in the emulator.
From the Build menu, select "Publish [Project Name".
In the Publish Wizard, create a new "Profile" using the "Custom" publish target.
In the "Connection" pane select "File System" as the publish method, and give it a suitable target location.
In the "Settings" pane choose the appropriate configuration (in your case probably "Debug"), and set any other options that you'd like.
Then press "Publish", and the project should be built, and then deployed to the new file location.
You should then be able to start the emulator from this newly published location, which will be using your transformed web.config.
I have found this solution and it works perfectly
https://translate.google.co.il/translate?hl=en&sl=de&tl=en&u=http%3A%2F%2Fwww.sascha-dittmann.de%2Fpost%2FWebConfig-Transformation-im-Windows-Azure-Compute-Emulator.aspx&anno=2

Getting the WebRole module inside my Azure web role app to read web.config settings

I understand that the WebRole module inside my Web Role web app project runs inside WAIISHost.exe and the rest of the app runs inside W3WP.EXE. Therefore web.config settings cannot be read from the WebRole app domain.
This can be solved by creating a special "waiishost.exe.config" in the web project file and set the "Copy to Output Directory" property to "Copy Always".
That's fine. However, now, I have config settings in ServiceConfiguration AND web.config AND "waiishost.exe.config". This is only a minor but annoying issue though. The biggest problem is that when I publish my Azure project, ServiceConfiguration and web.config get automatically transformed into the production values whereas waiishost.exe.config does not get transformed, so I end up with development config going into the production environment. (the production env is not live yet, so not a major issue yet)
Can anyone think of any ideas as to how I can also have the Publish process transform waiishost.exe.config? Maybe I could run some kind of startup process which could simply copy and rename the web.config file to be waiishost.exe.config before waiishost.exe starts.
BTW, I cannot simply move config to the ServiceConfiguration file as I have whole config sections and connection strings which are used by third party components, like the ServiceBusConfiguration section.
Many thanks
Yes, there is.
A little manual, but is "one-time-setup" per project. Check out this and that blog posts I've made a while ago (even before you could have ServiceConfiguration files). These blog posts will give you a great idea on how to achieve your desire.

How do I test that all my expected web.config settings have been defined?

I am using the built in test framework in VS2008 and I would like be able to write a test that makes sure all the expected web.config settings have been defined so that if by accident one is removed or changed my suite of tests will detect it and not have to be tested in a runtime scenario. How would I set this up?
I do not want to setup a mockup of my web.config since I don't want to maintain two versions and this would make my test invalid anyways since I am really trying to capture the fact that the project's web.config is correct.
Any suggestions, alternatives, hints?
Solution: I ended up using the copy in the pre-build that was suggested with one change. On copy I rename the web.config to app.config so that the test project would automatically pick it up.
I tried to split out the config files as suggested as well but the problem I ran into was when the test project ran, it actually didn't run out of the bin directory (which setting the config files to 'Content' type would copy to) but instead to a results directory that has been pre defined. I could not figure out how to make it copy thos extra files to this results directory so the config files could never be found.
I'am using the pre-build event to copy working web.config to your test project directory.
Set the command line of the pre-build event of test project to string like this:
copy $(SolutionDir)\YourWebAppDir\web.config $(ProjectDir) /y
After that your tests will always run with actual web.config version.
Comment to pcampbell's answer:
I think if you use the configSource attribute you can just set it to the same path in web.config of your web app and app.config of test project and that makes not necessary to use build events.
sorry, I can't leave comments yet.
To expand on bniwredyc's answer, perhaps consider:
refactoring your web.config to reference a new config file like appSettings.config or similar.
modify your project's web.config to:
<appSettings configSource="appSettings.config" />
modify your Unit Test project's app.config to use this file as well.
modify your post or pre-build events to copy just this file.
this also helps ease of deployment in Test/Staging/Prod
Ultimately, the web.config is an XML file. You could generate a schema to validate the sections required are present and that required values have been populated. Obviously, you couldn't contextually validate any sort of business logic that the configuration might contain, but you could use a combination of an XSD validation plus a lightweight class that is used to parse conditions within the file.
Used in conjunction with a copy pre-build event you actually create a very nice test harness for your production quality configurations.

Resources