MvcContrib.FluentHtml.ModelWebViewPage and ASP.NET MVC4 - asp.net-mvc

Just trying to get work mvc4 with mvccontrib
I set base page in web.config (the same way i did in my mvc3 projects)
<pages pageBaseType="MvcContrib.FluentHtml.ModelWebViewPage">
But in run time i get an error:
Server Error in '/' Application.
--------------------------------------------------------------------------------
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: CS0305: Using the generic type 'MvcContrib.FluentHtml.ModelWebViewPage<T>' requires 1 type arguments
Source Error:
Line 32:
Line 33:
Line 34: public class _Page_Views_Home_Index_cshtml : MvcContrib.FluentHtml.ModelWebViewPage {
Line 35:
Line 36: #line hidden
Source File: c:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\17698293\b6166ab6\App_Web_index.cshtml.a8d08dba.e7moq-gi.0.cs Line: 34
May be some one may help with that? Any thoughts?
UPDATE:
Looks like the problem is:
If i set custom base class <pages pageBaseType="MvcContrib.FluentHtml.ModelWebViewPage"> and if view does not have any custom model on it in such case i am getting that error. So in such case i just need to set model in the view to #model dynamic and it would work and than failed again, so i just change basePageType to its default and use MvcContrib.FluentHtml.ModelWebViewPage<dynamic> and MvcContrib.FluentHtml.ModelWebViewPage<MyModel> until it will be fixed in the next MvcContrib/MVC 4 versions.

The issue is MvcContrib.FluentHtml.ModelWebViewPage does not exist, there is only MvcContrib.FluentHtml.ModelWebViewPage<T>. If you don't have a model class, you could do MvcContrib.FluentHtml.ModelWebViewPage<dynamic>.
All that said, I have no idea of MVC Contrib will work with MVC4, typically one needs to at least recompile things against the new MVC version . . . .

Related

error CS1704: DLL loaded twice ? -razormachine

I want to use RazorMachine on my VS2012 MVC 4 .NET webapp, called "Haas2013".
Everything is working, until I hit a line where I actually call RazorMachine.
I keep getting this error:
error CS1704: An assembly with the same simple name 'haas2013, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=null has already been imported.
Try removing one of the
references or sign them to enable side-by-side.
Line 25: RazorMachine rm = new RazorMachine();
-->Error here: Line 26: ITemplate template = rm.ExecuteContent(res.Body);
When looking into Debug/Windows/Symbols, I can see the DLL of my project "haas2013.dll" loaded twice.
haas2013.dll C:\Users\Reinhard\AppData\Local\Temp\Temporary ASP.NET Files\root\6224be98\f18ecea8\assembly\dl3\43249f4b\692c79c0_cd2ace01\haas2013.dll No Yes Symbols loaded. C:\Users\Reinhard\AppData\Local\Temp\Temporary ASP.NET Files\root\6224be98\f18ecea8\assembly\dl3\43249f4b\692c79c0_cd2ace01\haas2013.pdb 29 1.0.0.0 27.03.2013 10:30 05740000-057A0000 [3820] iisexpress.exe: Managed (v4.0.30319)
haas2013.dll E:\Arbeiten\haas2013\haas2013\haas2013\obj\Debug\haas2013.dll No Yes Symbols loaded. E:\Arbeiten\haas2013\haas2013\haas2013\obj\Debug\haas2013.pdb 84 1.0.0.0 27.03.2013 10:30 08F90000-08FF0000 [3820] iisexpress.exe: Managed (v4.0.30319)
Is this the source of the error, or is this the expected behaviour?
I am using IIS-Express.

F# compile code at runtime ends in missing assemblies

I have a question related to the code provided in an answer to this question.
The problem I have is that the three referenced assmeblies (System.dll, FSharp.Core.dll, FSharp.Powerpack.dll) that are passed to CompilerParameters are not found at runtime. The error I get is:
unknown-file(0,0) : error 0: error FS0218: Unable to read assembly
'c:\user s\utente\documents\visual studio
2010\Projects\TrashSolution\TrashSolution\bin\D ebug\FSharp.Core.dll'
How do I tell the compiler to search for these assemblies in the GAC, instead of the project's bin directory? If I open a namespace in the code provided as a string, how do I know which assemblies to add? Where can I get this information?
In the code from the answer you linked, there's a line towards the bottom:
let asm = Reflection.Assembly.LoadFrom(fileinfo.Value.FullName)
If you call Reflection.Load instead and pass it the fully-qualified assembly name, it'll try to load the assembly from the GAC (and a few other places, if the assembly isn't in the GAC).
let asm =
Assembly.Load "SampleAssembly, Version=1.0.2004.0, Culture=neutral, PublicKeyToken=8744b20f8da049e3"
If you don't know the fully-qualified assembly name you have to create an AssemblyName with the simple name of the assembly, then call the Reflection.Load overload which takes an AssemblyName instead of a string.
let asmName = AssemblyName "Your.Assembly.Name"
let asm = Assembly.Load asmName
As far as knowing which assemblies to load -- I don't think there's a simple way to determine that programmatically. The only two solutions I can think of right now:
If you have some knowledge about the code you're being given (as a string), you could parse it with the FSharpCodeProvider and look at which namespaces/modules are opened and which types are used. If you're looking to see if some particular namespace or type is used (i.e., that you would need to include an assembly reference for when compiling the code), you could create a Map (in your .fsx which is doing the compilation) of namespaces and/or type names to assembly names and use it to reference the appropriate assemblies.
You could "brute-force" search the GAC, by using the semi-documented Fusion API to enumerate all of the assemblies installed in the GAC, then using Reflection to examine each assembly and determine if it's one you require. This is likely to be extremely slow, so I'd avoid it at all costs. If you do decide to go this route, you must also use the Assembly.ReflectionOnlyLoad method to load the assemblies! This allows the assemblies to be unloaded after you finish examining them -- if you use normal Reflection the assemblies can't be unloaded and your program will likely crash with an OutOfMemoryException or similar.
EDIT: Turns out that loading the assembly by its simple name succeeds in fsi and not in normal F# code because fsi automatically installs a handler for the AppDomain.AssemblyResolve event. This event is triggered by the CLR when you try to load an assembly and it can't be resolved; the event provides a way for you to "manually" resolve the assembly and/or generate an assembly dynamically and return it.
If you look at the FileNotFoundException raised when you try to run the code in an F# project, you'll see something like this in the Fusion Log property of the exception:
=== Pre-bind state information ===
LOG: User = Jack-Laptop\Jack
LOG: DisplayName = System
(Partial)
WRN: Partial binding information was supplied for an assembly:
WRN: Assembly Name: System | Domain ID: 1
WRN: A partial bind occurs when only part of the assembly display name is provided.
WRN: This might result in the binder loading an incorrect assembly.
WRN: It is recommended to provide a fully specified textual identity for the assembly,
WRN: that consists of the simple name, version, culture, and public key token.
WRN: See whitepaper http://go.microsoft.com/fwlink/?LinkId=109270 for more information and common solutions to this issue.
LOG: Appbase = file:///C:/Users/Jack/Documents/Visual Studio 2010/Projects/StackOverflow1/StackOverflow1/bin/Debug/
LOG: Initial PrivatePath = NULL
Calling assembly : StackOverflow1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null.
===
LOG: This bind starts in default load context.
LOG: No application configuration file found.
LOG: Using host configuration file:
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework64\v4.0.30319\config\machine.config.
LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind).
LOG: Attempting download of new URL file:///C:/Users/Jack/Documents/Visual Studio 2010/Projects/StackOverflow1/StackOverflow1/bin/Debug/System.DLL.
LOG: Attempting download of new URL file:///C:/Users/Jack/Documents/Visual Studio 2010/Projects/StackOverflow1/StackOverflow1/bin/Debug/System/System.DLL.
LOG: Attempting download of new URL file:///C:/Users/Jack/Documents/Visual Studio 2010/Projects/StackOverflow1/StackOverflow1/bin/Debug/System.EXE.
LOG: Attempting download of new URL file:///C:/Users/Jack/Documents/Visual Studio 2010/Projects/StackOverflow1/StackOverflow1/bin/Debug/System/System.EXE.
Looking towards the bottom of that log, you'll see where the CLR searched for the assembly before it gave up.
Here's a simple handler to give you an idea of how to use the AppDomain.AssemblyResolve handler to manually resolve the assembly. (NOTE: The handler needs to be added before the code that attempts to load the assembly!)
System.AppDomain.CurrentDomain.add_AssemblyResolve (
System.ResolveEventHandler (fun _ args ->
let resolvedAssembly =
System.AppDomain.CurrentDomain.GetAssemblies ()
|> Array.tryFind (fun loadedAssembly ->
// If this assembly has the same name as the one we're looking for,
// assume it's correct and load it. NOTE : It may not be the _exact_
// assembly we're looking for -- then you'll need to adjust the critera below.
args.Name = loadedAssembly.FullName
|| args.Name = loadedAssembly.GetName().Name)
// Return null if the assembly couldn't be resolved.
defaultArg resolvedAssembly null))
If you add that code to a new F# console project, followed by the code which uses AssemblyName with Assembly.Load, you should be able to load the System assembly because it's referenced by default in an F# project and it'll be loaded when you run the project. If you try to resolve System.Drawing, it'll fail because our custom event handler can't find the assembly. Obviously, if you need some more complicated assembly-resolving logic, you should build that into the event handler in whatever way makes sense for your application.
Finally, here's a link to the MSDN whitepaper mentioned in the exception message: Best Practices for Assembly Loading. It's worth a read if you get stuck and can't figure out how to resolve the assemblies you need.

