Decompile ASP.NET Core 3.x sources in Rider - c#

I use Rider 2019.2.3 on the Ubuntu 18.10 with installed .NET SDK of version 3.1.100.
When I try to navigate to decompiled sources I sometimes see only declaration of the methods without their implementations, for example:
public static class IdentityServiceCollectionExtensions
{
public static IdentityBuilder AddIdentity<TUser, TRole>(
this IServiceCollection services)
where TUser : class
where TRole : class;
...
}
Rider took this source from the assembly /usr/share/dotnet/packs/Microsoft.AspNetCore.App.Ref/3.1.0/ref/netcoreapp3.1/Microsoft.AspNetCore.Identity.dll. I inspect the IL code of it and it looks like this assembly contains only stubs for methods without their implementations.
So I have a couple of questions:
What is the location of the actual assemblies of ASP.NET Core libraries on my computer?
How can I force Rider to decompile actual assemblies?
My .csproj has Microsoft.NET.Sdk.Web SDK specified, so I thought that maybe I should install some special SDK for web development, but I can't find any information about this.

Navigation into decompiled method body in .NET Core 3.0 fixed in JetBrains Rider 2019.3. Please update your version to the latest available release.

The base path for the implementation assemblies is:
C:\Program Files\dotnet\shared\
I created a simple ASP.NET Core App and added your IdentityBuilder AddIdentity<TUser, TRole> function. When decompiled it couldn't get resolved automatically as well.
The actual assembly path for your case is:
C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App\3.1.0\Microsoft.AspNetCore.Identity.dll
On Ubuntu it should be:
/usr/share/dotnet/shared/Microsoft.AspNetCore.App/3.1.0/Microsoft.AspNetCore.Identity.dll
Once i added this file manually to the decompiler i could step through the source code
If the path doesn't exist you might have to install the AspNetCore SDK for Ubuntu.

Related

Could not load file or assembly System.Fabric with Azure Functions

Are there any restrictions with packages you can use with Azure Functions. I have researched as much as I can and it doesn't seem so, however when I try creating an Azure Function that references the package "Microsoft.ServiceFabric" I get the following error:
System.Private.CoreLib: Exception while executing function:
ScaleDownServiceFabrics. FunctionApp2: Could not load file or assembly
'System.Fabric, Version=6.0.0.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35'. Could not find or load a specific
file. (Exception from HRESULT: 0x80131621). System.Private.CoreLib:
Could not load file or assembly 'System.Fabric, Version=6.0.0.0,
Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
I have tried both Azure Func and.1 and 2, and .Net Framework and .Net Core with no luck.
using System;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Extensions.Logging;
using System.Fabric;
namespace FunctionApp5
{
public static class Function1
{
[FunctionName("Function1")]
public static void Run([TimerTrigger("*/5 * * * * *")]TimerInfo myTimer, ILogger log)
{
log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
FabricClient client = new FabricClient();
}
}
}
Is this possible, or a limitation of Azure Functions in Visual Studio - if so, what packages are acceptable?
ServiceFabric packages are x64 bit, if your function target 32bit it will fail. Try the solution proposed by Jerry Liu
Service Fabric Packages have to be added as packages instead of reference the dll directly in the project, because of the dependencies on other libraries. You should add the NuGet package Microsoft.ServiceFabric.
Microsoft.ServiceFabric latest version 6.3.x targets .Net Standard 2.0 and .Net Framework from 4.5 to 4.7.1, make sure you are using any of these on your project.
Make sure the Microsoft.ServiceFabric DLLs are being copied to the bin folder when built\deployed.
When you use FabricClient outside the cluster, you have to specify the settings and credentials, otherwise you won't be able to connect to the cluster. See this example and this docs.
FabricClient uses Service Fabric API to interact with the cluster, if are having issues with the packages, another option is use HttpClient and make the requests to the API and avoid the packages conflicts
Diego and Suraj have pointed out the cause, conflict between 64 and 32 bit.
Two points to fix
Set build platform to x64 like what you have done.
Get x64 Function runtime. Functions work on Function runtime(contained in Azure Function core tools), but the default bit is x86 downloaded by VS.
To get x64 bit in an easy way, let's use Nodejs to install Azure Functions Core Tools from NPM.
After installation, in cmd input npm i -g azure-functions-core-tools --unsafe-perm true to get Function core tools.
Then set project debug properties(right click on your project>Properties>Debug blade).
Set Launch type to Executable
Set Executable path to %appdata%\npm\node_modules\azure-functions-core-tools\bin\func.exe.
Add Application arguments start.
I run into exactly same issue as #tank140 commented in original post:
Unable to load DLL 'FabricClient.dll' or one of its dependencies: The specified module could not be found. (Exception from HRESULT: 0x8007007E)
More details in another question that I fired on the issue. As answer, it was confirmed that SF Client API for .NET requires that SF runtime is installed on the platform, which is not supported in Azure Functions.
In my case I just update the azure platform configuration to 64 bit. However, I was using .net core 3.1 function app into win platform.

