app.config inside Visual Studio Extension? - visual-studio-extensions

I have created a Visual Studio extension representing a Visual Studio Project wizard (vsix package). I am attempting to wire up log4net, and this has been unsuccessful. I have chased the issue down to my app.config not being loaded properly.
I have added this to my app.config in my visual studio extension:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="test" value="hello world"/>
</appSettings>
</configuration>
and within my IWizard implementation, I have added this line of code:
var test = System.Configuration.ConfigurationManager.AppSettings["test"];
However, the test variable above is always null when debugging.
I have verified these things:
App.config is set to Build Action: Content
App.config is set to Copy to Output Directory: Copy Always
App.config is set to Include in VSIX: True
App.config file is present in my C:\Users\<user>\AppData\Local\Microsoft\VisualStudio\16.0_...\Extensions\<Name>\<Company>\1.0 folder
What am I missing? And if App.config is not allowed within VSIX extension development, how would you go about wiring up log4net?

What am I missing? And if App.config is not allowed within VSIX
extension development, how would you go about wiring up log4net?
An App.Config file is copied and renamed during build to .exe.config, so only executables can have and use "App.Config" files using ConfigurationManager directly.
Usually,a VSIX project generates a DLL that is loaded in the Visual Studio executable (devenv.exe), so your project, using ConfigurationManager directly, can only read settings from the devenv.exe.config (folder C:\Program Files (x86)\Microsoft Visual Studio 16.0\Common7\IDE).
And when l test it with my only app.config file in a vsix project,the project seems to be unable to get the value of my custom files only from the default devenv.exe.config which contains only two values TestProjectRetargetTo35Allowed and EnableWindowsFormsHighDpiAutoResizing.This means that in any case, it gets values from devenv.exe.config.
Solution
1#. you can just define the new key in the devenv.exe.config file and you can get it directly from the file.
<appSettings>
<add key ="TestProjectRetargetTo35Allowed" value ="true"/>
<add key ="EnableWindowsFormsHighDpiAutoResizing" value ="true"/>
insert new key here
</appSettings>
2#. You can get this app.config by code and get the values of the keys directly from it.
ExeConfigurationFileMap configMap = new ExeConfigurationFileMap();
configMap.ExeConfigFilename = #"xxxxxxxx"; // the path of the custom app.config
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None);
var test = config.AppSettings.Settings["test"].Value;
In addition, I recommend solution2 is better and easier to solve your issue.
Hope it could help you.

Related

Why is my ASP.NET Web Site project targeting .NET Framework 4.6.1 when I have told it to target 4.8?

We have a large Visual Studio 2015 solution with several Web Site (as opposed to Web Application) projects and dozens of business-logic DLL projects. We were targeting .NET Framework 4.6.1, but I've now installed Visual Studio 2019 on my local PC and re-targeted all the projects to 4.8
When I build the solution using our existing PowerShell/MSBuild script, all the DLLs build successfully, but I get the following error when it comes to our first Web Site project:
C:\[omitted]_MyWebSite.metaproj : warning MSB3274: The primary reference
"C:\[omitted]\MyDLL.dll" could not be resolved
because it was built against the ".NETFramework,Version=v4.8"
framework. This is a higher version than the currently targeted
framework ".NETFramework,Version=v4.6.1".
Then later, when the compiler reaches some code on the site that tries to use the DLL:
c:\[omitted]MyController.cs(6): error CS0246: The type or namespace
name 'MyDLL' could not be found (are you missing a using directive or
an assembly reference?) [C:\[omitted]_MyWebSite.metaproj]
(This is just a sample reference error. In fact, all of the DLLs seem to suffer from this issue wherever they are used in the Web Site project.)
Relevant lines in the site's web.config file:
<location path="." inheritInChildApplications="false">
<system.web>
[omitted]
<httpRuntime targetFramework="4.8" requestValidationMode="2.0" maxRequestLength="10240" />
<compilation debug="true" strict="false" explicit="true" targetFramework="4.8">
I am using the following MSBuild.exe path:
C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Current\Bin\msbuild.exe -maxcpucount:1 -verbosity:detailed
Looks like it's not enough to update the web.config file for Web Site Projects. You also have to update the target in the property pages. So in Solution Explorer, find your Web Site Project, right-click > Property Pages > Build > Target Framework > change to .NET Framework 4.8.
This is independent of the web.config file and resulted in a change in my .sln file from:
ProjectSection(WebsiteProperties) = preProject
[...omitted...]
TargetFrameworkMoniker = ".NETFramework,Version%3Dv4.6.1"
to:
ProjectSection(WebsiteProperties) = preProject
[...omitted...]
TargetFrameworkMoniker = ".NETFramework,Version%3Dv4.8"

Using web.config from base folder and not bin folder

