I am new to IIS configuration. I'm trying to host my .NET 7 API on IIS but i have an error that i cant solve.
When i try to run my application with IIS, i get this error: "HTTP Error 500.30 - ASP.NET Core app failed to start".
The Event Viewer says:
System.IO.FileNotFoundException: Could not load file or assembly 'w3wp, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.
File name: 'w3wp, Culture=neutral, PublicKeyToken=null'
at System.Reflection.RuntimeAssembly.InternalLoad(AssemblyName assemblyName, StackCrawlMark& stackMark, AssemblyLoadContext assemblyLoadContext, RuntimeAssembly requestingAssembly, Boolean throwOnFileNotFound)
at System.Reflection.Assembly.Load(AssemblyName assemblyRef)
at Microsoft.AspNetCore.Mvc.ApplicationParts.ApplicationPartManager.GetApplicationPartAssemblies(String entryAssemblyName)
at Microsoft.AspNetCore.Mvc.ApplicationParts.ApplicationPartManager.PopulateDefaultParts(String entryAssemblyName)
at Microsoft.Extensions.DependencyInjection.MvcCoreServiceCollectionExtensions.GetApplicationPartManager(IServiceCollection services, IWebHostEnvironment environment)
at Microsoft.Extensions.DependencyInjection.MvcCoreServiceCollectionExtensions.AddMvcCore(IServiceCollection services)
at Microsoft.Extensions.DependencyInjection.MvcServiceCollectionExtensions.AddControllersCore(IServiceCollection services)
at Microsoft.Extensions.DependencyInjection.MvcServiceCollectionExtensions.AddControllers(IServiceCollection services)
Program.cs -> Line 57 -> "builder.Services.AddControllers();" throw the exception. When i start application, i see w3wp.exe running up. But still i get the error.
What i did so far:
Installed IIS on my Windows Server 2019.
Installed .Net 7 Hosting Bundle.
I publish my app with this settings:
Configuration: Release
Target Framework: net7.0
Deployment Mode: Framework-dependent
Target Runtime: Portable
Copied the files to this location: C:\inetpub\wwwroot\api.mycompany.com
Create my application pool with "No Managed Code". and "Integrated" pipeline mode.
Create the site and run it.
My web.config file:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<location path="." inheritInChildApplications="false">
<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2"
resourceType="Unspecified" />
</handlers>
<aspNetCore processPath="dotnet" arguments=".\WebApi.dll" stdoutLogEnabled="false"
stdoutLogFile=".\logs\stdout" hostingModel="inprocess" />
</system.webServer>
</location>
</configuration>
With this settings, my application give me the error i mentioned above. However, when i double-click my exe and run it outside of IIS, my app runs perfectly and i can run all get-post request with no error.
What i try to solve the problem:
Changed .NET CLR Version to v4.0, failed.
Set Enable 32-Bit Applications to true, failed,
Changed Managed Pipeline Mode to Classic, failed.
Publish my app Self-contained both win-x86 and win-x64, failed.
Based on other answers
Changed Identity to Local service, failed.
Changed Identity to NetworkService, failed.
Changed Identity to custom account and logged in as admin user, failed.
Set Load User Profile setting to true, failed.
Grant IIS_IUSRS and Network Service full access to the directory "C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files", failed.
Grant IIS_IUSRS, Network Service full access to project directory, failed.
Remove .pdb files on project directory, failed.
Start Visual Studio as an administrator and publish my app again, failed.
No matter what i tried it didn't work. What am i doing wrong?
I found the solution. Like @Lex Li said, there is incorrect code on my Program.cs:
var options = new WebApplicationOptions
{
ApplicationName = Process.GetCurrentProcess().ProcessName,
ContentRootPath = AppContext.BaseDirectory,
Args = args
};
var builder = WebApplication.CreateBuilder(args);
Looks like "Process.GetCurrentProcess()" method goes to w3wp. I changed to original version:
var builder = WebApplication.CreateBuilder(args);
After that, my application runs perfectly on IIS.