.NET Core 3.0: ASPNETCORE_ENVIRONMENT has value Development on publish - visual-studio

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?

Related

Disable DOTNET_STARTUP_HOOKS in ASP.NET Core 5 web app

When deploying my ASP.NET Core 5 app (which targets .NET 5) to an Azure App Service, it throws a 502 error with the following details logged:
<Event>
<System>
<Provider Name=".NET Runtime"/>
<EventID>1026</EventID>
<Level>1</Level>
<Task>0</Task>
<Keywords>Keywords</Keywords>
<TimeCreated SystemTime="2021-07-12T08:58:41Z"/>
<EventRecordID>-2033762671</EventRecordID>
<Channel>Application</Channel>
<Computer>RD0003FF7ABC48</Computer>
<Security/>
</System>
<EventData>
<Data>Application: dotnet.exe
CoreCLR Version: 5.0.721.25508
.NET Version: 5.0.7
Description: The process was terminated due to an unhandled exception.
Exception Info: System.ArgumentException: Startup hook assembly 'C:\Program Files\dotnet\SDK\5.0.301\DotnetTools\dotnet-watch\5.0.301-servicing.21271.7\tools\net5.0\any\middleware\Microsoft.AspNetCore.Watch.BrowserRefresh.dll' failed to load. See inner exception for details.
---> System.IO.FileNotFoundException: Could not load file or assembly 'C:\Program Files\dotnet\SDK\5.0.301\DotnetTools\dotnet-watch\5.0.301-servicing.21271.7\tools\net5.0\any\middleware\Microsoft.AspNetCore.Watch.BrowserRefresh.dll'. The system cannot find the path specified.
File name: 'C:\Program Files\dotnet\SDK\5.0.301\DotnetTools\dotnet-watch\5.0.301-servicing.21271.7\tools\net5.0\any\middleware\Microsoft.AspNetCore.Watch.BrowserRefresh.dll'
at System.Runtime.Loader.AssemblyLoadContext.LoadFromPath(IntPtr ptrNativeAssemblyLoadContext, String ilPath, String niPath, ObjectHandleOnStack retAssembly)
at System.Runtime.Loader.AssemblyLoadContext.LoadFromAssemblyPath(String assemblyPath)
at System.StartupHookProvider.CallStartupHook(StartupHookNameOrPath startupHook)
--- End of inner exception stack trace ---
at System.StartupHookProvider.CallStartupHook(StartupHookNameOrPath startupHook)
at System.StartupHookProvider.ProcessStartupHooks()
</Data>
</EventData>
</Event>
The path that is not found corresponds to the environment variable that is being set in the web.config, which is set automatically by VisualStudio when I build the app:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<!--
Configure your application settings in appsettings.json. Learn more at http://go.microsoft.com/fwlink/?LinkId=786380
-->
<!--App settings needed to the Cloud media service library-->
<system.web>
<customErrors mode="off" />
</system.web>
<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
</handlers>
<aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false" startupTimeLimit="3600" requestTimeout="23:00:00">
<environmentVariables>
<environmentVariable name="COMPLUS_ForceENC" value="1" />
<environmentVariable name="ASPNETCORE_HTTPS_PORT" value="8088" />
<environmentVariable name="ASPNETCORE_AUTO_RELOAD_WS_ENDPOINT" value="ws://localhost:53593/MyWebApp/" />
<environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" />
<environmentVariable name="ASPNETCORE_HOSTINGSTARTUPASSEMBLIES" value="Microsoft.AspNetCore.Watch.BrowserRefresh" />
<environmentVariable name="DOTNET_STARTUP_HOOKS" value="**C:\Program Files\dotnet\SDK\5.0.301\DotnetTools\dotnet-watch\5.0.301-servicing.21271.7\tools\net5.0\any\middleware\Microsoft.AspNetCore.Watch.BrowserRefresh.dll**" />
</environmentVariables>
</aspNetCore>
</system.webServer>
</configuration>
What is this environment variable used for? Can I disable its generation to allow the app run properly on the Azure App Service when deploying?
Thanks in advance.
Probably not the most fancy solution, but to manually delete the DLL referenced in the DOTNET_STARTUP_HOOKS environment variable in the web.config prevents VS to automatically set it over and over again.
Startup hooks are disabled by default on trimmed apps. Not sure if you're in the position to do app trimming, but if you could, that'd effectively disable startup hooks.
The main reason for this is explained here, and comes down to it being potentially dangerous to be run on trimmed apps.
Another, scarier way, would be to write yourself a global startup hook that disables local startup hooks to load. Of course, the simpler solution is to just make sure that that environment variable is not set in the first place.

Autogenerated web.config not accepted by IIS 10

