ASP.NET MVC2 IoC: Looking for an example using CastleWindsor container that works with minimum config - asp.net-mvc-2

I am looking for an example of how to configure an ASP.NET MVC2 project to use CastleWindsor container to do IoC.
I keep running into problems setting it up, and for every problem there seems to be a solution on-line, but in the end I make so many changes and end up with such a verbose setup to get IoC working using CastleWindsor, that I thought it best to ask this question.
I am looking for the minimum configuration required in the Global.asax page, the Web.config, and if required, what other changes and extension classes are required.
I am not looking to inject into actionfilters at this stage, so just the basics. Preferably not using XML files, but doing it in .NET programatically.
Thank you in advance...

This is as basic as it gets:
Start a MVC2 project from VS2010
Download MvcContrib for MVC2 (the one that says "extra binaries")
In your project, add a reference to (all these DLLs are included in MvcContrib):
Castle.Core.dll
Castle.DynamicProxy2.dll
Castle.MicroKernel.dll
Castle.Windsor.dll
MvcContrib.dll
MvcContrib.Castle.dll
In your Application_Start(), add these lines (and whatever namespaces are needed):
var container = new WindsorContainer();
container.RegisterControllers(typeof(HomeController).Assembly);
ControllerBuilder.Current.SetControllerFactory(new WindsorControllerFactory(container));

Related

Read modules from web.config in autofac to change container configuration according to solution configurations

