Runtime: Assembly.CodeBase does not produce a correctly escaped URI

Created on 10 Jun 2020  路  7Comments  路  Source: dotnet/runtime

Hi,

We have a customer case where their Application path contains '#'.
For example path is:
C:/Users/asoni/source/repos/Demo#App/DemoApp
and we get path for IBM.Data.DB2.Core.dll using below code:
typeof(DB2Connection).Assembly.CodeBase

Output is:

file:///C:/Users/asoni/source/repos/Demo#App/DemoApp/bin/Debug/netcoreapp3.1/IBM.Data.DB2.Core.dll

Now we use Uri class to get physical path:
string assemLoc = (new Uri(typeof(DB2Connection).Assembly.CodeBase)).LocalPath;

Output is:

C:\Users\asoni\source\repos\Demo

Expected output:

C:/Users/asoni/source/repos/Demo#App/DemoApp/bin/Debug/netcoreapp3.1/IBM.Data.DB2.Core.dll

If we don't have # in Application path it works perfectly fine..

Could you share what is the reason behind this behavior and How come we can overcome this issue if we want to allow # in path ?

area-System.Reflection bug

All 7 comments

I couldn't figure out the best area label to add to this issue. Please help me learn by adding exactly one area label.

Doesn't appear to be an issue with Uri itself. Can you share a minimal repro?

I tried:
```c#
var u = new Uri("C:/Users/asoni/source/repos/Demo#App/DemoApp/bin/Debug/netcoreapp3.1/IBM.Data.DB2.Core.dll");
Console.WriteLine(u);
Console.WriteLine(u.LocalPath);


Which outputs:

file:///C:/Users/asoni/source/repos/Demo%23App/DemoApp/bin/Debug/netcoreapp3.1/IBM.Data.DB2.Core.dll
C:\Users\asoni\source\repos\Demo#App\DemoApp\bin\Debug\netcoreapp3.1\IBM.Data.DB2.Core.dll
```

Tagging subscribers to this area: @dotnet/ncl
Notify danmosemsft if you want to be subscribed.

@marcpopMSFT did you mean to self-assign this one?

@scalablecory it was assigned to me in the sdk repo for triage and I forgot to unassign when transferring over

@scalablecory here is repro:

var u = new Uri("file:///C:/Users/asoni/source/repos/Demo#App/DemoApp/bin/Debug/netcoreapp3.1/IBM.Data.DB2.Core.dll");
            Console.WriteLine(u);
            Console.WriteLine(u.LocalPath);

Output:

file:///C:/Users/asoni/source/repos/Demo#App/DemoApp/bin/Debug/netcoreapp3.1/IBM.Data.DB2.Core.dll
C:\Users\asoni\source\repos\Demo

Uri will parse the # in file:///C:/Users/foo#App/bar.dll as a fragment.
If # is meant as a character belonging to the file path, it should be escaped: file:///C:/Users/foo%23App/bar.dll.

Uri will correctly escape the # when you retrieve the absolute Uri:
new Uri("C:/Users/foo#App/bar.dll").AbsoluteUri => file:///C:/Users/foo%23App/bar.dll

So I would argue that the problem here is not with Uri, but Assembly.CodeBase.
Seeing as https://github.com/dotnet/runtime/issues/31127#issuecomment-545084027 is approved, making CodeBase both obsolete and [EditorBrowsable(Never)], the customer should switch to using Assembly.Location.

Assembly.Location outputs the file path without the explicit file scheme, so the # is not treated as a fragment if used in Uri.
new Uri(typeof(Program).Assembly.Location).LocalPath

Was this page helpful?
0 / 5 - 0 ratings

Related issues

matty-hall picture matty-hall  路  3Comments

iCodeWebApps picture iCodeWebApps  路  3Comments

bencz picture bencz  路  3Comments

sahithreddyk picture sahithreddyk  路  3Comments

GitAntoinee picture GitAntoinee  路  3Comments