Castle Windsor does not work under Medium Trust

When attempting to run an ASP.NET MVC application which uses Castle Windsor, the following error occurs.
Server Error in '/' Application.
Inheritance security rules violated while overriding member:
'Castle.MicroKernel.DefaultKernel.InitializeLifetimeService()'.
Security accessibility of the overriding method must match the
security accessibility of the method being overriden.
Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.
Exception Details: System.TypeLoadException: Inheritance security
rules violated while overriding member:
'Castle.MicroKernel.DefaultKernel.InitializeLifetimeService()'.
Security accessibility of the overriding method must match the
security accessibility of the method being overriden.
Source Error:
An unhandled exception was generated during the execution of the
current web request. Information regarding the origin and location of
the exception can be identified using the exception stack trace below.
Stack Trace:
[TypeLoadException: Inheritance security rules violated while
overriding member:
'Castle.MicroKernel.DefaultKernel.InitializeLifetimeService()'.
Security accessibility of the overriding method must match the
security accessibility of the method being overriden.]
Castle.Windsor.WindsorContainer..ctor() +0
The assemblies Castle.Core.dll and Castle.Windsor.dll have been built off the source code available on github. The source code for the assemblies includes the file SecurityAssemblyInfo.cs which has the following source code:
// Sets up assembly level security settings
#if ! SILVERLIGHT
[assembly: System.Security.AllowPartiallyTrustedCallers]
#if DOTNET40
[assembly: System.Security.SecurityRules(System.Security.SecurityRuleSet.Level2)]
#endif
#endif
I have verified using ILSpy that the System.Security.AllowPartiallyTrustedCallers attribute is declared at the assembly on both files.
The web application is being run with <trust level="Medium" />.
I have been able to get a successful build + run of my mvc application by making the following changes to Castle.Core:
SecurityAssemblyInfo.cs:
Changed to only the following:
[assembly: System.Security.AllowPartiallyTrustedCallers]
[assembly: System.Security.SecurityRules(System.Security.SecurityRuleSet.Level1)]
ProxyGenerator.cs:
Changed three lines in ProxyGenerator::ProxyGenerator(IProxyBuilder):
if (HasSecurityPermission())
{
Logger = new TraceLogger("Castle.DynamicProxy", LoggerLevel.Warn);
}
becomes:
// if (HasSecurityPermission())
// {
// Logger = new TraceLogger("Castle.DynamicProxy", LoggerLevel.Warn);
// }
I have no idea what sort of consequences these might have. It would be nice if someone familiar with the SecurityRuleSets could chime in on the first part, and #Krzysztof Koźmic could advise me on why I had to comment out the trace logger.

