Add Interceptors through the web.config? NHibernate - nhibernate

I can't seem to find an example where someone added an interceptor via web.config - is this possible?
And yes I know about event listeners and will be using them on another project - but I wanted to see if I could get around having to inject the interceptor in code - thank you

I don't think it's supported but you can easily fetch and instantiate interceptors from a custom config section:
NHibernate.Cfg.Configuration cfg = ...
var interceptors = (NameValueCollection) ConfigurationManager.GetSection("nhibernate.interceptors");
foreach (string k in interceptors)
cfg.SetInterceptor((IInterceptor) Activator.CreateInstance(Type.GetType(k)));
web.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="nhibernate.interceptors" type="System.Configuration.NameValueSectionHandler, System" />
</configSections>
<nhibernate.interceptors>
<add key="MyApp.Interceptors.SomeInterceptor, MyApp" value=""/>
<add key="MyApp.Interceptors.AnotherInterceptor, MyApp" value=""/>
</nhibernate.interceptors>
</configuration>

Related

Set default webpage in vb.Net web-project

I have a class api, and a function test in my web-project. To call this function, I type the link http://localhost/api/test in my browser. Now my question is it possible to call a default page, for example index.html, if i just call the class without the function like: http://localhost/api ?
"I doesn't use ASP.Net"
I solved the problem with a redirect with the following code in my web.config file.
<configuration>
<system.webServer>
<httpRedirect enabled="true" exactDestination="true" httpResponseStatus="Found">
<add wildcard="*api" destination="/index.htm" />
<add wildcard="*api/" destination="/index.htm" />
</httpRedirect>
</system.webServer>
</configuration>

Move simple membership profider functionality to class library project

I'm trying to move my DbSet's to a class library project that is going to be used for database operations.
I've been following this tutorial to a Code First / SimpleMembershipProfider project. I've already got the database filled with new tables etc, via the class lib project.
But i am missing the webpages_ tables you can see on this image.
This is my datacontext class:
public class DataContext : DbContext
{
public DataContext() : base("DefaultConnection")
{
}
public DbSet<Orders> Orders { get; set; }
public DbSet<Appointment> Appointment { get; set; }
public DbSet<UserProfile> UserProfile { get; set; }
}
And for every DbSet i created a cs file. I copied the connectionString from the web.config and placed it in the app.config in the class lib project. This is how the app.config file looks like:
<configuration>
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=.;Initial Catalog=aspnet-CodeFirst-test;Integrated Security=SSPI;" providerName="System.Data.SqlClient" />
</connectionStrings>
<system.web>
<roleManager enabled="true" defaultProvider="SimpleRoleProvider">
<providers>
<clear />
<add name="SimpleRoleProvider" type="WebMatrix.WebData.SimpleRoleProvider, WebMatrix.WebData" />
</providers>
</roleManager>
<membership defaultProvider="SimpleMembershipProvider">
<providers>
<clear />
<add name="SimpleMembershipProvider" type="WebMatrix.WebData.SimpleMembershipProvider, WebMatrix.WebData" />
</providers>
</membership>
</system.web>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
</entityFramework>
</configuration>
I'm not sure what to do with the Filters folder (which has the class InitializeSimpleMembershipAttribute) in my webproject.
Can someone tell me how to get the webpages_ created in the database? And how to move websecurity etc to the class lib project?
Thanks in advance!
If you trying to take control of Asp.net Simple Membership Table and or Including Asp.net Simple Membership Tables as Part of Your Entity Framework Model your Project Entity framework, there is a number of steps you need to take. I would explain them step by step but it would take too long so i will just provide you with references.
Including Asp.net Simple Membership Tables as Part of Your Entity Framework Model
Seed Users and Roles with MVC 4, SimpleMembershipProvider, SimpleRoleProvider, Entity Framework 5 CodeFirst, and Custom User Properties
Building Applications with ASP.NET MVC 4. You can use trial version on pluralsight
If you have any specific questions, i would be happy to answer them.

Config NHibernate

