Kentico, global events not firing - module

I am trying to create an event on page save, I have followed step by step documentation and created a class in App_Code folder, I can build without any error but event is not triggering.
here is my code
using CMS;
using CMS.DataEngine;
using CMS.DocumentEngine;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
[assembly: RegisterModule(typeof(ElectionEventHandler))]
public class ElectionEventHandler : Module
{
public ElectionEventHandler()
: base("CustomInithahaah")
{
}
protected override void OnInit()
{
base.OnInit();
// Assigns custom handlers to events
DocumentEvents.Update.After += Update_After;
DocumentEvents.Insert.After += Insert_After;
ObjectEvents.Insert.After += Insert_After1;
ObjectEvents.Update.After += Update_After1;
}
private void Update_After1(object sender, ObjectEventArgs e)
{
throw new NotImplementedException();
}
private void Insert_After1(object sender, ObjectEventArgs e)
{
throw new NotImplementedException();
}
private void Insert_After(object sender, DocumentEventArgs e)
{
}
private void Update_After(object sender, DocumentEventArgs e)
{
}
}
here is the location of my class in project.
In Pages module, I have created some pages and on the form tab of the page I am updating some values and expect these events to trigger but nothing is happening.

You're using a web app vs. a web site so technically there is no App_Code directory. I believe all you need to do is build the project and your event should fire.
With a web app, you need to build each time you view the site. With a web site, it will run the JIT compiler and compile that code automatically in the App_Code directory.

Related

How to get first or guardian actor in akka.net?

Edit : Executive summary: Where the bleep is 'Sys' defined? I see it in Akka.net code all over the internet, but my build is not finding it. Who or what do I have to import, use, link, do, bribe or kill?
Should be screamingly easy. Taking first steps in Akka.net, the sample does not build. This was copied from the [Getting Started example][1]
[1]: https://getakka.net/articles/intro/tutorial-1.html . It does not build, because 'Sys' is not defined. This obviously elementary step is nowhere described on their site, and I've given up on tweak-n-try.
Here is all of the code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MyAkka
{
class Program
{
public class PrintMyActorRefActor : UntypedActor
{
protected override void OnReceive(object message)
{
switch (message)
{
case "printit":
IActorRef secondRef = Context.ActorOf(Props.Empty, "second-actor");
Console.WriteLine($"Second: {secondRef}");
break;
}
}
}
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
var firstRef = Sys.ActorOf(Props.Create<PrintMyActorRefActor>(), "first-actor");
Console.WriteLine($"First: {firstRef}");
firstRef.Tell("printit", ActorRefs.NoSender);
Console.ReadKey();
}
}
}
Here is a working version of your code:
using System;
using Akka.Actor;
namespace SysInAkkaNet
{
class Program
{
public class PrintMyActorRefActor : UntypedActor
{
protected override void OnReceive(object message)
{
switch (message)
{
case "printit":
IActorRef secondRef = Context.ActorOf(Props.Empty, "second-actor");
Console.WriteLine($"Second: {secondRef}");
break;
}
}
}
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
using (var actorSystem = ActorSystem.Create("MyActorSystem"))
{
var firstRef = actorSystem.ActorOf(Props.Create<PrintMyActorRefActor>(), "first-actor");
Console.WriteLine($"First: {firstRef}");
firstRef.Tell("printit", ActorRefs.NoSender);
Console.ReadKey();
}
}
}
}
You need to create an actor system to put your actors in. And you need to add a reference to the Akka NuGet package, and a corresponding using Akka.Actor; statement.
I know that the Akka.TestKit has a property Sys, which gives you a reference to the actor system that is created for a given test.
Apart from that, I am not able to answer why the documentation you are referring to shows these "Sys.ActorOf(...)" examples like that (with a capital S), indicating that it is a (possibly built-in) property, so I kind of understand your confusion there.

Registering RoutedEventHandler in UWP template control

I'm having difficulty finding how to register a RoutedEventHandler in UWP. I'm attempting to code a template control that has event properties similar to ContentDialog's:
PrimaryButtonClick="ClickEvent"
Where ClickEvent is defined in the cs file. I'm only just getting the hang of templates, but I believe I want to do something that looks like this:
<Button Content="{TemplateBinding PrimaryButtonText}" Click="{TemplateBinding PrimaryButtonClick}"/>
Currently, all I can find is references to WPF versions of this type of code:
public static readonly RoutedEvent ValueChangedEvent =
EventManager.RegisterRoutedEvent("ValueChanged",
RoutingStrategy.Direct, typeof(RoutedPropertyChangedEventHandler<double>),
typeof(NumericBox));
public event RoutedPropertyChangedEventHandler<double> ValueChanged
{
add { AddHandler(ValueChangedEvent, value); }
remove { RemoveHandler(ValueChangedEvent, value); }
}
private void OnValueChanged(double oldValue, double newValue)
{
RoutedPropertyChangedEventArgs<double> args =
new RoutedPropertyChangedEventArgs<double>(oldValue, newValue);
args.RoutedEvent = NumericBox.ValueChangedEvent;
RaiseEvent(args);
}
But of course the types have changed. Can someone point me in the right direction?
Unfortunately, the concept of RoutedEvent (bubbling, tunneling) is not available in UWP currently. You can just create a classic event however instead:
public event EventHandler PrimaryButtonClick;
protected void InnerButton_Click(object sender, EventArgs e)
{
PrimaryButtonClick?.Invoke( sender, e );
}
Bubbling of events is possible for some predefined events, but it is not yet possible to allow bubbling for custom events in current version of UWP.

Fody MethodDecorator not working

