Unable to set $(ReplacableToken...) in Web.config when publishing a Web Project - continuous-integration

I have a web project and I craeted a Test publish configuration. I use Web Deploy Package for publish method. My target is to have a replacable parameter for a setting in appSettings, so that on deployment a new setting value would be used in SetParameters.xml. I use parameters.xml file created in the root project folder with the following structure.
parameters.config
<parameters>
<parameter name="webApiUrl"
defaultValue="http://localhost:50594/">
<parameterEntry
kind="XmlFile"
scope="Web.config"
match="/configuration/appSettings/add[#key='webApiUrl']/#value" />
</parameter>
</parameters>
Web.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="webApiUrl" value="http://localhost:50594/" />
</appSettings>
</configuration>
The problem is that when the project is published the webApiUrl setting is not changed to $(ReplacableToken_webApiUrl) so when the project is deployed the value in SetParameters.xml is not taken into consideration.
I used a lot of different variations of scope and match, but non of them worked.
scope="\Web.config$", "obj\x86\Test\Package\PackageTmp\Web.config$", "\Web.config$", "\web.config$"
When I publish the project I check the folder (ProjectRoot)\obj\x86\Test\Package\PackageTmp\Web.config to see if the parametrization works.

Related

.NET Core 3.0: ASPNETCORE_ENVIRONMENT has value Development on publish

I tried to migrate my ASP.NET Core 2.2 project to the newly released ASP.NET Core 3.0 over the weekend.
Everything looked good in the local environment, but after publishing and deploying to IIS, I faced a few issues as it was using the development environment configurations.
I am using Visual Studio 2019 Community Edition version 16.3.0
Upon inspection, I found that the web.config file had the ASPNETCORE_ENVIRONMENT value set to Development, which was causing the issue. It was generated with the web publish even in Release configuration.
I thought it was supposed to be Production? Or did I miss some configuration? I‘ve never faced this issue with any earlier versions of .NET Core.
Now the issue is that if I publish the whole folder again, the issue is likely to come back.
Any solutions or suggestion regarding the root cause of the issue? My Web.config looks like the following:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<httpRuntime executionTimeout="180" />
</system.web>
<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
</handlers>
<aspNetCore processPath="dotnet" arguments=".\MyApp.dll" forwardWindowsAuthToken="false" stdoutLogEnabled="false" startupTimeLimit="3600" requestTimeout="23:00:00" hostingModel="InProcess" stdoutLogFile=".\logs\stdout">
<environmentVariables>
<environmentVariable name="ASPNETCORE_HTTPS_PORT" value="44329" />
<environmentVariable name="COMPLUS_ForceENC" value="1" />
<environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" />
</environmentVariables>
</aspNetCore>
</system.webServer>
</configuration>
Check your csproj file and remove below code if it exists.
<PropertyGroup>
<EnvironmentName>Development</EnvironmentName>
</PropertyGroup>
Also check the Properties/PublishProfiles/{profilename.pubxml}.This will set the Environment name in web.config when the project is published.
Refer to How to set aspnetcore_environment in publish file?

How do I set the environment in a Visual Studio generated publish profile

I have 2 publish profiles. I want to make one staging and one production. After browsing a bit I found out you could use the command line publish to set an environment variable: dotnet publish /p:Configuration=Release /p:EnvironmentName=Staging. However my publish profiles are generated by Visual Studio and I was wondering if I could set it in there?
Although I didn't tried this approach on .Net Core, I believe that It will work since It is the feature of VS Publish (msbuild).
you need to create web.config. Something like this:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<handlers>
<remove name="WebDAV" />
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
</handlers>
<modules>
<remove name="WebDAVModule" />
</modules>
<aspNetCore requestTimeout="00:30:00" processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout">
<environmentVariables>
<environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" />
</environmentVariables>
</aspNetCore>
</system.webServer>
</configuration>
then you need to add Config Transform for these (see below)
For the config transformation you can check this github documentation: https://github.com/vijayrkn/webconfigtransform/blob/master/README.md
then you need to transform your publish profile as well.
web.$(publish-profile-name).config is going to be your web.config name. You need to change environment variables of the transformed web.configs respectively.

How do you add a mime type when using ASP.NET vNext?