I have created some Autofac modules... Now I want to register some of them in my container using web.config... In my web.config I have written:
<autofac defaultAssembly="Autofac.Example">
<modules>
<module type="DebugModuleTest1"></module>
<module type="DebugModuleTest2"></module>
</modules>
</autofac>
Now I have to build my container. But the autofac documentation is not clear to me. I do not understand what I have to do to read my modules and build the container.
public class MyCustomContainer
{
public void Build(HttpConfiguration config)
{
var builder = new ContainerBuilder();
Microsoft.Extensions.Configuration.ConfigurationBuilder x = new Microsoft.Extensions.Configuration.ConfigurationBuilder();
//var sec = x.AddInMemoryCollection().Build().GetSection("autofac");
// var y = x.AddXmlFile("Web.config");
var y = new ConfigurationBuilder().SetBasePath(AppDomain.CurrentDomain.BaseDirectory);
var z = y.AddXmlFile("Web.Config");
config.DependencyResolver = new AutofacWebApiDependencyResolver(container);
}
}
I am using latest version of Autofac so I do not have the ConfigurationSettingsReader class available.
Can anyone help me please?
EDIT
I had found interesting saving configuration on web.config because in this way I could "change" web.config according to my solution configuration (you know, the classic web.debug.config, web.release.config, etc)...
That could me help to register the correct modules avoiding the use of directives (#if bla bla bla, ...) or simply conditions...
I am already using modules, but I do not think the correct solution is adding a property inside the module to choose the component to resolve according the selected environment where I want to deploy the project..
I just think of this solution reading this example (By the way, Flexibility to Override still refers to ConfigurationSettingsReader. Is it ok?)
In the 4.0 version of configuration you don't store anything in web.config. It's all in separate XML or JSON files. I'd recommend JSON. The documentation outlines that pretty well:
If you were using the app.config or web.config based configuration available before, you will need to migrate your configuration to the new format and update the way you set configuration with your application container.
We actually spent a lot of time trying to document as much as possible, so while there's definitely a lot there try not to "TL;DR" it. If you skip around, you're liable to end up in the "pre 4.0" section thinking that will still work with the 4.0 stuff. It won't. It sounds like from your comment on this other question that you may have missed a few things the first time through.
Spend some time in the quick start section. That section has both C# and JSON code showing how things work. Again, it's easy to skip past that.
If the docs don't show enough examples, look at the unit tests in the Autofac.Configuration repo, especially the folder full of test files that shows both XML and JSON formatted examples we use in testing.
Finally... three tips:
Configuration is not a feature-for-feature replacement for code. If you're looking to do amazing, crazy, logic-based stuff then stick to modules, possibly with some configuration to register the modules.
Be familiar with Autofac and DI terminology. If you're new to DI or Autofac, "components," "services," and other terms will be confusing. The configuration uses these terms, which means you may not get what you're looking at. Spend time with the docs. The getting started page includes an intro to some of the terminology.
Learn about the new Microsoft config system. There is separate doc about that maintained by Microsoft. Their docs explain everything from how to change config based on environment to creating custom config providers. Autofac is standing on the shoulders of config giants - we don't have to build in that flexibility anymore because it comes for free from the new config system.

add scaffolded item wizard cannot resolve data model class

everyone.
I'm trying to create new Scaffolded item for simple ASP.NET Wep API application which should be based on domain object class and DbContext derivative in separate assembly. The assembly is in the solution, target app has a reference to it (and manually created code which invokes the classes from my lib is build up without any errors) and, obviously, the classes I've mentioned have access modifier public.
The problem is wizard for creating new scaffolded item cannot see my model classes. (By the way, when the model classes were in another ASP.NET MVC5 app the wizard worked well.) I'm using Visual Studio 2013 Update5.
How to fix this? Any workaround would be helpful too!
Sorry for disturbing.
the origin of the problem seems to be that I've move my data model classes from ASP.NET MVC app into my class library in wrong way.
I've not just cut/paste them, but copy-paste, cut-paste (confirmed replacement).
this is the only thing which could cause the problem (although I still don't know the details).
Never the less I've started from the beginning again and cut/past the data model files initially - everything seems to work fine now.

IoC, MVC4 Web API & HttpParameterBinding/ParameterBindingAttribute

I'm using ASP.Net MVC 4 RTM Web API. I have a controller action with a parameter that I'd like to populate via custom model binding. To achieve this, I created a class that derives from System.Web.Http.Controllers.HttpParameterBinding that sets the value of this parameter. I then created an attribute class that derives from System.Web.Http.ParameterBindingAttribute which I use to decorate the parameter on my controller action.
This is all working great, my HttpParameterBinding class is populating the action parameter correctly. The problem I have is that my custom parameter binding class has a dependency that I'd like resolved via my IoC container (Unity). Is there a way to override how Web API creates HttpParameterBinding instances so that I can build up my custom binding class dependency from Unity? I was able to do something similar for a filter attribute by creating a custom filter provider that uses Unity's BuildUp method to populate dependencies, however I'm not seeing anything similar for Web API's HttpParameterBindings.
In general: to use IoC / Unity in the Web API you need to set it up seperately.
Try downloading the nuget package Unity.WebApi and see if that helps!
Take a look at this article: Parameter Binding in WebAPI
It walks through a couple different options from Converters to Binders to BinderProviders. It sounds like you may be able to write a custom ModelBinderProvider which knows how to provide your dependency. If that isn't high enough in the chain you can look at replacing the default IActionValueBinder service. It's a DefaultActionValueBinder instance, which you can extend or simply re-implement.
I also highly recommend downloading the WebAPI source code, as it's been an incredible help for these issues as I've run into them. Here's the WebAPI source code. I recommend downloading it so you can open it in VS for easy navigation.
Feel free to check out FlitBit too (It's very modular, don't let the number of packages scare you off)! I'm working on a WebAPI package for supporting FlitBit, specifically FlitBit.IoC and FlitBit.Dto. I'll add an update if I work out my IoC issue, since it's very similar to yours.

FacebookSDK - AllowPartiallyTrustedCallers Problem

I'm using the latest version of the FacebookSDK (4.0.2) (facebooksdk.codeplex.com)
And I noticed the Src code is made up of several projects namely:
1)
Samples -> Facebook.Samples.AspWebsite Project
Samples -> Facebook.Samples.MvcFacebookApp Project
Samples -> Facebook.Samples.MvcWebsite Project
2)
Tests -> Facebook.Tests
Tests -> Facebook.Web.Tests
3)
Facebook Project
Facebook.Web Project
Facebook.Web.Mvc Project
Okay, so I am building a Facebook Canvas .Net 4.0 MVC 2 application and am simply using the Facebook.Samples.MvcFacebookApp Project and replacing and adding classes and files as I see fit for my situation. However I have come to the point where my site will be running on a Microsoft Azure cloud and I want to abstract the data layer (database) into it's Project. So I went ahead and created a new Project in the solution called BusinessData and added an EntityDataModel to it, so now the solution looks like this:
1)
Samples -> Facebook.Samples ...
2)
Tests -> Facebook.Tests ...
3)
Facebook Project
Facebook.Web Project
Facebook.Web.Mvc Project
BusinessData Project
-> Business.edmx
Great so now I add a reference to the BusinessData Project to my Facebook.Samples.MvcFacebookApp Project and try and access my database. However I get this error in my TrackUserAttribute.cs class in the Facebook.Samples.MvcFacebookApp Project:
System.MethodAccessException occurred
Message=Attempt by security transparent method 'Facebook.Samples.MvcFacebookApp.Models.TrackUserAttribute..ctor()' to access security critical method 'BusinessData.BusinessEntities..ctor()' failed.
Assembly 'Facebook.Samples.MvcFacebookApp, Version=4.0.2.0, Culture=neutral, PublicKeyToken=null' is marked with the AllowPartiallyTrustedCallersAttribute, and uses the level 2 security transparency model. Level 2 transparency causes all methods in AllowPartiallyTrustedCallers assemblies to become security transparent by default, which may be the cause of this exception.
Source=Facebook.Samples.MvcFacebookApp
StackTrace:
at Facebook.Samples.MvcFacebookApp.Models.TrackUserAttribute..ctor() in C:\Users\rkara\Desktop\FacebookPazooza\ThuziFacebookSDK\Source\Src\Facebook.Samples.MvcFacebookApp\Models\TrackUserAttribute.cs:line 13
InnerException:
After some uneventful research and not being fully understood about CAS Policy and how that has evolved with ASP.Net 4.0 my understanding is that the GlobalAssemblyInfo.cs file contained within all of the projects of the FacebookSDK solution has something to do with it.
This line of code in the GlobalAssemblyInfo.cs file:
#if !(SILVERLIGHT || TESTS)
[assembly: AllowPartiallyTrustedCallers]
#endif
seems to be the culprit. According to this Microsoft expert:
http://www.simple-talk.com/dotnet/.net-framework/whats-new-in-code-access-security-in-.net-framework-4.0---part-i/
BusinessData has SecurityCritical level privileges. My class TrackUserAttribute.cs only has SecurityTransparent, so therefore cannot does not have privileges to invoke methods in the BusinessData assembly. Is this correct?
I've tried all sorts of things to get this thing to work even removing [assembly: AllowPartiallyTrustedCallers] from the GlobalAssemblyInfo.cs file, but I get another problem with the .edmx file itself saying it can't load the metadata, so I assume it still has something to do with full trust privileges etc.
I am sure there is a simple answer to this and am sure the guys who developed this SDK wrapper would have consider the fact that people will be extending their SDK to suit individual needs.
What is the best approach for me to ensure abstraction of my data and anything else I want to abstract without having to tie it into the projects provided?
For now and to continue with development and not be tied up in this for too long I've just stored my entity model (edmx) into the models folder of the Facebook.Samples.MvcFacebookApp. This works for now but I would much rather store the edmx in its own project.
Cheers
Rob
Ok so after I left that problem and moved onto installing log4net as a event logger for my application I came across this site:
http://it.toolbox.com/blogs/daniel-at-work/using-log4net-in-cnet-26794
Essentially I wanted to convert log4net to asp.net 4.0 rather than 1.0/2.0 that it was originally written in. Then after reading through this guys instructions on how he got the old log4net code to work in 4.0 the aha! moment came. Essentially all he needed to do was to put this attribute into his AssemblyInfo.cs file:
[assembly: AllowPartiallyTrustedCallers]
So I did the same for my BusinessData project and updated the AssemblyInfo.cs file there, and hey presto it worked!
After some deep contemplation I figured that what this did was allow my Facebook.Samples.MvcFacebookApp partial privileges to call a SecurityCritical assembly including those objects contained within it. Thus I could go ahead now and call my edmx (EntityDataModel) in this assembly from the Mvc application.
The second fix and is not fully known why this is necessary but it fixes the problem is to copy the connection string from the App.config file to the Web.config file in the Mvc application. For some reason the edmx seemed to try to read from the web.config instead of the App.config for the connection string. So now I have the same connection string in the App.config and Web.config file.
Meh! it works and I'm happy ... moving on!
:-D

