26
26
using System ;
27
27
using System . Collections . Generic ;
28
28
using System . IO ;
29
+ using System . Reflection ;
29
30
using System . Runtime . InteropServices ;
30
31
31
32
namespace Microsoft . Playwright . Helpers ;
@@ -48,19 +49,19 @@ internal static (string ExecutablePath, Func<string, string> GetArgs) GetExecuta
48
49
}
49
50
if ( assemblyDirectory ? . Exists != true || ! File . Exists ( Path . Combine ( assemblyDirectory . FullName , "Microsoft.Playwright.dll" ) ) )
50
51
{
51
- string assemblyLocation ;
52
52
var assembly = typeof ( Playwright ) . Assembly ;
53
- #pragma warning disable SYSLIB0012 // 'Assembly.CodeBase' is obsolete: 'Assembly.CodeBase and Assembly.EscapedCodeBase are only included for .NET Framework compatibility.
54
- if ( Uri . TryCreate ( assembly . CodeBase , UriKind . Absolute , out var codeBase ) && codeBase . IsFile )
55
- #pragma warning restore SYSLIB0012 // 'Assembly.CodeBase' is obsolete: 'Assembly.CodeBase and Assembly.EscapedCodeBase are only included for .NET Framework compatibility.
53
+ if ( TryGetCodeBase ( assembly , out var codeBase ) && codeBase . IsFile )
56
54
{
57
- assemblyLocation = codeBase . LocalPath ;
55
+ assemblyDirectory = new FileInfo ( codeBase . LocalPath ) . Directory ;
56
+ }
57
+ else if ( ! string . IsNullOrEmpty ( assembly . Location ) )
58
+ {
59
+ assemblyDirectory = new FileInfo ( assembly . Location ) . Directory ;
58
60
}
59
61
else
60
62
{
61
- assemblyLocation = assembly . Location ;
63
+ assemblyDirectory = new FileInfo ( AppContext . BaseDirectory ) . Directory ;
62
64
}
63
- assemblyDirectory = new FileInfo ( assemblyLocation ) . Directory ;
64
65
}
65
66
66
67
string executableFile ;
@@ -95,6 +96,22 @@ internal static (string ExecutablePath, Func<string, string> GetArgs) GetExecuta
95
96
throw new PlaywrightException ( $ "Driver not found: { executableFile } ") ;
96
97
}
97
98
99
+ private static bool TryGetCodeBase ( Assembly assembly , out Uri codeBase )
100
+ {
101
+ try
102
+ {
103
+ // assembly.CodeBase might throw with:
104
+ // System.NotSupportedException: CodeBase is not supported on assemblies loaded from a single-file bundle.
105
+ Uri . TryCreate ( assembly . CodeBase , UriKind . Absolute , out codeBase ) ;
106
+ return true ;
107
+ }
108
+ catch ( NotSupportedException )
109
+ {
110
+ codeBase = null ;
111
+ return false ;
112
+ }
113
+ }
114
+
98
115
private static ( string ExecutablePath , Func < string , string > GetArgs ) GetPath ( string driversPath )
99
116
{
100
117
string platformId ;
0 commit comments