How to use Prism with Spring.net - prism

I am currently trying to complete this tutorial to get Prism to work with Spring.net.
After referencing Prism4 and Spring.Net through NuGet (or manualy referencing the assemblies), setting up a bootstrapper and running the application I get a "File or Assembly "System, Version=1.0.3300.0, Culture=neutral, ..." FileNotFoundException.
I am succesfully using Prism and Spring.Net in seperate projects. Above exception only occours in a project where Prism AND Spring.net is referenced. Spring.net is not even used in code or app.config. Searching various sites I could not find any informations on version issues or similar problems.
namespace PrismSpringSandbox {
/// <summary>
/// Interaktionslogik für "App.xaml"
/// </summary>
public partial class App : Application {
protected override void OnStartup(StartupEventArgs e) {
base.OnStartup(e);
Bootstrapper bootstrapper = new Bootstrapper();
bootstrapper.Run();
}
}
}
The Exception occours on "bootstrapper.Run()".
namespace HelloWorld {
public class Bootstrapper : UnityBootstrapper {
protected override DependencyObject CreateShell() {
Shell1 shell = new Shell1();
shell.Activate();
RegionManager.UpdateRegions();
shell.Show();
return shell;
}
protected override IModuleCatalog CreateModuleCatalog() {
DirectoryModuleCatalog catalog = new DirectoryModuleCatalog{ModulePath = #".\"};
//ModuleCatalog catalog =
// new ModuleCatalog().AddModule(typeof(HelloWorldModule.HelloWorldModule)).AddModule(
// typeof(SecondaryModule.SecondaryModule));
return catalog;
}
}
}
Maybe someone knows a solution for this problem when trying to use current Prism with current Spring.Net versions.

Ok, got it!
Problem was a reference to unity while referncing spring.net at the same time.

Related

Resolution failed exception in Xamarin IOS prism

I have an iOS application written in Xamarin and I am getting a Unity Exceptions Resolution Failed exception when I try and run the application in iOS. However this error does not happen when I run the android version of the application. The exception is being thrown while the initalize function from prism is taking place.
Here is a snippet from my app.xaml.cs
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
this.RegisterLocal(containerRegistry);
this.RegisterServices(containerRegistry);
this.RegisterPagesForNavigation(containerRegistry);
}
This code all executes and passes.
This is the iOS initialization
Register("AppDelegate")]
public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
{
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
global::Xamarin.Forms.Forms.Init();
PullToRefreshLayoutRenderer.Init();
LoadApplication(new App(new IosInitializer()));
return base.FinishedLaunching(app, options);
}
public class IosInitializer : IPlatformInitializer
{
public void RegisterTypes(IContainerRegistry containerRegistry)
{
containerRegistry.Register<IUAirshipUpdate, UAirshipUpdate>();
}
}
This code also executes
The exception being thrown is an argument null exception indicating that IModuleCatelog is not present. I do not understand why it is looking for that module and can not find it. The source code on GitHub indicates that class was registered.
This issue was caused because linker behavior for IOS application was set to full and that causes issues with Unity IOC Container.

RegionManager not injecting properly

I'm trying to pass our application that was using the old Prism 4.0 to latest Prism 7.1.0.431
I'm almost done, everything compiles. Dependency injection has been updated to use latest Unity. So everything seems back on track as I see injection working somewhat.
Though I still have a problem with Module loading: region manager cannot be resolved. I think I'm missing something in my initialization code but cannot find any relevant documentation on that. Try to get into all Prism.Wpf samples but could find relevant code.
Injecting the region manager within module is probably not a good practice from the code I'm seeing while search an answer to my issue, but bear with me that right now it's a huge application and would want to avoid changing that as much as possible:
Here is the exception error I'm having:
EXCEPTION: Prism.Modularity.ModuleInitializeException: An exception occurred while initializing module 'AdvancedExportModule'.
- The exception message was: Resolution of the dependency failed, type = 'Codex.Modules.AdvancedExport.AdvancedExportModule', name = '(none)'.
Exception occurred while: while resolving.
Exception is: InvalidOperationException - The current type, Prism.Regions.IRegionManager, is an interface and cannot be constructed. Are you missing a type mapping?
Am I missing something initialization code for the RegionManager to be mapped and injected correctly by Unity?
Here are the code sample, I tried to simply the most of it and hopefully it's enough for you to understand what's wrong...
This my App.xaml:
<prism:PrismApplication x:Class="Codex.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:prism="http://prismlibrary.com/">
<Application.Resources>
<ResourceDictionary Source="Resources/Merged.xaml"/>
</Application.Resources>
</prism:PrismApplication>
And in my Code behind App.xaml.cs
namespace MyNamespace
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Threading;
using Prism.Ioc;
using Prism.Logging;
using Prism.Modularity;
using Prism.Unity;
public partial class App : PrismApplication
{
private static ILoggerFacade Logger { get; set; }
public static void Main()
{
var application = new App();
application.InitializeComponent();
application.Run();
}
protected override void OnStartup(StartupEventArgs startupEventArgs)
{
base.OnStartup(startupEventArgs);
}
protected override void ConfigureModuleCatalog(IModuleCatalog moduleCatalog)
{
var modulesFilePaths = new Dictionary<string, string>();
modulesFilePaths.Add("Namespace.Modules.Module1.dll", "Namespace.Modules.AdvancedExport.Module1Module");
var pathToExecutingLibrary = Directory.GetParent(Assembly.GetExecutingAssembly().Location).FullName;
foreach (KeyValuePair<string, string> moduleFilePath in modulesFilePaths)
{
var referenceUri = Path.Combine(pathToExecutingLibrary, moduleFilePath.Key);
var assembly = Assembly.LoadFrom(referenceUri);
var type = assembly.GetType(moduleFilePath.Value);
moduleCatalog.AddModule(
new ModuleInfo(type)
{
ModuleName = type.Name,
Ref = referenceUri,
InitializationMode = InitializationMode.WhenAvailable
});
}
moduleCatalog.Initialize();
}
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
ConfigureViewModelLocator();
var containerExtension = CreateContainerExtension();
containerRegistry.RegisterInstance(containerExtension);
// These methods have been commented out they are use to register all the types of the application.
//RegisterSettings(containerRegistry);
//RegisterServices(containerRegistry);
//RegisterHandlers(containerRegistry);
//RegisterWrappers(containerRegistry);
containerRegistry.RegisterInstance(Dispatcher.CurrentDispatcher);
}
protected override Window CreateShell()
{
Window mainShell = Container.Resolve<MainShell>();
return mainShell;
}
}
}
You're doing too much and the wrong things in your overrides. Example: RegisterTypes should just register types...
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
// this has already been called by the base class: ConfigureViewModelLocator();
// this has also been called by the base class: var containerExtension = CreateContainerExtension();
// containerRegistry.RegisterInstance(containerExtension);
// These methods have been commented out they are use to register all the types of the application.
//RegisterSettings(containerRegistry);
//RegisterServices(containerRegistry);
//RegisterHandlers(containerRegistry);
//RegisterWrappers(containerRegistry);
containerRegistry.RegisterInstance(Dispatcher.CurrentDispatcher);
}
You should review the source code to get an understanding of how the overrides are supposed to be called. Essentially, they should not call each other, just do their own work.