ASP.NET MVC Error in non user code, how do I debug it?

Anyone have any idea how I start debugging this?
Below is what the browser is showing when the error occurs.
I've just cut and paste it below.
Seems the error is in an auto generated temporary file.
Server Error in '/' Application.
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: CS1026: ) expected
Source Error:
Line 240: #line default
Line 241: #line hidden
Line 242: #__w.Write("\r\n\r\n <p>\r\n ");
Line 243:
Line 244: #line 15 "K:\Projects\GroupTargets_01\Views\Group\Index.aspx"
Source File: c:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\c52144fb\cf3eebf6\App_Web_index.aspx.1dacda6f.ihdui7sw.0.cs Line: 242
Show Detailed Compiler Output:
Show Complete Compilation Source:
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.1
This is an error in your view page code for K:\Projects\GroupTargets_01\Views\Group\Index.aspx. The code you're seeing in the error message is what those <# ... #> tags get turned into.

asp.net mvc code not getting compiled on server

i have this code in one of my asp.net mvc views:
<%Html.RenderFile(#"C:\Members\newsletters\welcome.html");%>
I have created an extension on the Html class to read in a file. the code looks like this:
public static class HtmlRenderer
{
public static void RenderFile(this HtmlHelper helper_, string path_)
{
var reader = new StreamReader(path_);
var contents = reader.ReadToEnd();
helper_.ViewContext.HttpContext.Response.Write(contents);
}
}
This all works perfectly when i run in visual studio on my desktop but when i ftp these files to the server, i get the following error in the browser:
Compiler Error Message: CS1061: 'System.Web.Mvc.HtmlHelper' does not contain a definition for 'RenderFile' and no extension method 'RenderFile' accepting a first argument of type 'System.Web.Mvc.HtmlHelper' could be found (are you missing a using directive or an assembly reference?)
The HtmlRenderer class is in a namespace with my controllers so there is no other external assembly reference needed.
Does anyone have any idea how this could be happening or what i am doing wrong ?
You need to compile the project and then deploy (xcopy or publish from VS) to the server.
Googled and found something.Does the server has .net 3.5?
Try to publish the web on a local IIS7 or IIS6.( Right click project "Publish" ).
You have a good chance you already get a more specific error during "publish".
If not run the page on your local IIS6 or 7 and see if you get an error.
i tried doing a full refresh (deleting everything on the server and republishing and now everything works fine..
so i am happy that everything is working but still have no clue why it wasn't before.

Resources