ASP.NET: ICollection Constructor Not Found? - c#

I have a ASP.NET application running on a remote web server and I just started getting this error. I can't seem to reproduce it in my development environment:
Method not found: 'Void System.Collections.Generic.ICollection`1..ctor()'.
Could this be due to some misconfiguration of .NET Framework or IIS 6?
Update:
I disassembled the code in the DLL and it seems like the compiler is incorrectly optimizing the code. (Note that Set is a class that implements a set of unique objects. It inherits from IEnumerable.) This line:
Set<int> set = new Set<int>();
Is compiled into this line:
Set<int> set = (Set<int>) new ICollection<CalendarModule>();
The Calendar class is a totally unrelated class!! Has anyone ever noticed .NET incorrectly compiling code like this before?

Are the .NET versions on both systems the same inc. the same service pack?

Is your IIS setup to use .NET 2.0? If not, change it to 2.0. If you can't see 2.0 in the list then you'll need to run aspnet_regiis from the 2.0 framework directory.

This was caused by a bug in the aspnet merge tool which incorrectly merged optimized assemblies. It can be solved by either not merging the assemblies or not optimizing them.

Related

Sytem.Reflection.Emit error when establishing XML client in Azure Function

Trying to establish an xmlrpc to a third party web service from my function app. It works fine from localhost, and indeed, it works in other functions but when published to Azure I'm getting this error:
System.MissingMethodException: Method not found: 'System.Reflection.Emit.AssemblyBuilder System.AppDomain.DefineDynamicAssembly(System.Reflection.AssemblyName, System.Reflection.Emit.AssemblyBuilderAccess)'.
at CookComputing.XmlRpc.XmlRpcProxyGen.BuildAssembly(Type itf, String assemblyName, String moduleName, String typeName, AssemblyBuilderAccess access)
at CookComputing.XmlRpc.XmlRpcProxyGen.Create(Type itf)
at CookComputing.XmlRpc.XmlRpcProxyGen.Create[T]()
It's failing at this line:
_serviceClient = XmlRpcProxyGen.Create<IServiceClient>();
IServiceClient is an interface that looks like this:
using CookComputing.XmlRpc;
using System;
namespace App.Core.Helpers.Service
{
[XmlRpcUrl("https://some.url")]
public interface IServiceClient : IXmlRpcProxy
// methods
}
I've manually added the System.Reflection.Emit package to the solution in an attempt to fix it, but no joy. The exact same code is running fine in other apps we have in Azure. The app is running .Net framework 4.6.1.
This is a problem which can occur when there is an old version of a DLL still lingering somewhere around. Make sure that the latest assemblies are deployed and no duplicated older assemblies are hiding in certain folders. Your best bet would be to delete every built item(Clean Build) and Rebuild/redeploy the entire solution.
In particular, be sure an old version is not in the GAC.
This can help to Remove an Assembly from the Global Assembly Cache.
I was facing same issue as you were facing.
What I found solution is that, I was using asp.net core framework So I have to change it into net 4.5 framework. And that's all.
Issue resolved.
So you may try that.

Fixing PlatformNotSupportedException when referencing System.Data.SqlClient from C# Azure Function

I am creating an Azure Function in C# using a target framework of netstandard2.0 in a Windows 10 environment. The function calls a method that's in another class library and that method creates an instance of SqlConnection. When I run the function I get the following exception:
Microsoft.Azure.WebJobs.Host.FunctionInvocationException : Exception
while executing function: Functions.RefreshImages --->
System.Reflection.TargetInvocationException : Exception has been
thrown by the target of an invocation. --->
System.PlatformNotSupportedException : System.Data.SqlClient is not
supported on this platform. at
System.Data.SqlClient.SqlConnection..ctor(String connectionString)......
Obviously SqlConnection is supported on Windows so I assume there's something else going on here.
This happens when a .Net Standard lib that uses a SqlConnection is loaded dynamically via reflection. The .Net Standard lib will typically reference System.Data.SqlClient which seems to be a dummy lib without actual implementation. It apparently ensures that the lib will compile on all platforms, including those without Registry and other platform specific stuff that the real SqlClient implementation relies on.
The easiest solution I can find is to add a reference to the Microsoft.Data.SqlClient NuGet package in the host application (the .Net core application that dynamically loads the .Net standard lib).
You may see a small yellow warning exclamation icon in the Solution explorer because Visual Studio thinks you are not using the lib and if you use the "Remove Unused References" feature it will also suggest removing the package. There is a feature to suppress warnings in the PropertyGrid, but I cannot figure out wat number should be filled in since the warning does not appear in the error list when compiling...
It looks like this is related to loading a SQL connection via reflection in .NET core (you are running on netstandard2.0 but the principle should still be the same).

BadImageFormatException when migrating from ASP.Net Core 1.1 to 2.0