What is typically included in the OnStart or a Xamarin.forms application?

My application uses a database and the connection is currently created with this code when the DataAccess is first referenced:
public static DataAccess DataAccess
{
get
{
if (dataAccess == null)
{
dataAccess = new DataAccess();
}
return dataAccess;
}
}
Would it be more appropriate to put this on the OnStart? I'm just not sure as the current developer did it in the way above.
protected override void OnStart()
{
// Handle when your app starts
}

ASP.NET 5 / MVC 6, How to use session in class library?

With .NET 4.6.1 I wanna use Sessionstate in a class library, something like :
public static void SetUserId()
{
HttpContext.Current.Session["userId"] = 1;
}
but when I call this method it throws an exception "An unhandled exception occurred while processing the request." > "NullReferenceException: Object reference not set to an instance of an object."
I have a reference to system.Web in my class library. In .NET 4 I can do this safe. But it is a problem in .NET 4.6.1.
How can I solve this?
Thanks
This is null by default, the concept driving ASP.NET Core 1.0 is pluggable middleware. You need to explicitly opt-in for session.
You need to ensure that both the Microsoft.Extensions.Caching.Memory and Microsoft.AspNet.Session dependencies exists in the project.json. Then in your
Startup.cs add the following:
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddMvc();
services.AddCaching();
services.AddSession(options => {
options.IdleTimeout = TimeSpan.FromMinutes(30);
options.CookieName = ".MyApplication";
});
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
app.UseSession();
// Removed for brevity...
}
Detailed article here. That should be all you need to do.
Update
Avoid attempting to use HttpContext.Current outside of the web application. This is intended only within the context of the HTTP request / response pipeline. It is preferred to utilize abstractions to achieve the desired interactions you're looking for.
I would need you to share more source code in order to provide an explicit example.

Examples of how to use NServiceBus with NServiceBus.Ninject-CI in ASP.NET MVC 3 solution

I would like to experiment with NServiceBus using ASP.NET MVC 3. I've got a solution with NServiceBus installed, plus NinjectMVC3 and NServiceBus.Ninject-CI. Trouble is, I have no idea how to setup NServiceBus stuff in the NinjectMVC3.cs file in App_Start.
Rather annoyingly I'm having trouble finding any examples of how to use NServiceBus.Ninject-CI (I hate it when people don't bother giving examples of how to use their stuff).
Can someone help me get started please?
Load a module like this into the kernel to provide access to the bus
public class NServiceBusModule : NinjectModule
{
public override void Load()
{
this.Bind<IBus>().ToConstant(this.CreateBus()).InSingletonScope();
}
private IBus CreateBus()
{
return NServiceBus.Configure.WithWeb()
.NinjectBuilder(this.Kernel)
... // put NServiceBus config here
.CreateBus()
.Start();
}
}
Read the NServiceBus documentation about how to configure NServiveBus:
http://docs.particular.net/nservicebus/containers/ninject
http://docs.particular.net/samples/web/asp-mvc-application/
Hopefully this will help someone. I had a lot of trouble finding sample code for getting ninject working within NServiceBus.
This code below works for me in place of the more common Castle version:
public class EndpointConfig : IConfigureThisEndpoint, AsA_Publisher, IWantCustomInitialization
{
#region IWantCustomInitialization Members
public void Init()
{
Configure
.With()
.NinjectBuilder(CreateKernel())
.XmlSerializer()
.MsmqTransport();
SetLoggingLibrary.Log4Net(XmlConfigurator.Configure);
}
protected IKernel CreateKernel()
{
var kernel = new StandardKernel();
kernel.Load<MyCustomNinjectModule>();
return kernel;
}
#endregion
}
with the ninject module being the usual format, ie:
public class MyCustomNinjectModule : NinjectModule
{
public override void Load()
{
Bind(typeof(ILogger<>)).To(typeof(Log4NetLogger<>));
...
}
}

Resources