I want to publish a entity framework core (.Net) to a IIS.
For this I publish the application in Visual Studio Community 2017. When I publish the application the web.config file will be auto generated as the following
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<location path="." inheritInChildApplications="false">
<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
</handlers>
<aspNetCore processPath="dotnet" arguments=".\basket.api.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" />
</system.webServer>
</location>
</configuration>
<!--ProjectGuid: ba174064-1719-4e47-a0c3-5e8cff312047-->
I go to my IIS 10
I post it into the default website. Into to the location "C:\inetpub\wwwroot".
I start the web site an access via the webbrowser localhost.
I get allways the following error message:
I check the web page of Microsoft of the error code 0x8007000d,it is basicly saying that the format of a XML Element is not correct. Which is impossible I check all the formating and also I let autogenerate by the Visual Studio when i publish the site. I tried everything and don't know what to do. I am very newbie in IIS stuff.

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.

Can't commit several files from Visual Studio project to subversion

Since a few weeks, I have some problems with Subversion. When I try to commit files from a Visual Studio 2017 project there are some files which I can't commit to my Visual SVN Server. To be precise all files in the project folder like *.cs, *.config, *.csproj, *.resx, ...
My setup:
Client: TortoiseSVN 1.9.7 on Windows10
Server: VisualSVN behind a IIS-ReverseProxy running on Windows Server 2012r2
The error I get when I try to commit for example a *.cs file:
Commit
D:\Test\branches\ScaraControl\ScaraControl\Form1.cs
D:\Test\branches\ScaraControl\ScaraControl\Form1.cs
Commit failed (details follow):
File 'D:\Test\branches\ScaraControl\ScaraControl\Form1.cs' is out of date
'/svn/Test/!svn/txr/5-9/branches/ScaraControl/ScaraControl/Form1.cs' path not found
You have to update your working copy first.
Updating the working copy is finishing successfully but doesn't fix the problem.
You can see my project in the picture below. For testing, I created a completely new and empty repository. As you can see the .vs, bin and obj folders are ignored with all the files inside of them, all other folders are committed to the server (without the files inside of them). In the second picture you can see that I can commit the *.sln file but no other file in the project folder.
For testing, I created an empty text file and renamed it to text.cs. Even this empty file cannot be committed to the Server with the same error message.
Due to the fact that this is happening to all Clients, it is more likely to be a problem on the Server side I guess but I have no idea what could cause this error. Unfortunately, the VisualSVN Server has no error logging or at least not the free version I'm using.
I would be very grateful for any tip I can get to solve this annoying problem.
Edit1: Problem is caused by the IIS Reverse-Proxy
After connecting via port 8443 directly to the VisualSVN server (bypassing the reverse proxy) everything is working again. So there must be a problem with the configuration of the URL Rewrite module. To be honest it took me quiet a long time to get it working somehow because my knowledge about all the settings is very limited.
This my Web.config with the settings for the URL Rewrite module. Maybe there is something not configured as it should be. If you need further information just ask.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<outboundRules>
<rule name="ReverseProxyOutboundRule1" preCondition="ResponseIsHtml1" enabled="true">
<match filterByTags="A, Form, Img" pattern="^http(s)?://svn.example.org:8443/(.*)" />
<action type="Rewrite" value="http{R:1}://svn.example.org/{R:2}" />
</rule>
<preConditions>
<preCondition name="ResponseIsHtml1">
<add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
</preCondition>
</preConditions>
</outboundRules>
<rules>
<rule name="ReverseProxyInboundRule1" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{CACHE_URL}" pattern="^(https?)://" />
</conditions>
<action type="Rewrite" url="{C:1}://svn.example.org:8443/{R:1}" />
</rule>
</rules>
</rewrite>
<security>
<authorization>
<remove users="*" roles="" verbs="" />
<add accessType="Allow" users="" roles="Users" />
<add accessType="Allow" users="*" />
<add accessType="Allow" users="?" />
</authorization>
</security>
<urlCompression doStaticCompression="false" doDynamicCompression="false" />
<httpRedirect enabled="false" destination="https://svn.example.org" exactDestination="true" childOnly="true" />
<directoryBrowse enabled="false" />
</system.webServer>
</configuration>
I had the same issue.
The fails because IIS prohibits URL suffixes like .cs and .config
You can work around this by adding this to the web.config of the IIS proxy
<system.webServer>
<security>
<requestFiltering>
<fileExtensions allowUnlisted="true" applyToWebDAV="true">
<clear />
</fileExtensions>
<verbs allowUnlisted="true" applyToWebDAV="true" />
<hiddenSegments applyToWebDAV="true">
<clear />
</hiddenSegments>
</requestFiltering>
</security>
</system.webServer>
Credits to this post IIS7 and ARR as reverse proxy for Subversion
I came across the same problem and am running a reverse proxy through IIS, so believe that has something to do with it.
VisualSVN is served up locally on https://localhost:8443 and I was attempting to use the reverse proxy to route from https://svn.mysite.com. This appears to work fine. You can even checkout a fresh copy of the repo and all files are downloaded. It's when you try and commit that you have problems - as you've identified, certain files fail to be found on the repo.
The only work around I have found (thanks to your question narrowing down the likely causes) was to add the port to the URL: https://svn.mysite.com:8443. This shouldn't be necessary as the reverse proxy should handle, so I'm guessing it's an issue with VisualSVN which may be fixed in a future update.

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>.

Resources