Deploying MVC2 application to IIS7.5 - Ninject asked to provide controllers for content files

I have an application that started life as an MVC (1.0) app in Visual Studio 2008 Sp1 with a bunch of Silverlight 3 projects as part of the site. Nothing fancy at all. Using Ninject for dependency injection (first version 2 beta, now the released version 2 with the MVC extensions).
With the release of .Net 4.0, VS2010, MVC2 etc., we decided to move the application to the newest platform. The conversion wizard in VS2010 apparently took care of everything, with one exception - it didn't change references to mvc1 to now point to mvc2, so I had to do that manually. Of course, this makes me think about other MVC2 things that could be missing from my app, that would be there if I did File -> New Project... But that is not the focus of this question.
When I deploy this application to the IIS 7.5 server (running on Win2008 R2 x64), the application as such works. However, images, scripts and other static content doesn't seem to exist. Of course they are there on disk on the server, but they don't show up in the client web browser.
I am fairly new to IIS, so the only trick I knew is to try to open the web page in a browser on the server, as that could give me more information. And here, finally, we meet our enemy. If I try to go directly to the URL of one of the images (http://server/Content/someimage.jpg for instance), I get the following error in the browser:
The IControllerFactory 'Ninject.Web.Mvc.NinjectControllerFactory' did not return a controller for a controller named 'Content'.
Aha. The web server tries to feed this request to MVC, who with its' default routing setup assumes Content to be a controller, and fails.
How can I get it to treat Content/ and Scripts/ (among others) as non-controllers and just pass through the static content? This of course works with Cassini on my developer machine, but as soon as I deploy, this problem hits.
I am using the last version of Ninject MVC 2 where the IoC tool should pass missing controllers to the base controller factory, but this has apparently not helped. I have also tried to add ignore routes for Content etc., but this apparently has no effect either. I am not even sure I am addressing the problem on the right level.
Does anyone know where to look to get this app going? I have full control of the web server so I can more or less do whatever I want to it, as long as it starts working.
Thanks!
I had a similar problem with StructureMap and favorite.ico what I ended up doing was to add a route to ignore that path.
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("favicon.ico");
Keep in mind that I have absolutely no idea really but another thing that changed is the need for Default.aspx, also if you have any custom pages those would need to mapped. That's the only two problems I had with routing.
routes.RouteExistingFiles = false;
EDIT: I meant that the RouteExistingFiles should be false otherwise I get that exception in MVC2 :)
Turns out this was caused by some account settings - I was unaware of the IIS AppPool\sitename account automatically being created by IIS in Win2008 R2 server. After trying "everything", I came across this information, gave the proper rights, and stuff magically started working.
Pretty hard thing to debug, especially for someone (me) with very limited IIS experience.