I just migrated an ASP.Net Core 1.1 application to the new 2.0 version that was just released. Now I get the following exception:
System.BadImageFormatException: 'Could not load file or assembly
'dotnet-aspnet-codegenerator-design' or one of its dependencies. An
attempt was made to load a program with an incorrect format.'
The exception is thrown on the following line (AddMvc):
public IServiceProvider ConfigureServices(IServiceCollection services)
{
...
services.AddMvc(options =>
{
options.Filters.Add(new MiddlewareFilterAttribute(typeof(LocalizationPipeline)));
})
.AddJsonOptions(options =>
{
// Maintain property names during serialization. See:
// https://github.com/aspnet/Announcements/issues/194
options.SerializerSettings.ContractResolver = new DefaultContractResolver();
})
.AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
.AddDataAnnotationsLocalization();
}
I'm targeting the .Net Framework 4.7, using AnyCPU target platform. I'm using the win10-x64 RID if this is of any help.
Every Nuget package is up to date etc.
Any idea? I've got no luck by googling.
the same exception occurred when I switched from x86 to x64 (after upgrading from core 1.1 to 2.0).
Since dotnet-aspnet-codegenerator-design is not really needed at runtime, I removed that reference. But then an identical exception occurred with the Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv assembly.
Luckily, that problem was related to the fact I didn't change the .NET Core Runtime Identifier (RID). This was still set on win7-x86, so I changed it to win7-x64 and the Libuv devepency could load.
I hope there will be some fix for dotnet-aspnet-codegenerator-design since that dependency is probably necessary for scaffolding controllers and views...
Update: there is an issue for this on Github (https://github.com/aspnet/Scaffolding/issues/601)
There's a chance that the dependency is corrupted. You can use a tool like Everything (https://filehippo.com/download_everything/) to search and find the corrupt dependency. There's a good chance it will be somewhere in this folder (C:\Program Files\dotnet\shared). Once you find it, try to google and redownload it(there's a good chance, you'll find a nuget for it) and then replace the old\corrupt version (be sure to back up).
If you're having trouble finding a replacement for the dependency online, simply publish your project on to a folder, all the dependencies would be copied to that folder. If you publish your project, and you run dotnet PROJECTNAME.dll and you still get the same error, you might have to run a clean install or repair on Visual Studio.
I hope this saves someone a lot of time, this error can take a while to debug.
I was getting this error too on SmarterASP.NET hosting. I couldn't try all the file updates suggest here, but found another solution, prior to which I tried compiling to several targets without success. Finally, it all just started working when I targeted for Portable. On Smarter it is now running nicely as a Framework Dependent Deployment, ASP 2.2 Core, Portable. Sometimes I find getting Core running is like having to get out the ol' trial-and-error chops. As always, your mileage may vary.

C# Can't find reference system.web.ui

I'm using framework 2.0 and trying to build project,but i'm getting a lot of errors in the aspx.designer.cs pages,example.
protected global::System.Web.UI.UpdatePanel updMenu;
The error says the name UpdatePanel doesn't exist in the web.ui namespace,i think its because the framework doesn't support this?Or it't other thing that is causing this?
In the references i have the system.web,i exlucluded and included multiple times and errors continues.
You must use .Net Framework 3.5 or upper.
https://msdn.microsoft.com/en-us/library/system.web.ui.updatepanel(v=vs.110).aspx

Method 'ExecuteAsync' in type 'System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy' does not have an implementation

I am newer about using Code First in c#.
After I enabled Migration in my project and launch my site, I get an Error:
Method 'ExecuteAsync' in type 'System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy' from assembly 'EntityFramework.SqlServer, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' does not have an implementation.
I have defined the context class as below.
namespace MyOA.Migration.Contexts
{
public class OADBContext : DbContext
{
public OADBContext() { }
}
}
and I tried to create the DB in Global.asax as below.
protected void Application_Start()
{
// Forces initialization of database on model changes.
using (var context = new Migration.Contexts.OADBContext())
{
context.Database.Initialize(force: true);
}
}
I tried to search the reason but got no idea.
Any suggestions?
Thanks in advance.
If you check the .NET version of the two asseblies:
EntityFramework (v4.5)
EntityFramework.SqlServer (v4.0)
You will see that EntityFramework.SqlServer has v4.0 .NET dependency, but EntityFramework uses v4.5. That is the root of the issue. I use dotpeek tool for checking the assembly version (there are other options from stack overflow to check .net vestion of an assembly).
Note: and really when you decompile EntityFramework.SqlServer.dll using jetBrains reflector tool you will find that there is no ExecuteAsync method.
What we have to do to fix the issue is to use nuget.exe (from visual studio or from stand alone nuget executable: please find "latest nuget.exe"). And run it from command line:
cd "[path to your nuget.exe]"
nuget Install EntityFramework
A set of EF 6 assemblis will be downloaded. Use the EntityFramework and EntityFramework.SqlServer from .net v4.5 folder.
I +1'd #Spirit's answer for pointing me at the root of the issue, but I was able to fix it for myself by searching through all of the *.csproj files in the solution and replacing
packages\EntityFramework.6.1.3\lib\net40\EntityFramework.dll
^^^^^
with
packages\EntityFramework.6.1.3\lib\net45\EntityFramework.dll
^^^^^
I had a mix of usages in the projects. My symptom was that NCruch would get the exception on a test, but running the same test from NUnit test runner would pass just fine.
I faced this issue in my work, and tried suggested solutions with no luck, but the solution that worked out for us was to use a relatively older version of Entity Framework, we used version 6.0.0 while the latest (as of the time I'm writing down this answer) is 6.1.3
The issue with me was that my project was targeting .Net Framework 2.0, and suddenly when upgraded to .Net Framework 4.6.1 a warning appeared so I tried to update Entity Framework package which was on the latest 6.1.3, but it stopped working properly, and got the exception mentioned above.
Hope this will help somebody later.
I had this same problem but the cause was different. In the "C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files" folder, there were several versions of the entityframe dlls. No doubt older versions. I had already recycled the iis pool and restarted the computer but that didn't work. I had to delete all the files in that folder and that did the trick.
Since all similar S/O questions are being directed to this one, I'll leave another idea to consider. Check to make sure that EntityFramework hasn't been installed into you GAC by chance. IIS will take the GAC version over your bin version leading to some serious head scratching.

Categories