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

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.

Related

Cocoapod library and multiples configurations embedding

After googling a lot regarding my need, I finally ask to this awesome community a way to achieve my goal.
I need to create a swift library with cocoapod, and integrate multiple build configurations. I want to create 3 schemes on my project to switch easily environments variables (like target API, log level, and many more).
I really want to do this on library side, not on app-side, since it's for debug & testing purposes, and finally, applications which embed this pod will only use the "Release" build (except us, developers who maintain this library)
I tried opening the _Pods.xcodeproj and doing update in this file (create *.xcconfig files I need, mapped to configurations schemes) but disappear after ran a "pod install".
Not sure at all if you can do that hack on library side. Looks weird.
But the best practice would be:
When initializing and configuring libraries, endpoints etc (all you need to change between schemes), just check which one is used, and pass different parameters
create a file, FE Constants
struct Constants {
static var libraryApiKey: String {
#if DEBUG
return "debugKey"
#else
return "productionKey"
}
And when initializing
Library.initialize(withKey: Constants.libraryApiKey)

ZF2 Album tutorial using stored procedures

Please also suggest which namespace to use. I am new to zend and MVC.
module.php
public function getServiceConfig()
{
// what code here?
}
**Album\Model\Album.php**
{
//what code here?
}
**Album\Model\AlbumTable.php**
{
//what code here?
//do we have to use this class or a different class?
}
I can understand the sudden confusion when starting out with ZF2. However, the manual has really done a good job at helping those who are just starting out. You can find the most up-to-date information here : http://zf2.readthedocs.org/en/latest/user-guide/overview.html
Once you've got the Skeleton Application working I believe some of your confusion will be relieved. If not, come back and ask more specific questions OR you could even join the FreeNode IRC channel at #zftalk.
But to also help answer some of what you've asked here:
getServiceConfig() is where you will interact with the ServiceManager. Try to stay away from closures and work with Factories.
Album.php is sort of like the Hydrator. You don't have to use a Hydrator but for example purposes it was put here. It can make like easier in the long run.
AlbumTable.php is the Database Table you will be interacting with.
The namespace which is used in the tutorial is called Album. The Skeleton application comes packaged with a namespace called Application to start with.
You can manually download ZF2 here: https://packages.zendframework.com/
The latest skeleton application can be found here: https://github.com/zendframework/ZendSkeletonApplication
You may also choose to use Composer to install your entire application which tends to make the installation process much easier for those just starting out, so in your situation I'm going to recommend you use that. You can find instructions on how to use Composer here: http://zf2.readthedocs.org/en/latest/user-guide/skeleton-application.html

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

ASP.NET MVC2 IoC: Looking for an example using CastleWindsor container that works with minimum config

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));

zend framework under document root in subdir

I developed a application with Zend Framework and now I want to be able to place the app in an subdirectory of a Documentroot.
e.g. http://www.example.com/myapp/
I read quite a lot of Docu how this could work, but all in all these solutions don´t fit my needs. Is there a trivial way to do the subdir thing, without adding the concrete path to any file which generates the pages.
There are some examples in the net, where a basePath is set in the application enviroment and so there is a method call bevor each "form" creation which prepends the path before the link.
$form->setAction($this->_request->getBaseUrl() . $this->_helper->url('sign'));
This was from: http://johnmee.com/2008/11/zend-framework-quickstart-tutorial-deploy-to-a-subdirectory-instead-of-web-root/
But this is only works for small examples, I have tons of forms, tons of views and tons of scripts. I can´t belive this (lets call it hack :) ) is the only solution to do this.
Any ideas?
You don't have to do anything special. See my tutorial at http://akrabat.com/Zend-framework-tutorial which is developed entirely within a sub-directory.
As they say on the web page:
I’m told this last issue has been
lodged has a defect and not necessary
from releases “1.7″ and beyond. The
helper->url will henceforth prepend
the baseUrl to its result.
So you should be fine. Do you actually use the $form->setAction() method on every form already? Because if you use it in combination with the url helper, the baseUrl will already be included.