Reflection error with self-hosted nancyfx app on mono - reflection

After building and successfully running a self-hosted NancyFx application on windows with visual studio I went on and tried to run the application on linux with mono. I was able to build the solution (using xbuild) but when running the app I get a strange error:
Unhandled Exception:
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. --->
System.TypeLoadException: Could not load type 'System.Net.HttpListener'
from assembly 'System, Version=4.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089'.
at Microsoft.Owin.Host.HttpListener.OwinServerFactory.Initialize (IDictionary`2 properties) <0x413ae4f0 + 0x000eb> in <filename unknown>:0
For some reason, the app tries to initialize class HttpListener from the System assembly, but HttpListener lives in System.Netassembly. I looked in the GAC folder and found that System.Net is there as expected. I moved a copy to the bin folder where the other dependencies of the app live but that didn't solve the problem. Any help would be appreciated!!
here is my OWIN-related code:
// Startup.cs
using Owin;
namespace MyApp
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.UseNancy();
}
}
}
// Program.cs
using System;
using Microsoft.Owin.Hosting;
namespace MyApp
{
public class Program
{
static void Main(string[] args)
{
var url = "http://127.0.0.1:8080/";
using (WebApp.Start<Startup>(url))
{
Console.WriteLine("Running on {0}", url);
Console.WriteLine("Press enter to exit");
Console.ReadLine();
}
}
}
}

Yep, wrong assembly indeed.
Im not sure why yet, but I'm having the same issue with CENTOS only using mono 4.3.178. Debian Jesse using version 4.2.x had no issues. Several other Debian Jesse boxes I use to test seem to have no issues with 4.3.x. In my case I'm listening on an alternate port.
RedHat Strikes Again :/
A MUCH deeper stack trace into the IL seems to indicate that gcc on the machine you built mono on, and also the build essentials package have some extremely dated libs and even the compiler. I was surprised to see the number of warnings, nearly 70,000 as opposed to a typical 400 depending on the diversity of libs you have. I have about 10 hours to figure this out, so hopefully its not too bad.

Related

Custom MsBuild Task fails with assembly loading errors

I have defined a custom MSBuild Task, using .netcore 5.0 and it is accessible here.
The "Application" project uses "SnykTaskFile" in its build process.
When I build "Application" I got the following error :
System.BadImageFormatException: Could not load file or assembly
'System.Diagnostics.Process, Version=5.0.0.0,
If I remove the following code from "SnykTaskFile" :
var processInfo = new ProcessStartInfo();
processInfo.FileName = "cmd";
processInfo.WorkingDirectory = Location;
Process.Start(processInfo);
And build the "Application" again, it builds successfully, however if I have a logic around "Location" :
if (Location.Contains("...."))
{
return false;
}
It throws another error :
System.IO.FileNotFoundException: Could not load file or assembly
'System.Runtime, Version=5.0.0.0,
According to the answer here and the discussion "Custom Tasks don't work with NET5.0" in git hub, it seems I should use netstandard 2.0 for task project type.

Error: Can't load metadata reference from the entry assembly. Make sure PreserveCompilationContext is set to true in *.csproj file

This problem is specific to RazorLight.
Error:
Can't load metadata reference from the entry assembly. Make sure
PreserveCompilationContext is set to true in *.csproj file
This error pops up only after we deploy to AWS. On the local machine things work fine. I've already added PreserveCompilationContext to the *.csproj file.
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<PreserveCompilationContext>true</PreserveCompilationContext>
</PropertyGroup>
We use circleci for deployments. The API that's having this problem is hosted in AWS Lambda.
private async Task<string> GenerateText(string template, ProseModel model)
{
var engine = new RazorLightEngineBuilder()
.UseMemoryCachingProvider()
.Build();
try
{
// ERROR thrown on next line
var result = await engine.CompileRenderAsync(Guid.NewGuid().ToString(), template, model);
return result;
}
catch(Exception e)
{
Logger.LogError("Error generating template", e);
throw e;
}
}
I found that people are getting this same error in Azure Functions. Is that similar to Lambda's and maybe requires a similar fix? If yes, how can I fix this in a Lambda?
I've also tried to set SetOperatingAssembly(Assembly. GetExecutingAssembly())
I ran into the same issue but the fix that you posted for the Azure Function hack worked for me. You must make sure to replace the "RazorLight" package with the "RazorLight.Unofficial" package version beta1.3. Then it should work.
The problem is that the entry assembly when running on Lambda is called:
Bootstrap, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
Which I'm assuming isn't compiled to preserve the compilation context.

Entry point not found in .NET Core 2.0 DLL

I couldn't find anything that explains this -- for some reason my .NET Core 2.0 ASP.NET application does not run as a DLL via:
dotnet MyProject.Web.dll
And instead I get the exception:
Unhandled Exception: System.MissingMethodException: Entry point not found in assembly 'MyProject.Web, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.
namespace MyProject.Web
{
public class Program
{
public static void Main(string[] args)
{
LoadDependencies();
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
host.Run();
}
private static void LoadDependencies()
{
DependencyLocator.Instance.DefineIfUndefined<IDataProvider, DataProvider>();
}
}
}
It runs fine as a standalone executable (when targeting a "Console Application" in the project's config), but now that I'm trying to deploy to a server that needs it to run via the dotnet command (as a DLL, i.e. "dotnet .\MyProject.Web.dll"), it seems to be having issues. I get the above exception on both my server and my local development box.
I'm kind of blown away that it cannot locate the Main method -- it's declared as static and in Program.cs. Am I missing something?
(EDIT: To clarify, the DLL I'm trying to run against the "dotnet" command is from the target compiling as a "Console Library," since my server is explicitly asking for a DLL, since they will not run executables).
OK, so this is annoying and will hopefully help someone else out.
My host wants to specifically run DLL's thru .NET Core ONLY. They do not allow for executables to be run.
Because DLL's are frequently built as "Class Library" output types on the project, I assumed that this was the workflow necessary to build it. However, I found out that whenever you build your project as a "Console Application," it builds a DLL in addition to an EXE. So, in the above example, MyProject.Web.exe and MyProject.Web.dll are both built when the output type is "Console Application."
MyProject.Web.dll that comes from "Console Application" is different than MyProject.Web.Dll that comes from "Class Library." The one that comes from "Class Library" will NOT have an entry point that can be discovered on it, which will lead to the problem above.
So, if you're getting this error, look for the DLL that ships with your EXE of the same name -- that's the actual DLL you'll want to run in your dotnet console (i.e. dotnet MyProject.Web.dll)

ReactJS.NET - Bundles - TinyIoCResolutionException: Unable to resolve type: React.IReactEnvironment

I'm attempting to minify my .JSX files with ASP.NET Minification and Optimization via System.Web.Optimization.React. I've installed the MVC4 React Package as well as the Optimization package, but whenever I try to include a bundle I get the following:
React.TinyIoC.TinyIoCResolutionException: Unable to resolve type: React.IReactEnvironment
The InnerException is always null
My bundles are setup as follows:
bundles.Add(new ScriptBundle("~/Bundle/Scripts/ReactJS").Include(
"~/Scripts/React/react-0.12.2.js",
"~/Scripts/React/react-with-addons-0.12.2.js",
"~/Scripts/React/JSXTransformer-0.12.2.js"
));
bundles.Add(new JsxBundle("~/Bundle/Scripts/ReactCalendar").Include(
"~/Scripts/React/Calendar/Main.react.jsx",
"~/Scripts/React/Calendar/Components/Calendar.react.jsx",
"~/Scripts/React/Calendar/Components/CalendarEvent.react.jsx",
"~/Scripts/React/Calendar/Components/CalendarControls.react.jsx",
"~/Scripts/React/Calendar/Components/CalendarTimeSlots.react.jsx"
));
And included in the view as:
#section scripts{
#Scripts.Render("~/Bundle/Scripts/ReactJS");
#Scripts.Render("~/Bundle/Scripts/ReactCalendar");
}
The error is always thrown on line:
#Scripts.Render("~/Bundle/Scripts/ReactCalendar");
Anyone got any ideas on how to solve / debug this one? Let me know if more info is needed.
I'm not sure if this is the same issue I was facing, but I googled the exact same error, found this SO topic as the first hit, with no definitive answer, so I thought I'd offer my solution.
I'm using .NET 4.5 in an MVC app, and React.Web.Mvc4 v3.0.0.
I managed to work around this issue with the help of this comment on Github.
Here's my entire ReactConfig.cs:
using React;
using React.TinyIoC;
using React.Web.TinyIoC;
namespace NS.Project
{
public static class ReactConfig
{
public static void Configure()
{
Initializer.Initialize(AsPerRequestSingleton);
ReactSiteConfiguration.Configuration
.SetLoadBabel(false)
.AddScriptWithoutTransform("~/React/dist/server.bundle.js");
}
private static TinyIoCContainer.RegisterOptions AsPerRequestSingleton(
TinyIoCContainer.RegisterOptions registerOptions)
{
return TinyIoCContainer.RegisterOptions.ToCustomLifetimeManager(
registerOptions,
new HttpContextLifetimeProvider(),
"per request singleton"
);
}
}
}
Then, I'm callingReactConfig.Configure explicitly from Application_Start.
"Unable to resolve type: React.IReactEnvironment" with no InnerException generally means ReactJS.NET is not initialising properly for some reason. In web apps, ReactJS.NET handles initialisation through the use of WebActivator. Make sure your project is referencing React.Web, React.Web.Mvc4 and WebActivatorEx, and all the corresponding .dll files are ending up in your app's bin directory.
Also, you do not need to (and should not) include JSXTransformer in your JavaScript bundles, as ReactJS.NET does all the JSX compilation server-side.
Something looks like changed from React.Web.MVc4 version 4.0.0. versions before didnt have that problem.
as stated here
Install the React.Web.Mvc4 package through NuGet. You will also need to install a JS engine to use (either V8 or ChakraCore are recommended). See the JSEngineSwitcher docs for more information.
To use V8, add the following packages:
JavaScriptEngineSwitcher.V8
JavaScriptEngineSwitcher.V8.Native.win-x64
ReactConfig.cs will be automatically generated for you. Update it to register a JS engine and your JSX files:
using JavaScriptEngineSwitcher.Core;
using JavaScriptEngineSwitcher.V8;
[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(React.Sample.Mvc4.ReactConfig), "Configure")]
namespace React.Sample.Mvc4
{
public static class ReactConfig
{
public static void Configure()
{
ReactSiteConfiguration.Configuration
.AddScript("~/Content/Sample.jsx");
JsEngineSwitcher.Current.DefaultEngineName = V8JsEngine.EngineName;
JsEngineSwitcher.Current.EngineFactories.AddV8();
}
}
}
If anyone needs this, just install this nuget and it will resolve this issue.
System.Web.Optimization.React

ASP.net app crashes - Could not load file or assembly 'Microsoft.Threading.Tasks.Extensions.Desktop'

I want to build a Google BigQuery C# ASP.net application using OAuth2 and the .Net 4.5 framework. I ran these NuGet installs
Install-Package Google.Apis.Bigquery.v2 -Pre
Install-Package Google.Apis.Authentication.OAuth2 -Version 1.2.4696.27634
Install-Package Google.Apis -Pre
Install-Package Google.Apis.Auth -Pre
and I placed the relevant "usings" in code-behind file "default.aspx.cs":
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Services;
using Google.Apis.Bigquery.v2;
using Google.Apis.Bigquery.v2.Data;
namespace BigQueryDemoApp
{
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
UserCredential credential;
FileStream stream;
using (stream = new FileStream(
Server.MapPath("~/client_secrets.json"),
FileMode.Open, FileAccess.Read)
)
{
GoogleWebAuthorizationBroker.Folder =
"Tasks.Auth.Store";
credential = GoogleWebAuthorizationBroker.
AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
new[] { BigqueryService.Scope.Bigquery },
"user", CancellationToken.None).Result;
}
// Initialize the service.
var Service = new BigqueryService(
new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "BigQueryDemo"
}
);
}
}
}
I set this specific page as the project start page. I picked "Installed application" when I built the Client ID file at the Google console
APIS & auth -> Credentials -> CREATE NEW CLIENT ID
and I made sure I added this file (client_secrets.json) with the solution explorer in VS2013. In the code-behind, I made sure that I correctly mapped to the client_secrets file with Server.MapPath. For the credential machinery, I used this code
<https://code.google.com/p/google-api-dotnet-client/wiki/OAuth2>
as the starting point. When I run the app, it returns a browser error page that starts with
Could not load file or assembly 'Microsoft.Threading.Tasks.Extensions.Desktop, Version=1.0.16.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
and crashes at the "credential =" line. I tried to add in some images of the actual ASP.net crashed browser page showing the Assembly Load Trace / Stack Trace / etc. but it looks like I don't have the account rights for this. When I set a breakpoint at the "credential =" line and then run the app through
DEBUG -> Start Debugging
in VS2013, the page stops at the "credential =" line and a file picker opens, looking for file
"GoogleClientSecrets.cs"
from directory
"c:\code\google.com\google-api-dotnet-client\default\Tools\Google.Apis.Release\bin\Debug\output\default\Src\GoogleApis.Auth\OAuth2\GoogleClientSecrets.cs"
which is nowhere on the drive. Using the Assembly Load Trace in the generated ASP.net error page, I tried digging around through the suggested configuration files but nothing worked. More generally, I tried looking for this issue in StackOverflow and while I did find some mention of it, none of that material helped.
Because the error is based on the fact that the latest version of Microsoft.Bcl.Async doesn't work in .NET 4.5, you can try to do the following:
Open your Package Manager Console, and run the following commands:
1) Uninstall-Package Microsoft.Bcl.Async -Force
2) Install-Package Microsoft.Bcl.Async -Version 1.0.16
It works in a sample I'm currently writing. Please let me know if it works for you.
UPDATE (March 21st):
You can update the package (new version 1.0.166-beta is available - https://www.nuget.org/packages/Microsoft.Bcl.Async/1.0.166-beta).
I tested it on VS2013 with .NET 4.5 framework and it works.
They released a new version of -Package Microsoft.Bcl.Async.
If somebody has this issue, please install the "latest" version instead of 1.0.16.
I hope it works for you.
I already encountered this error before. It looks like the Bcl.Async package contains a reference to Microsoft.Threading.Tasks.Extensions.Desktop when you run a .NET 4.0 applications but somehow it is missing in .NET 4.5 application.
My advice for you (until I'll figure our with the owner of Microsoft.Bcl.Async why it happens) is to copy Microsoft.Threading.Tasks.Extensions.Desktop from packages\Microsoft.Bcl.Async.1.0.165\lib\net40\Microsoft.Threading.Tasks.Extensions.Desktop.dll to your BIN folder. It should solve this issue.
UPDATE (March 17th):
Consider adding the following Post-build event to your project:
copy /Y "$(SolutionDir)packages\Microsoft.Bcl.Async.1.0.16\lib\net40\Microsoft.Threading.Tasks.Extensions.Desktop.dll" "$(TargetDir)Microsoft.Threading.Tasks.Extensions.Desktop.dll"
Unfortunately, there isn't a solution for this problem yet from the owners of the Bcl.Async package.
This approach did not fix the issue - I got the same runtime error. But after a rebuild, I noticed that the VS2013 compiler showed this warning, which I formatted a little for the SO editor
C:\Program Files (x86)\MSBuild\12.0\bin\Microsoft.Common.CurrentVersion.targets(1635,5): warning
MSB3247: Found conflicts between different versions of the same dependent assembly. In Visual
Studio, double-click this warning (or select it and press Enter) to fix the conflicts;
otherwise, add the following binding redirects to the "runtime" node in the application
configuration file:
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Microsoft.Threading.Tasks.Extensions.Desktop" culture="neutral" publicKeyToken="b03f5f7f11d50a3a" />
<bindingRedirect oldVersion="0.0.0.0-1.0.165.0" newVersion="1.0.165.0" />
</dependentAssembly>
</assemblyBinding>
so I dropped the suggested block in the app web.config file. Then the app decided to work. I have no idea why it works now, but I get the impression that the XML block and / or the reference fix you mentioned somehow touched the Microsoft.Threading.Tasks.Extensions.Desktop DLL, or some low-level machinery inside .Net, or both. Or neither, for all I know. Anyway, thanks for your help. I only wish I had a better understanding of the internal machinery.

Resources