I am trying to create a method decorator using Fody but it gives me the following error:
I have taken specific care to not wrap my IMethodDecorator inside any namespace as has been mentioned in a lot of places online. Following is the sample code I am trying in a console app.
IMethodDecorator
using System;
using System.Reflection;
public interface IMethodDecorator
{
void OnEntry(MethodBase method);
void OnExit(MethodBase method);
void OnException(MethodBase method, Exception exception);
}
MethodDecoratorAttribute
using System;
using System.Diagnostics;
using System.Reflection;
using FODYPOC;
// Atribute should be "registered" by adding as module or assembly custom attribute
[module: MethodDecorator]
namespace FODYPOC
{
// Any attribute which provides OnEntry/OnExit/OnException with proper args
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor | AttributeTargets.Assembly | AttributeTargets.Module)]
public class MethodDecoratorAttribute : Attribute, IMethodDecorator
{
// instance, method and args can be captured here and stored in attribute instance fields
// for future usage in OnEntry/OnExit/OnException
public MethodDecoratorAttribute() { }
public void OnEntry(MethodBase method)
{
Console.WriteLine();
}
public void OnExit(MethodBase method)
{
Console.WriteLine();
}
public void OnException(MethodBase method, Exception exception)
{
Console.WriteLine();
}
}
public class Sample
{
[MethodDecorator]
public void Method()
{
Debug.WriteLine("Your Code");
}
}
}
Can someone point me in the right direction. It looks pretty simple to implement and I know I am making a very silly mistake somewhere.
Apparently the latest version of MethodDecorator.Fody (Version 0.9.0.6 currently) was not working. Downgrading the version to version 0.8.1.1 fixed the issue for me.
After a little more investigation, it appears that the interface method signatures were different in the two versions. So when I had the new package, it was not expecting MethodBase as a parameter and due to not finding anything that matches the interface it expects, it was throwing the error.

How to set custom URL in MVC4

I need in MVC4 in C#...
{id}.example.com or
{id}.example.com/{controller}/{action}
Or
In localhost how can I test it.
I means, can I debug this code in below format ...
{id}.localhost:51782 or
{id}.localhost:51782/{controller}/{action}
Please explain this in full steps.
You need to handle your requests with "virtual" URL, for example {user}.example.com and internally update httpcontext path using httpContext.RewritePath(). Finally you will have two types of URIs.
Virtual: {id}.example.com/{controller}/{action}
Real: {id}/{controller}/{action}
Handling is possible in Application_BeginRequest inside your Global.asax.cs file
protected void Application_BeginRequest(object sender, EventArgs e)
{
var domainHandler = new DomainsHandler(Context);
domainHandler.Handle();
}
And example of DomainHandler:
public class DomainsHandler
{
private readonly HttpContext httpContext;
public DomainsHandler(HttpContext httpContext)
{
this.httpContext = httpContext;
}
public void Handle()
{
httpContext.RewritePath(path);
}
}
Rewriting logic inside Handle() method is up to you.

Configuration which one should I be using? From fluent tutorial or from cookbook 3.0?

I am reading nhibernate cookbook 3.0 and the fluent tutorial and I am kinda confused which one I should be using(cookbook by itself has many different ways)
Fluent Nhibernate tutorial
private static ISessionFactory CreateSessionFactory()
{
return Fluently.Configure()
.Database(
SQLiteConfiguration.Standard
.UsingFile("firstProject.db")
)
.Mappings(m =>
m.FluentMappings.AddFromAssemblyOf<Program>())
.ExposeConfiguration(BuildSchema)
.BuildSessionFactory();
}
private static void BuildSchema(Configuration config)
{
// delete the existing db on each run
if (File.Exists(DbFile))
File.Delete(DbFile);
// this NHibernate tool takes a configuration (with mapping info in)
// and exports a database schema from it
new SchemaExport(config)
.Create(false, true);
}
cookbook 3.0 pg(76) web request
1. In the hibernate-configuration section of web.config, add the current_
session_context_class property with a value of web.
2. If it doesn't exist already, add a new Global application class (Global.asax).
3. In Global.asax, add these using statements.
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Context;
4. Create a static property named SessionFactory.
public static ISessionFactory SessionFactory { get;
private set; }
5. In the Application_Start method, add the following code.
protected void Application_Start(object sender, EventArgs e)
{
log4net.Config.XmlConfigurator.Configure();
var nhConfig = new Configuration().Configure();
SessionFactory = nhConfig.BuildSessionFactory();
}
6. In the Application_BeginRequest method, add the following code.
protected void Application_BeginRequest(object sender, EventArgs e)
{
var session = SessionFactory.OpenSession();
CurrentSessionContext.Bind(session);
}
7. In the Application_EndRequest method, add the following code:
protected void Application_EndRequest(object sender, EventArgs e)
{
var session = CurrentSessionContext.Unbind(SessionFactory);
session.Dispose();
}
Then they just use this to run it.
Guid productId = new Guid(Request["id"]);
Eg.Core.Product product;
var session = Global.SessionFactory.GetCurrentSession();
using (var tran = session.BeginTransaction())
{
product = session.Get<Eg.Core.Product>(productId);
tran.Commit();
}
Page.Title = product.Name;
Label1.Text = product.Name;
Label2.Text = product.Description;
With the fluent tutorial I am also kinda confused where I would typically put that code in an asp.net mvc application. I am trying to use the repository pattern with ninject(DI injection).
So with both ways I am not sure how to make it work with ninject and the repository pattern.
Is either way better for the repository pattern and Di?
Did you try to download the whole solution to run the project? These are just samples of code and you need the whole setup: VS project with Entity classes, mappings, repositories etc.
I would go to http://www.sharparchitecture.net/ and https://github.com/sharparchitecture download their Northwind sample project that has the exact setup that you need. You will have to find the Northwind database and install it on you local machine then modify the NHibernate.config to point to your database.