Intellisense errors in Visual Studio - c#

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;

Related

How to debug failed calls to ConvertAssemblyToTypeLib()?

The function ConvertAssemblyToTypeLib from assembly System.Runtime.InteropServices is a black box that is giving me a lot of head aches.
In most cases, this call
ITypeLibConverter tlbConv = new TypeLibConverter();
ITypeLib tlb = (ITypeLib)tlbConv.ConvertAssemblyToTypeLib(asm, tlbname, 0, this);
succeeds, but sometimes it throws this exception:
System.Runtime.InteropServices.COMException
HResult=0x80131163
Type library exporter encountered an error while processing 'MyType1.SendSetAsync(#0), MyType1'.
Error: Referenced type is defined in managed component, which is imported from a type library that could
not be loaded (type: 'MyType2.Interop.IComTask'; component:
'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\MyType2.Interop\v4.0_3.4.30000.0__nnnnnnnnnnnnnnnnn\MyType2.Interop.dll').
It's obvious that at least one of my dependent components is not installed/registered correctly.
But after removing everything from the GAC, scrubbing every related entry from the registry, and rebuilding everything from scratch, the above mentioned call succeeds again.
I'm sick and tired of driving the bull dozer through my living room just because a single nail is missing in the wall.
I tried running Assembly Binding Log Viewer while issuing the call to ConvertAssemblyToTypeLib but that did not record anything.
The assembly C:\Windows\Microsoft.Net\assembly\GAC_MSIL\MyType2.Interop\v4.0_3.4.30000.0__nnnnnnnnnnnnnnnnn\MyType2.Interop.dll does exists, I can open it in the object browser of Visual Studio and it looks just fine. Most notably it has the IComTask interface mentioned in the error message.
Any clues how I can pin point the component that causes the error?

Trying to Edit and Continue on a cshtml file is always throwing an exception

When I make any change to a CSHTML file while debugging then reload the page, I get this exception. This used to work, and I'm not aware of any changes that could affect this. Googling this error message hasn't been helpful either. Any direction is very much appreciated.
I have tried all the suggestions in https://stackoverflow.com/a/27672935/3397630 without any luck.
I am currently using Visual Studio Community 2022 and .NET 5, but this same thing was happening with 2019.
An unhandled exception occurred while processing the request.
MissingMethodException: Method not found: 'Void Microsoft.CodeAnalysis.CompilationOptions.set_ReferencesSupersedeLowerVersions_internal_protected_set(Boolean)'.
Microsoft.CodeAnalysis.CSharp.CSharpCompilationOptions.WithReferencesSupersedeLowerVersions(bool value)
TypeInitializationException: The type initializer for 'Microsoft.CodeAnalysis.CSharp.CSharpCompilation' threw an exception.
Microsoft.CodeAnalysis.Razor.CompilationTagHelperFeature.GetDescriptors()
It sounds like you may need to enable runtime compilation of Razor files: https://learn.microsoft.com/en-us/aspnet/core/mvc/views/view-compilation?view=aspnetcore-5.0&tabs=visual-studio
See also: .NET Core 3.0: Razor views don't automatically recompile on change

refactoring namespace in RESTFul WCF causes error at runtime

I create a "WCF Service Application" project.
I make the interface, then class, a [DataMember] decorated object, and Global.asax.cs Global class in the same namespace, parentns.mynamespage.
I run this in VStudio IIS Express and everything is great.
.
Then I realize I want to refactor the namespace to parentns.subnamespace.
That is the only change alone in Imyclass.cs (the object also exists in this file), myClass.cs, and Global.asax.cs recompile and F5-run again in VStudio Imyclass
Now I get:
"the remote server returned an error: (500) Internal Server Error"
Nothing is reported of interest in the Output window except a very generic
"Exception thrown: 'System.Net.WebException' in System.dll"
If I Ctrl-Z everything and rerun everything works again.
What else am I needing to refactor?
How do you troubleshoot this?
Thanks for you help
Sorry. Immediately after posting this question I had the thought to search-and-replace.
I found a reference in the primary global file 'Global.asax' pointing to the old namespace. Updated that and everything works fine.
Thanks.

Troublesome System.TypeLoadException

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.

Expression Blend 4 failing when tries to load Reactive assembly

Maybe someone have same problem
When I try to set data context type of view to desired View Model. The error appears:
Could not load the assembly System.Reactive.Windows.Threading because
the following errors occurred: Method 'Schedule in type
'System.Reactive.Concurrency.DispatcherScheduler' from assembly
*System.Reactive.Windows.Threading, Version=1.0.10621.0, Culture=
neutral, RublicKeyToken=31b13856ad364e35' does not have an
implementation.
Any thoughts? I have looked at disassembled code, and the method has implementation.
Try adding a file reference to System.Reactive.Windows.Threading.dll to your project.
I've had a similar error and that fixed it.

Categories