Troublesome System.TypeLoadException - c#

I'm currently managing an ASP.NET application for my company. Recently, I have been getting a System.TypeLoadException when I try debugging the code.
The exact message is:
Inheritance security rules violated by type: 'System.Net.Http.Formatting.JsonContractResolver'. Derived types must either match the security accessibility of the base type or be less accessible.
And it keeps pointing to the following region as the source of the error:
Line 21: new { controller = "Help", action = "Index", apiId = UrlParameter.Optional });
Line 22:
Line 23: HelpPageConfig.Register(GlobalConfiguration.Configuration);
Line 24: }
Line 25: }
The above is found in the help page configuration for Web API help content.
Has anyone had any of such issues?
UPDATE:
This particular issue started the very moment I deleted the bin folder from TFS.

After over 24 hours of tireless research (and worrying) I resolved the issue.
Seeing that I kept on throwing a System.TypeLoadException, I researched around that type and found the several reasons why that exception gets thrown.
In my case it was because the particular version of Newtonsoft.Json.dll it needed wasn't what was being referenced, as it was stubbornly pointing to the wrong package folder that held the version meant for .NET 2.0 and 3.5. After removing those packages, I removed the referenced and added it again from the right package folder, cleaned the solution and built and wa-la it was back up and fine.

Those are not the lines that are causing the exception, you see those lines referenced because the HelpPageConfig.Register(GlobalConfiguration.Configuration) is where the code that causes the exception is run. The actual exception is happening somewhere deeper in your Register method on the HelpPageConfig class.
Most likely though, you have another class that is inheriting from JsonContractResolver that is marked as internal. I can't tell you exactly, obviously, without seeing all of your code. But do a quick search across the files for a class that is inherting from that base class and go from there.

I upgraded Microsoft.AspNet.WebApi to version="5.2.7" from version="4.0.30506.0"and updated newtonsoft to 7.0.1.
It is due to a mix of versions in the dependencies. Thanks for the pointer.

Related

Unknown build error, mapping URI is not valid

I have the following defined in my XAML file:
xmlns:databaseDesign="clr-namespace:My_Namespace;assembly=My Assembly Name
Intellisense picks it up without any issue and I can use autocomplete to grab classes and whatnot from that assembly in my XAML. However, when I go to build, I get the following error:
Error MC1000: Unknown build error,
''clr-namespace:My_Namespace;assembly=My Assembly Name' mapping URI is
not valid. Line 14 Position 14.' (14, 14)
The only thing I can think of that is different from my previous similar projects is that I am referencing an assembly from a private nuget repo rather than directly from the solution.
Using .NET Core 3.1 for both this project and the referenced library.
Any thoughts on what might be going wrong?
Turns out, assembly names with spaces aren't handled, with no indicator that this is the issue. Really, Microsoft?

MongoDB C# driver returns MissingMethodException. Any workaround?

I'm writing a fairly simple console app using .NET Core and trying to attach to a Mongo DB. It appears that the current release has been broken because of changes to the DnsClient (i.e. the actual error is: Method not found: 'DnsClient.IDnsQueryResponse DnsClient.LookupClient.Query(System.String, DnsClient.QueryType)'.). Is anyone aware of a work-around for this problem as the Mongo-ites don't seem in any rush to get a fix out for us poor C# junkies?
It looks like there is a ticket created regarding this issue in mongodb issue tracker
https://jira.mongodb.org/browse/CSHARP-2330
Also, there is a comment inside the ticket with a workarund:
I found the solution of this problem. DnsClient 1.1.1 cause this
error. downgrade to 1.0.7, and try again.
if you see error "Could not load file or assembly 'DnsClient' or one
of its dependencies. Manifest definition does not match the assembly
reference",make sure to check redirect setting in web.config.
newVersion section in "dependentAssembly/bindingRedirect" should be
"1.0.7.0".
Actually, I got this exception as well. Follow the above steps solved my problem.

Autofac couldn't resolve dependency on TeamCity