There's LOADS of information on how to add MIME types into a normal project.
These include configuring IIS, or modifying the web.config.
Both of these options are unavailable to me in vNext with IIS Express.
I had a look at the schema to the project.json file and couldn't find anything in there that would help either.
Can this be done yet? - I want to add a mime type for the .woff2 extension.
If you hosting it on IIS 7 or later then following step will do what you need. This answer I have used Visual Studio 2015 CTP5.
Publish your web application ( ASP.net vnext)
You can publish it to location like C:\MyPublish
Once it get successfully published you will find following location C:\MyPublish\wwwroot. Here You will find web.config.
Now host your site to in IIS ( Make sure that you have used C:\MyPublish\wwwroot as your path)
Now edit web.config over here just like you did for old version to add mime type. ( Following is my edit)
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="kpm-package-path" value="..\approot\packages" />
<add key="bootstrapper-version" value="1.0.0-beta2" />
<add key="kre-package-path" value="..\approot\packages" />
<add key="kre-version" value="1.0.0-beta2" />
<add key="kre-clr" value="CLR" />
<add key="kre-app-base" value="..\approot\src\WebApplication5" />
</appSettings>
<system.webServer>
<staticContent>
<remove fileExtension=".woff" />
<mimeMap fileExtension=".woff" mimeType="application/font-woff" />
</staticContent>
</system.webServer>
</configuration>
Note: As per my thinking In old version it is fix that it is always windows environment so we have direct web.config file in project and we edit that but now we have to follow different process to register as in future we can host completly on linux env as well.
Update : There is another way to do that as well. If you are using Microsoft.AspNet.StaticFiles package then you will have extension.
public void Configure(IApplicationBuilder app)
{
app.UseStaticFiles();
}
This will indirectly use https://github.com/aspnet/StaticFiles/blob/dev/src/Microsoft.AspNet.StaticFiles/FileExtensionContentTypeProvider.cs. Here you can see all mapping.
Update 2: (Add New Mime Type)
public void Configure(IApplicationBuilder app)
{
StaticFileOptions option = new StaticFileOptions();
FileExtensionContentTypeProvider contentTypeProvider = (FileExtensionContentTypeProvider)option.ContentTypeProvider;
contentTypeProvider.Mappings.Add("<<yourextention>>","<<mimetype>>");
app.UseStaticFiles(option);
}
Until this is released, you can also edit applicationhost.config which I found in D:\Documents\IISExpress\config (yours might be on your C drive [Documents]).
I added:
<mimeMap fileExtension=".woff2" mimeType="font/x-woff2" />
Inside <staticContent>.

VSTO Debug version fine, Installed version doesn't read app.config

I have a VSTO Excel 2007 add-in that should read connectionstrings from the app.config file, and then let the user decide which database to connect to. This works fine when I debug it, but when I run the deployed version (done with Windows Installer) the connectionstrings aren't read at all. I have added the primary outputs from all the projects to the setup project.
The app.config file is in the ExcelAddIn project, but not under the Excel heading. The class that manages the connectionstrings is in another project.
Here is my app.config file:
<?xml version="1.0"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
</configSections>
<connectionStrings>
<clear/>
<add name="MyEntities" connectionString="metadata=res://*/MyModel.csdl|res://*/MyModel.ssdl|res://*/SymModel.msl;provider=System.Data.SqlClient;provider connection string="data source=myServer;initial catalog=myDB;persist security info=True;user id=myUser;password=myPassword;multipleactiveresultsets=True;App=EntityFramework"" providerName="System.Data.EntityClient"/>
</connectionStrings>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0"/>
</parameters>
</defaultConnectionFactory>
</entityFramework>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>
I use the following to get to the connectionstrings:
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
ConnectionStringsSection csSection = config.ConnectionStrings;
I have tried to add the ExcelAddin.dll.config file to the setup project's folder in which the Release folder and .proj file are. I have set the app.config file's 'Copy to Output Directory' property to 'Copy always' and the Build Action property to 'Content'.
Is there something worng with my app.config file, or why is it not picked up (the connectionstrings are not loaded into csSection) after I've run the installer?
Add file:/// to [TARGETDIR]ExcelAddIn.vsto|vstolocal (ex: file:///[TARGETDIR]ExcelAddIn.vsto|vstolocal) at the registry entries under "installer".
You need to add app.Config File to your Setup Project but not from the actual project do it from the Release\Debug folder of the ExcelAddIn project.
When you build you ExcelAddIn project it will leave App.config File to the Release\Debug Folder, piCk the file form there and include it into the dependicies folder of the Setup Project.
It seems that it is working with ClickOnce. So I still don't know what the problem was with the setup project and using Windows Installer, but at least I can deploy it.

Server specific Web.config replacement

I am currently using Visual Studio 2010. I want to set up a different connection string for my "Testing" Configuration. I have tried doing it by using config transformations, but it does not seem to work.
In my Web.config my connection string is the following:
<add name="MyDb" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=DevelopmentDb;Integrated Security=SSPI; MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />
I found out that you can replace the connection string defined in Web.config by adding config transformation. I right clicked on Web.config and clicked on "Add config transformations". This created a Web.TEST.config file. I then added a connection string replacement, but it doesnt seem to work. It still uses the connection string defined in Web.config.
The file contents of Web.TEST.config is:
<?xml version="1.0"?>
<!-- For more information on using web.config transformation visit http://go.microsoft.com/fwlink/?LinkId=125889 -->
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<!--
In the example below, the "SetAttributes" transform will change the value of
"connectionString" to use "ReleaseSQLServer" only when the "Match" locator
finds an atrribute "name" that has a value of "MyDB".
-->
<connectionStrings>
<add name="MyDb"
connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=TestDb;Integrated Security=SSPI; MultipleActiveResultSets=True"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>
<system.web>
<!--
In the example below, the "Replace" transform will replace the entire
<customErrors> section of your web.config file.
Note that because there is only one customErrors section under the
<system.web> node, there is no need to use the "xdt:Locator" attribute.
<customErrors defaultRedirect="GenericError.htm"
mode="RemoteOnly" xdt:Transform="Replace">
<error statusCode="500" redirect="InternalError.htm"/>
</customErrors>
-->
</system.web>
</configuration>
Any ideas to what I could be missing?
Transformations get applied when you deploy the project, not when you compile it.
If you are using IIS, you can deploy to the configured web application directory, which will cause the transform to apply.

Resources