I use config file for NHibernate .
I want define more than one session-factrory in same config file .
I do it like this :
`
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="hibernate-configuration"
type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
</configSections>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory name="test1">
all properties
</session-factory>
<session-factory name="test2">
all properties
</session-factory>'
The application throw exception :
nhibernate.cfg.HibernateConfigException : An exception occurred parsing configuration : The element 'hibernate-configuration' has invalid child element 'session-factory'
You can't define two session factories in a single config file (the schema does not allow it, and NHibernate doesn't provide a way to access them anyway)
Use separate files or, better yet, one of the code-based approaches, which are more flexible.
See http://fabiomaulo.blogspot.com/2009/07/nhibernate-fluent-configuration.html and http://fabiomaulo.blogspot.com/2009/07/nhibernate-configuration-through.html

Connecting to multiple databases in Active Record

I'm trying to connect to multiple databases in castle active record (which uses nhibernate.) My config file looks like this:
<configSections>
<section name="activerecord" type="Castle.ActiveRecord.Framework.Config.ActiveRecordSectionHandler, Castle.ActiveRecord" />
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<activerecord>
<config type="Navtrak.Business.Schemas.CommonSchemas.Models.NavtrakOperations.NavtrakOperationsDatabase`1, CommonSchemas">
<add key="hibernate.connection.connection_string" value="myconnstring" />
<add key="hibernate.connection.driver_class" value="NHibernate.Driver.SqlClientDriver" />
<add key="hibernate.dialect" value="NHibernate.Dialect.MsSql2005Dialect" />
<add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider" />
</config>
<config type="Navtrak.Business.Schemas.CommonSchemas.Models.Errors.ErrorsDatabase`1, CommonSchemas">
<add key="hibernate.connection.connection_string" value="Data Source=myconnstring" />
<add key="hibernate.connection.driver_class" value="NHibernate.Driver.SqlClientDriver" />
<add key="hibernate.dialect" value="NHibernate.Dialect.MsSql2005Dialect" />
<add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider" />
</config>
</activerecord>
And then I have a base abstract class for each database like this:
public abstract class NavtrakOperationsDatabase<T> : ActiveRecordBase<T>
{
}
And then each class inherits from this. I'm then initializing active record like this:
ActiveRecordStarter.Initialize(typeof(SimpleModel).Assembly, ActiveRecordSectionHandler.Instance);
Which gives me the error:
Could not find the dialect in the configuration
If I change the activation code to this:
ActiveRecordStarter.Initialize(
ActiveRecordSectionHandler.Instance,
typeof(NavtrakOperationsDatabase<>),
typeof(ErrorsDatabase<>)
);
Then I get the following error:
You have accessed an ActiveRecord class that wasn't properly initialized. There are two possible explanations: that the call to ActiveRecordStarter.Initialize() didn't include Navtrak.Business.Schemas.CommonSchemas.Models.NavtrakOperations.Application class, or that Navtrak.Business.Schemas.CommonSchemas.Models.NavtrakOperations.Application class is not decorated with the [ActiveRecord] attribute.
I obviously don't want to include every single class in the Initialize function.
Any ideas?
In my case - which was the same situation - I have added this to the InitializeAR method:
LoggerProvider.SetLoggersFactory(new NoLoggingLoggerFactory());
It ended up like this:
lock (typeof(SessionManager))
{
if (!initialized)
{
LoggerProvider.SetLoggersFactory(new NoLoggingLoggerFactory());
System.Reflection.Assembly bin = typeof(SimpleModel).Assembly;
IConfigurationSource s = (IConfigurationSource)ConfigurationManager.GetSection("activerecord");
ActiveRecordStarter.Initialize(bin, s);
}
initialized = true;
}
Drop the "hibernate." prefix from all config keys. That prefix was used in NHibernate 1.x

"No message serializer has been configured" error when starting NServiceBus endpoint

