B
B
Boris the Animal2021-05-31 09:44:18
PowerShell
Boris the Animal, 2021-05-31 09:44:18

ASP.NET Core 3.1 + IIS + Powershell SDK does not work with Powershell commands if the web application is launched from IIS. How to fix?

Please do not touch the tags that I put. I don't know in which branch there is someone who had this problem and all the tags are correctly set. The question touches on everything that is specified in the tags. For example, suddenly in IIS you need to configure something or in ASP.NET Core, something related to IIS.

When I launch the application from under the debug or just double-click on the action, then PowerShell works fine, commands are executed in it, etc. After I publish the application, PowerShell seems to freeze. I send a request and there is no response. Postman takes a very long time to respond. How to find the cause of the problem, if there is not even an error? Perhaps there are useful logs somewhere? Someone faced similar?

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <UserSecretsId>***</UserSecretsId>
    <AspNetCoreHostingModel>OutOfProcess</AspNetCoreHostingModel>
    <LangVersion>9</LangVersion>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Extensions.Hosting.AsyncInitialization" Version="1.0.0" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.1.15" />
    <PackageReference Include="Microsoft.Data.Sqlite" Version="5.0.6" />
    <PackageReference Include="Microsoft.Management.Infrastructure" Version="2.0.0" />
    <PackageReference Include="Microsoft.PowerShell.SDK" Version="7.0.6" />
    <PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
    <PackageReference Include="Serilog.AspNetCore" Version="3.4.0" />
  </ItemGroup>

</Project>


public class Startup
    {
        public IConfiguration Configuration { get; }

        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public void ConfigureServices(IServiceCollection services)
        {
            // ... 
            // Много всего, что не относится к вопросу
            // ... 

            // OutOfProcess
            services.Configure<IISOptions>(options =>
            {
                options.ForwardClientCertificate = false;
            });
        }

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            // ... 
            // Много всего, что не относится к вопросу
            // ... 
        }
    }


Properties/PublishProfiles/ FolderProfile.pubxml
RuntimeIdentifier win7-x64 is being used because there was a problem
.
<?xml version="1.0" encoding="utf-8"?>
<!--
https://go.microsoft.com/fwlink/?LinkID=208121. 
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <DeleteExistingFiles>False</DeleteExistingFiles>
    <ExcludeApp_Data>False</ExcludeApp_Data>
    <LaunchSiteAfterPublish>True</LaunchSiteAfterPublish>
    <LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
    <LastUsedPlatform>x64</LastUsedPlatform>
    <PublishProvider>FileSystem</PublishProvider>
    <PublishUrl>bin\Release\netcoreapp3.1\publish\</PublishUrl>
    <WebPublishMethod>FileSystem</WebPublishMethod>
    <SiteUrlToLaunchAfterPublish />
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <RuntimeIdentifier>win7-x64</RuntimeIdentifier>
    <PublishSingleFile>False</PublishSingleFile>
    <ProjectGuid>***</ProjectGuid>
    <SelfContained>false</SelfContained>
    <PublishReadyToRun>False</PublishReadyToRun>
  </PropertyGroup>
</Project>


try
{
    using var powerShell = PowerShell.Create();
    // Логировал и знаю, что зависает после верхней строки.
    await powerShell
        .AddCommand("Start-Process")
        .AddArgument($"someclient://run-service/session={session}")
        .InvokeAsync();
}
catch (Exception ex)
{
    _logger.Error(SOME_TEMPLATE, ex);
    throw;
}


I created a separate application and it already gives an error:
2021-05-31 14:00:18.774 +03:00 [DBG] OnPowerShell_InvocationStateChanged. State: Running, Reason (Exception): -
2021-05-31 14:00:19.967 +03:00 [DBG] OnPowerShell_InvocationStateChanged. State: Failed, Reason (Exception): System.Management.Automation.CmdletInvocationException: This command cannot be run due to the error: Access denied.
---> System.InvalidOperationException: This command cannot be run due to the error: Permission denied..
at System.Management.Automation.MshCommandRuntime.ThrowTerminatingError(ErrorRecord errorRecord)
--- End of inner exception stack trace ---
at System.Management.Automation.Internal.PipelineProcessor.SynchronousExecuteEnumerate(Object input)
at System.Management.Automation.Runspaces.LocalPipeline.InvokeHelper()
at System.Management.Automation.Runspaces.LocalPipeline.InvokeThreadProc()

Answer the question

In order to leave comments, you need to log in

2 answer(s)
B
BasiC2k, 2021-05-31
@BasiC2k

IIS is isolated from the operating system and cannot be used with user rights. Everything works in the development environment, that's a fact. But when deploying an application on IIS, not everything works.

C
cicatrix, 2021-05-31
@cicatrix

I think your case:
https://stackoverflow.com/questions/43283339/runni...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question