Skip to content

Commit c42d6a5

Browse files
authored
Fix blazor detection (CopyText#203)
1 parent e9a4dcf commit c42d6a5

File tree

3 files changed

+13
-7
lines changed

3 files changed

+13
-7
lines changed

src/BlazorSample/Program.cs

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public static Task Main()
1818
#endregion
1919
builder.RootComponents.Add<App>("app");
2020
#endregion
21+
2122
serviceCollection.AddTransient(
2223
provider => new HttpClient
2324
{

src/TextCopy/ClipboardService.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public static partial class ClipboardService
3535
static ClipboardService()
3636
{
3737
#if NETSTANDARD2_1
38-
if (RuntimeInformation.IsOSPlatform(OSPlatform.Create("WEBASSEMBLY")))
38+
if (RuntimeInformation.OSDescription == "web")
3939
{
4040
throw new Exception($"The static class ClipboardService is not supported on Blazor. Instead inject an {nameof(IClipboard)} using {nameof(ServiceExtensions)}{nameof(ServiceExtensions.InjectClipboard)}.");
4141
}

src/TextCopy/ServiceExtensions.cs

+11-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#if !UAP
2+
using System;
3+
using System.Runtime.InteropServices;
24
using Microsoft.Extensions.DependencyInjection;
35

46
namespace TextCopy
@@ -29,17 +31,20 @@ public static void InjectClipboard(this IServiceCollection services)
2931
{
3032

3133
#if NETSTANDARD2_1
32-
33-
var jsRuntimeType = System.Type.GetType("Microsoft.JSInterop.IJSRuntime, Microsoft.JSInterop", false);
34-
if (jsRuntimeType != null)
34+
if (RuntimeInformation.OSDescription == "web")
3535
{
36+
var jsRuntimeType = System.Type.GetType("Microsoft.JSInterop.IJSRuntime, Microsoft.JSInterop", false);
37+
if (jsRuntimeType == null)
38+
{
39+
throw new Exception("Running in Blazor but could not resolve JSInterop type.");
40+
}
3641
var jsRuntime = provider.GetService(jsRuntimeType);
37-
if (jsRuntime != null)
42+
if (jsRuntime == null)
3843
{
39-
return new BlazorClipboard(jsRuntime);
44+
throw new Exception("Running in Blazor but could not get the JSInterop instance from the IServiceProvider.");
4045
}
46+
return new BlazorClipboard(jsRuntime);
4147
}
42-
4348
#endif
4449

4550
return new Clipboard();

0 commit comments

Comments
 (0)