I have a such code (It's a part of unit tests executed by nunit-console):
class MyClass
{
[DI(Type = typeof(MyClass))]
public IMyClass MyClassProperty {get;set;}
}
By reflection i'm scanning such classes and then register in Autofac:
// Register MyClass as IMyClass
autofacBuilder.RegisterType(diAttribute.Type).As(propertyInfo.PropertyType);
After that i need to resolve this property in the same way - by reflection:
autofacContainer.Resolve(propertyInfo.PropertyType) // it contains IMyClass
When I'm launching this code locally it works well.
But doesn't work on TeamCity. Fails with error:
Error: 'Autofac.Core.DependencyResolutionException: An exception was thrown while executing a resolve operation. See the InnerException for details. ---> Common Language Runtime detected an invalid program. (See inner exception for details.) ---> System.InvalidProgramException: Common Language Runtime detected an invalid program.
There are a lot of questions on StackOverflow about this exception. It's not an Autofac problem, it's a JIT compiler problem. A quick Google search on the exception yields a lot of info.
InvalidProgramException / Common Language Runtime detected an invalid program
Common Language Runtime detected an invalid program?
You even see projects like NewtonSoft.Json running into the issue.
The exception itself means "a program contains invalid Microsoft intermediate language (MSIL) or metadata; generally this indicates a bug in the compiler that generated the program."
Microsoft has a KB article on troubleshooting the issue. Whenever folks have resolved it, as far as I've seen, it's always one of two fixes:
A change to the compilation options. For example, some people running encryption/obfuscation sorts of tools find turning them off helps; others find turning off compiler optimization fixes it. (The KB article has some tips.)
Making sure things are patched to the latest versions. I've run into other similar situations myself where things work great on my machine but not on the build server; or vice versa. Almost invariably it's because I have my machine all patched up but the build server has been languishing unpatched; or someone just patched up the build server and I haven't installed my patches yet.
I'd recommend starting with the patches since that's usually the simplest fix. Use Windows Update to make sure both you and the build server have the latest .NET updates. If that doesn't fix it, check out some of the troubleshooting tips in the articles and questions above.

Intellisense errors in Visual Studio

I've made a modular application and each module is a separate project, they are of similar appearance to MVC areas.
I implemented a custom WebViewPage but even after removing the pointer from web.config back to default the intellisense is still dysfunctional.
On the first line of my razor view:
#model IEnumerable<Module2.Models.TestModel>
I get the warning:
xxx\Module2\Views\Shared\TestModelTable.cshtml: ASP.NET runtime error: The pre-application start initialization method Run on type
WebActivator.ActivationManager threw an exception with the following
error message: Exception has been thrown by the target of an
invocation.. xxx\Module2\Views\Shared\TestModelTable.cshtml
And the error:
The name 'model' does not exist in the current context xxx\Module2\Views\Shared\TestModelTable.cshtml
Then each time I try to reference the model an error appears:
The type arguments for method 'System.Web.Mvc.Html.DisplayExtensions.DisplayFor(System.Web.Mvc.HtmlHelper,
System.Linq.Expressions.Expression>)'
cannot be inferred from the usage. Try specifying the type arguments
explicitly. xxx\Module2\Views\Shared\TestModelTable.cshtml 26 14 Module2
Additionally if I add ViewBag.Title to the view I get this error:
One or more types required to compile a dynamic expression cannot be found. Are you missing a
reference? xxx\Module2\Views\Shared\TestModelTable.cshtml
I would assume there was is a problem with the web configs but they are standard web configs found in mvc areas (I removed the custom WebPageView)
This happens for every view I try to create in each module, however the main application works fine. As a result I attempted to copy the webconfigs from the main application to the module but I still had a lot of errors.
Update 6/12/12
After fixing the implementation of RazorBuildProvider to handle modules the app can now successfully render strongly typed views. However the intellisense still renders these errors. I suspect the first error on the #module... line is the culprit and is preventing the editor from loading the required information for the strongly typed model.
Update 6/12/12
Via a process of elimination I've figured out that ninject's PreApplicationStartMethod is the culprit. When the method calls RegisterServices my moduleloader is called and an error is thrown somewhere in there. [assembly: WebActivator.PreApplicationStartMethod(typeof(Frontline.App_Start.NinjectWebCommon), "Start")]
For a better solution i need to figure out how to debug this error properly, I couldn't find anything so I've posted another (more generic) question on SO Debugging Visual Studio editor exceptions
I'll also post my hacky fix as an answer
So I haven't been able to solve the root of the problem but i have a quick fix.
I'm killing the process during my module loading method if it has visual studio in the name:
if (System.Diagnostics.Process.GetCurrentProcess().ProcessName.Contains("VisualStudio"))
return;

System.IO.FileNotFoundException was unhandled Message=“The specified module could not be found. (Exception from HRESULT: 0x8007007E)”

I'm looking at code that is new-to-me. I have c++ code in a project called UI, with a number of dependencies, which builds correctly and doesn't return any errors. In the same solution in Visual Studio 2008, I have created a new C# windows forms project and added a reference to the dll generated by the UI project, and added a "using" statement. What I want to do is raise one of the dialogs that are defined in the UI project, so I have code like
UIDialog uIDialog = new UIDialog();
uIDialog.Show();
which builds.
When I run my project in debug, when it gets to the "new" part, I get the exception above - with no information as to what the missing module is.
Is there any way to find out what the missing module is, without digging through the code in the UI project?
(I'm trying to rephrase this question so that it doesn't get closed. If someone could give the definitive answer of "No, there isn't" I'd find that very helpful. Thanks also to those who closed the previous version.)
Edit:
System.IO.FileNotFoundException occurred
Message="The specified module could not be found.
(Exception from HRESULT: 0x8007007E)"
Source="ui"
StackTrace:
at ui.UIDialog.Startup()
at ui.UIDialog..ctor() in c:\..\ui\UIDialog.h:line 61
InnerException:
That's the problem, there's no useful information anywhere obvious!
It's not a 32/64 bit problem - before getting here, I had the typical "BadImageFormatException", so to get past that I forced everything to be 32bit.
You could use the MSIL Disassembler on the referenced dll and check the manifest for all the dependencies of the dll. The disassembler is part of the Windows SDK tools.
Or there is always DependencyWalker, though I haven't used it in years.
You can turn on Fusion logging to see what the loader is doing and find out what's missing.

Categories