My GenericHost hosted service is failing to start with the following message:
2010-05-07 09:13:47,406 [1] FATAL NServiceBus.Host.Internal.GenericHost [(null)] <(null)> - System.InvalidOperationException: No message serializer has been con
figured.
at NServiceBus.Unicast.Transport.Msmq.MsmqTransport.CheckConfiguration() in d:\BuildAgent-02\work\672d81652eaca4e1\src\impl\unicast\NServiceBus.Unicast.Msmq\
MsmqTransport.cs:line 241
at NServiceBus.Unicast.Transport.Msmq.MsmqTransport.Start() in d:\BuildAgent-02\work\672d81652eaca4e1\src\impl\unicast\NServiceBus.Unicast.Msmq\MsmqTransport
.cs:line 211
at NServiceBus.Unicast.UnicastBus.NServiceBus.IStartableBus.Start(Action startupAction) in d:\BuildAgent-02\work\672d81652eaca4e1\src\unicast\NServiceBus.Uni
cast\UnicastBus.cs:line 694
at NServiceBus.Unicast.UnicastBus.NServiceBus.IStartableBus.Start() in d:\BuildAgent-02\work\672d81652eaca4e1\src\unicast\NServiceBus.Unicast\UnicastBus.cs:l
ine 665
at NServiceBus.Host.Internal.GenericHost.Start() in d:\BuildAgent-02\work\672d81652eaca4e1\src\host\NServiceBus.Host\Internal\GenericHost.cs:line 77
My endpoint configuration looks like:
public class ServiceEndpointConfiguration
: IConfigureThisEndpoint, AsA_Publisher, IWantCustomInitialization
{
public void Init()
{
// build out persistence infrastructure
var sessionFactory = Bootstrapper.InitializePersistence();
// configure NServiceBus infrastructure
var container = Bootstrapper.BuildDependencies(sessionFactory);
// set up logging
log4net.Config.XmlConfigurator.Configure();
Configure.With()
.Log4Net()
.UnityBuilder(container)
.XmlSerializer();
}
}
And my app.config looks like:
<configSections>
<section name="MsmqTransportConfig" type="NServiceBus.Config.MsmqTransportConfig, NServiceBus.Core" />
<section name="UnicastBusConfig" type="NServiceBus.Config.UnicastBusConfig, NServiceBus.Core" />
<section name="Logging" type="NServiceBus.Config.Logging, NServiceBus.Core" />
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" requirePermission="false" />
</configSections>
<Logging Threshold="DEBUG" />
<MsmqTransportConfig
InputQueue="NServiceBus.ServiceInput"
ErrorQueue="NServiceBus.Errors"
NumberOfWorkerThreads="1"
MaxRetries="2" />
<UnicastBusConfig
DistributorControlAddress=""
DistributorDataAddress=""
ForwardReceivedMessagesTo="NServiceBus.Auditing">
<MessageEndpointMappings>
<!-- publishers don't need to set this for their own message types -->
</MessageEndpointMappings>
</UnicastBusConfig>
<connectionStrings>
<add name="Db" connectionString="Data Source=..." providerName="System.Data.SqlClient" />
</connectionStrings>
<log4net debug="true">
<root>
<level value="INFO"/>
</root>
<logger name="NHibernate">
<level value="ERROR" />
</logger>
</log4net>
This has worked in the past, but seems to be failing when the generic host starts. My endpoint configuration is below, along with the app.config for the service. What is strange is that in my endpoint configuration, I am specifying to use the XmlSerializer for message serialization. I don't see any other errors in the console output preceding the error message. What am I missing?
Thanks, Steve
It looks like this was my own issue, though I had a hard time figuring that out based on the error message that I received. I have a base class for all of my messages:
public abstract class RequestMessage : IMessage {}
Recently, I created a partner message:
public abstract class ResponseMessage<TRequest>
where TRequest : RequestMessage {}
Apparently, when the host was starting up the IStartableBus, it would hit this generic message type and would not handle it correctly. I only really saw the underlying error when running with the DefaultBuilder, rather than the UnityBuilder that I am using in my project. It had something to do with the generic type constraint. I had assumed that this would work, but sadly it did not.
Regards,
Steve