WebApi controller tests method not found

I have a very simple WebAPI 2 controller running on .NET Framework 4.6.2, that looks like this:
[RoutePrefix("Invitations")]
public class InvitationsController : CqrsApiController
{
[HttpPost, Route("Clients/{id:long}/Actions/Cancel")]
public IHttpActionResult PostClientInvitationCancel(long id, [FromBody] ClientInvitationCancelCommand command)
{
Execute(command);
return SeeOther("Invitations/Clients/{0}", id);
}
}
and am trying to write an NUnit test for it, like this:
[TestFixture]
public class WhenExecutingAValidCommand
{
[Test]
public void ItShouldReturnARedirect()
{
var dispatcher = Substitute.For<ICqrsDispatcher>();
var urlHelper = Substitute.For<UrlHelper>();
urlHelper.Link(Arg.Any<string>(), Arg.Any<object>()).Returns("https://tempuri.org/");
var sut = new InvitationsController(dispatcher);
sut.Request = new HttpRequestMessage();
sut.Configuration = new HttpConfiguration();
sut.Url = urlHelper;
var response = sut.PostClientInvitationCancel(1, new ClientInvitationCancelCommand());
response.Should().BeOfType<SeeOtherRedirectResult>();
}
}
```
However, when I run the test, I get the following error:
System.MissingMethodException : Method not found: 'Void System.Web.Http.ApiController.set_Request(System.Net.Http.HttpRequestMessage)'.
at ApiProjectTests.InvitationsControllerTests.WhenExecutingAValidCommand.ItShouldReturnARedirect()
The same code seems to work fine in similar projects based on .NET Framework 4.5.1, so I'm wondering if there's some sort of DLL hell going on here. System.Web.Http is using Microsoft.AspNet.WebApi.Core.5.2.3, whereas System.Net.Http is coming from the GAC (well, C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\Microsoft\Microsoft.NET.Build.Extensions\net461\lib\System.Net.Http.dll to be more precise).
Update: if I try to debug into the unit test, the error occurs before I even enter the method. So although VS2017 compiles the tests just fine, when the test runner fires up, then everything falls apart. Sounds more like DLL hell to me.
Update 2: if I comment out the setting of the request, then I can debug into the test method. If I then put in a breakpoint, and then use the Immediate window to directly set the request property, it works, and there is no Method not found error. I also disabled Resharper and used VS2017's Test Explorer to run the tests, in case R# was caching something, but it made no difference.
It looks like my problem is indeed DLL hell, more specifically the DLL hell referenced by https://github.com/dotnet/corefx/issues/25773. The issue is caused by other NuGet packages that contain references to the newer version of System.Net.Http (4.2.0.0). The current solution appears to be to add a binding redirect to downgrade the assembly version to the expected version (4.0.0.0), but so far that has not helped me.
The solution that did work for me was to install the latest NuGet package of System.Net.Http, and use assembly binding redirects in my test project to ensure that it used the 4.2.0.0 version instead of 4.0.0.0.
This is often caused by early versions of nuget packages targeting .NET standard, which have dependencies on OOB ("out-of-band") packages. OOB packages are a kind of polyfill for dlls that are part of .NET framework but not .NET standard. Here is a very good explanation of what happened. In my case, the following helped:
I identified the nuget package that had a dependency on the system.net.http 4.2.0 nuget package, and upgrade that package.
The dependency was no longer present in the upgraded package, so i could uninstall the system.net.http 4.2.0 nuget package.
The upgraded package of course still expects the reference to the system.net.http 4.0.0 assembly, so in case of doubt, you may reinstall the upgraded package to make sure that the assembly reference is in your *.csproj file.

Why WindowsIdentity class is not visible in .NET Core

Having the code below in VisualStudio 2017 .NET Core 2.0 Console App
using System;
using System.Security.Principal;
namespace smallTests
{
class Program
{
static void Main(string[] args)
{
var identity = WindowsIdentity.GetCurrent();
}
}
}
Why am I getting the error:
The name 'WindowsIdentity' does not exist in the current context
If I can see this class in .NET Core 2.0 library in .Net Core docs ?
Same code works in .NET Console app.
[EDIT]
#Will #JohnnyL Commented that I do not refer, System.Security.Principal.Windows.dll, that is true.
But I am curious why it is not working, because
in .NET 4.6.1 project (where class WindowsIdentity is visible) I also do not refer this System.Security.Principal.Windows.dll specifically. However i refer System.dll.
I always thought that it works like namespace hierarchy. For instance, when I refer to
System.Security.Principal.dll
i can use class which is in
System.Security.Principal.Windows.dll.
Am I wrong?
I added System.Security.Principal.dll to .NetCore solution by hand but it still does not work.
[EDIT2]
#Will Thank you a lot for expaining the subject it helped me a lot.
I tried to figure out is WindowsIdentity compatible with Core and it seems that it is please see:
in this apisof.net in Declarations area i can see that WindowsIdentity is in .Net Core 2.0 System.Security.Principal.Windows, Version=4.1.1.0, PublicKeyToken=b03f5f7f11d50a3a
but i do not have System.Security.Principal.Windows.dll in references, should I add it? If yes from where?
in .NET Core api reference i see this class in the list (what is the purpose of that listing if it is not compatible with core?
I also find information about that class in that link
Am I looking in wrong places?
Microsoft announced Windows Compatibility Pack for .NET Core a few weeks ago,
https://blogs.msdn.microsoft.com/dotnet/2017/11/16/announcing-the-windows-compatibility-pack-for-net-core/
And by analyzing the source code of System.Security.Principal.Windows.csproj and the commit adding it,
https://github.com/dotnet/corefx/blob/master/src/System.Security.Principal.Windows/src/System.Security.Principal.Windows.csproj
My conclusion is that this is also part of the Windows only compatibility libraries, so can only be used on Windows.
To add that to your project, open your csproj and add a PackageReference tag for System.Security.Principal.Windows manually (or use Visual Studio's NuGet Package Manager).

Getting Autofac to work with MVC6/ASP.NET5

I cannot get autofac to work, I have looked at this potentially duplicate question, but it doesn't help.
I am using the full .NET stack, DNX 4.5.1
I have included the following dependencies.
"dependencies": {
// matched latest autofac version with latest dependencyinjection version.
"Autofac": "4.0.0-beta8-157",
"Autofac.Framework.DependencyInjection": "4.0.0-beta8-157",
"Microsoft.AspNet.Mvc": "6.0.0-rc1-final" ...
And the following initialisation code.
// void?
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
var container = new ContainerBuilder();
...
// compilation error here!
container.Populate(services);
}
I am receiving this error:
Error CS1503 Argument 2: cannot convert
from'Microsoft.Extensions.DependencyInjection.IServiceCollection' to
'System.Collections.Generic.IEnumerable<Microsoft.Framework.DependencyInjection.ServiceDescriptor>'
MuWapp.DNX 4.5.1 C:\MuWapp\Startup.cs 54 Active
For RC1 you will need to use the Autofac.Extensions.DependencyInjection package.
https://www.nuget.org/packages/Autofac.Extensions.DependencyInjection/
We renamed our package to align with Microsoft's rename to Microsoft.Extensions.DependencyInjection. It's been a moving target supporting the early DNX releases.
As I've mentioned in the comment, you should use compatible versions of all packages in your project.json. I see on their page: https://github.com/autofac/Autofac/releases that they have released version for RC1, however there is no Autofac.Framework.DependencyInjection for RC1, so if you require this package, you will be unable to run it.
I think you should use built-in dependency injection during your development untill there is RTM version and all of the third-party packages will become stable.
Build-in DI has functionality for injecting classes into controllers, properties as well as attributes, so unless you use some advanced scenarios in which autofac is necessary, you should stick to asp.net 5 DI.

Build TagLib# DLL from source and make it COM Visible to PHP

Hello I want to scan audio-video files and store their metadata in a database using php. I found this Command-line wrapper that uses TagLib.dll compiled by banshee developpers to do the job. It's works fine but it limited by the functions implemented. I want to access directly to the dll methods via PHP.
In PHP we have a function (DOTNET) that allows me to instantiate a class from a .Net assembly and call its methods and access its properties like this :
/*$obj = new DOTNET("assembly", "classname");*/
$stack = new DOTNET("mscorlib", "System.Collections.Stack");
$stack->Push(".Net");
$stack->Push("Hello ");
echo $stack->Pop() . $stack->Pop();
//Returns string(10) "Hello .Net";
Here is the Taglib# project sources in github
I saw many questions relatives to PHP-DLL-COM and there is some recommendations :
Make the dll comVisible;
Register the dll with regsvr32;
Use a module definition file similar to
;
;
DESCRIPTION "Simple COM object"
EXPORTS
DllGetClassObject PRIVATE
DllCanUnloadNow PRIVATE
DllRegisterServer PRIVATE
DllUnregisterServer PRIVATE
My question is : How can I build the DLL and use its method via PHP ?
My config :
OS
Windows Server 2012 R2 Standard Edition i586
Apache :
Apache/2.2.21 (Win32) DAV/2 PHP/5.4.42 mod_ssl/2.2.21 OpenSSL/0.9.8r
PHP
PHP Version : 5.4.42
Arch : x86
Compiler : MSVC9 (Visual C++ 2008)
COM support : enabled
DCOM support : disabled
.Net support enabled
Microsoft Visual Studio 2013
Give the following steps a try:
Download taglib source code from github
Remove the ApplicationIcon tag from the .csproj file and open .sln in Visual Studio
Unload the test project (you don't need to build this)
Right-click on taglib-sharp project --> properties --> build --> enable 'Register for COM interop'. Also clear the conditional compilation symbols textbox so that you don't have to bother with downloading SharpZipLib for now.
Observe #1: in project properties --> Application --> Assembly information --> 'Make assembly COM visible' is checked
Observe #2: in project properties --> Application --> target framework is set to 3.5 (make sure you leave it as that)
Build the project (F6)
Read the contents of the output window to see some warnings
Now that you have src\taglib-sharp.dll you need to register it into the global assembly cache in order for the DOTNET class to find it. See PHP DOTNET hell for details if you are not familiar with this.
If all is good, you can get yourself a copy of SharpZipLib, and reenter the HAVE_SHARPZIPLIB conditional compilation symbol --> rebuild --> redeploy to GAC --> and be a happy man! :)
You may compile you DLL with .NET Framework 3.5 if not, PHP do not be able to load it via DOTNET class.
Download .NET 3.5

Categories