microsoft / dotnet

This repo is the official home of .NET on GitHub. It's a great starting point to find many .NET OSS projects from Microsoft and the community, including many that are part of the .NET Foundation.
https://devblogs.microsoft.com/dotnet/
MIT License
14.34k stars 2.21k forks source link

Process.GetCurrentProcess().ProcessName is throwing InvalidOperationException for Blazor Webassembly project #1319

Closed sonupmandal closed 3 years ago

sonupmandal commented 3 years ago

I have created a simple Blazor Webassembly application in which Target Framework : .NET Standard 2.1

Contents of Counter.razor file

@page "/counter"

<h1>Counter</h1>

<p>Current count: @currentCount</p>

<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>

@code {
    private int currentCount = 0;

    private void IncrementCount()
    {
        var CurrentProcess = System.Diagnostics.Process.GetCurrentProcess();
        var pname = CurrentProcess.ProcessName;

        currentCount++;
    }
}

Upon running this application, a browser window opens, when i press click me button, i end up getting this exception. _crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100] Unhandled exception rendering component: Process has exited or is inaccessible, so the requested information is not available. System.InvalidOperationException: Process has exited or is inaccessible, so the requested information is not available. at System.Diagnostics.Process.getProcessName () <0x2d34700 + 0x0004a> in :0 at WebApplication1.Pages.Counter.IncrementCount () <0x2d33d80 + 0x0000c> in :0 at Microsoft.AspNetCore.Components.EventCallbackWorkItem.InvokeAsync[T] (System.MulticastDelegate delegate, T arg) <0x2d07e80 + 0x0005e> in :0 at Microsoft.AspNetCore.Components.EventCallbackWorkItem.InvokeAsync (System.Object arg) <0x2d07bc0 + 0x0000a> in :0 at Microsoft.AspNetCore.Components.ComponentBase.Microsoft.AspNetCore.Components.IHandleEvent.HandleEventAsync (Microsoft.AspNetCore.Components.EventCallbackWorkItem callback, System.Object arg) <0x2d07b28 + 0x0000a> in :0 at Microsoft.AspNetCore.Components.EventCallback.InvokeAsync (System.Object arg) <0x2d076f0 + 0x00040> in :0 at Microsoft.AspNetCore.Components.RenderTree.Renderer.DispatchEventAsync (System.UInt64 eventHandlerId, Microsoft.AspNetCore.Components.RenderTree.EventFieldInfo fieldInfo, System.EventArgs eventArgs) <0x2d06ce0 + 0x000a8> in :0

Can someone assist me why i am getting this error ? Is Process.GetCurrentProcess().ProcessName API not supported with Blazor webassembly app ?

mairaw commented 3 years ago

Tagging @danroth27

You'd get better support if you filed this issue on the appropriate repo: https://github.com/dotnet/aspnetcore

danroth27 commented 3 years ago

@sonupmandal Both Process.GetCurrentProcess() and Process.ProcessName are not supported when running on WebAssembly in a browser. Browsers run your code in a security sandbox, so not all .NET APIs are available. In .NET 5 you can use the Platform Compatibility Analyzer to get design time insights into which APIs are supported when running in a browser.

@terrajobst

sonupmandal commented 3 years ago

Thanks @mairaw Posted this issue here : https://github.com/dotnet/aspnetcore/issues/34828