I have an asp.net mvc web page. I'm using config transforms with it. The config transform in the bin directory is correct. However, when I debug with IIS express, all the app settings being used are actually from the original web.config, not the transformed web.config in the bin folder. If I rename the web.config to something else in the root folder, debugging will halt as soon as it tries to read a connection string because it says it's not initialized. What is the problem?
Transformations only apply when you publish the project. However their is a hack to it
You can try using the below code. Your original configuration should be in Web.Base.Config
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\WebApplications\Microsoft.WebApplication.targets" />
<Target Name="BeforeBuild">
<TransformXml
Source="Web.Base.config"
Transform="Web.$(Configuration).config"
Destination="Web.config" />
</Target>
You can also refer to below URL as sometime it makes transformation twice and you need to add steps for beforebuild
https://sebnilsson.com/blog/asp-net-transform-web-config-with-debug-release-on-build/

ASP.Net Website - publishing doesn't move all files

When publishing my ASP.Net Website (not a Web Application), the publisher does not include the Web.ConnectionStrings.config file that is next to the web.config. This is required since my web config looks like this:
<connectionStrings configSource="Web.ConnectionStrings.config"/>
How can I get a File System Publish to include files that Visual Studio seems to be ignoring. Please note that this is a website created using [File] > [New Website] in Visual Studio, not a [File] > [New Project] ASP.Net site so Content=Include will not work.
Steps to reproduce:
In Visual Studio: File > New > Website..
Create the Web.ConnectionStrings.config xml document (see ConnectionStrings.config code below).
In the web config link up the Web.ConnectionStrings.config file to the Web.Config file (see Web.config code below)
Publish the website to a folder on your file system, the Web.ConnectionStrings.config doesn't move with the rest of the files.
Web.config:
<configuration>
<connectionStrings configSource="Web.ConnectionStrings.config"/>
..
Web.ConnectionStrings.config:
<?xml version="1.0" encoding="utf-8" ?>
<connectionStrings>
<add name="connString" connectionString="yourConnectionstringhere"/>
</connectionStrings>
The way you publish the website is OK.
But the name of the file into which the connectionstrings get stored must not start with the prefix web., just call it connectionstrings.config instead.
In web.config you put:
<configuration>
<connectionStrings configSource="connectionStrings.config"/>
In the renamed file connectionstrings.config you place:
<?xml version="1.0" encoding="utf-8" ?>
<connectionStrings>
<add name="connString" connectionString="yourConnectionstringhere"/>
</connectionStrings>
I think this article will help
https://learn.microsoft.com/en-us/aspnet/web-forms/overview/deployment/visual-studio-web-deployment/deploying-extra-files
Basically you edit the picture .pubxml file to tell it to include additional files during deployment

Webdeploy - precompile asp.net application error with excluded connectionstrings in excluded app_data folder

I try to use webdeploy for our asp.net application. Today I tried to activate the "Precompile during publishing" flag. It seems that this flag doesn`t work together with the "Exclude files from the App_Data folder".
Before I activated the precompiling in webdeploy everything worked fine. If I remove the configSource from my connectionStrings and run the webdeploy with precompiling it works again.
The problem is that I have to load the external connectionstring file, because it will be managed by the administrators for production-environments.
The error message I get from webdeploy is:
"An error occurred loading a configuration file: Directory 'C:\MyProject\obj\Release\AspnetCompileMerge\Source\App_Data' does not exist. Failed to start monitoring file changes."
My current setup is the following:
Web.Config:
<configuration>
<!-- Section stuff here -->
<connectionStrings configSource="App_Data\DBConnection.xml" />
<!-- More stuff here -->
</configuration>
New webdeploy settings:
Try excluding the config file from the package by adding the following MSBuild script to either your project file or a wpp.targets file:
<ItemGroup>
<ExcludeFromPackageFiles Include="App_Data\DBConnection.xml">
<FromTarget>Project</FromTarget>
</ExcludeFromPackageFiles>
</ItemGroup>
Hey this did not work for us. But we also updated our tfs in the last days and we had a look into the webdeploy parameters which can be set easily with the release Manager from our new tfs. So we just readded he connectionstrings into the web.config and than we compile everything. In the end we decided where it's going to be deployed and than we set automatically the connectionstrings, which are hidden for us Devs.

Object reference not set to an instance of an object on build web site

I have a web site that is just serving as a Remoting Server and has remoting configuration inside its web.config file.
<system.runtime.remoting>
<application>
<service>
<activated type="abc.def.ghi"/>
</service>
<channels>
<channel ref="http" machineName="localhost"/>
</channels>
</application>
</system.runtime.remoting>
Besides this web.config file, it has only these files in it:
dataConfiguration.config
enterpriseLibrary.config
log.config
website.publishproj
bin folder - which has the DLLs that are served from this project via remoting
When I build the web site, I receive build error which is:
"object reference not set to an instance of an object".
How can I debug what's causing this error and how to remove it?
Please advise.
The issue is resolved. I had a assembly reference in my web.config where there was a version mismatch between it and the assembly present inside the bin folder:
<add assembly="Oracle.DataAccess, Version=4.112.3.0, Culture=neutral, PublicKeyToken=89B483F429C47342"/>
This error appears when Target framework is not selected. enter image description here

Resources