CDI Like Events in ASP.NET - asp.net

I am primarily a JEE6 developer. I did like to know if there is a way to implement Fire and forget functionality of CDI events in asp.net. and the event observers get to receive a specific data payload with which to respond to the event
Thank you

Sure, you'd have to have a registration object always available so your listeners can register thir interest. Then that same object will have a fire method, or an event object would call that fire method, then you just iterate through the listeners and call their method. Use interfaces to keep it simple.

Related

Axoniq Event Handler Resuming from offset

I am looking at the AxonIQ framework and have managed to get a test application up and running. But I have a question about how EventHandlers should be treated when using a store that has persistence in the Read Model.
From my (possible naive) understanding. #EventHandler annotated methods in my Projection class get called from the beginning when first launched. This would mechanism seems to assume that the Projection utilises some kind of in volatile store (e.g. an in memory sql like h2) which is re-created from scratch during the application bootup.
However, if the store was persistent in something like Elastic Search, I would want the #EventHandler to resume from its last persisted event instead of from the beginning event.
Is there anyway to control the behaviour of the #EventHandler in this way?
Axon has two types of Event Processors: Subscribing and Tracking.
The Subscribing mode (which was the default up to Axon 3) will handle events in the thread that delivers them. That means you're at "the mercy" of the delivery guarantees of whichever component delivers the events.
The Tracking mode (which is the default since Axon 4 when using an Event Store or otherwise a source that supports it) will have events handled in dedicated threads, managed by the Event Processor itself. That means events are handled asynchronously from the actual publication mechanism.
The Tracking Event Processor uses Tokens to keep track of progress. These Tokens are stored in a TokenStore and updates as the Processor has correctly processed each incoming event (possibly batched). You decide where those tokens are stored. If you update a relational database, we recommend storing the tokens in the same database, so that event changes and tokens are updated atomically.
If you don't specify any TokenStore, it depends on whether you're on Spring Boot, in which case Axon will attempt to detect a suitable TokenStore implementation for you. Otherwise, it may very well just be an in-memory TokenStore, which causes Processors to re-initialize on every startup (and possibly start from the beginning).
To configure a TokenStore
On Spring (Boot), simply add a bean of type TokenStore with the implementation you want to use
When using Axon's Configuration API, on the EventProcessingConfigurer, use one of the registerTokenStore(...) methods.
When the Tracking Processor starts, it will check the Token Store for previous progress, and continue from there automatically.

Symfony2 Handler vs Listener what the difference?

I need to create listener or handler to provide some actions for AccessDenied exception.
I've looked some sources of standard handlers and listeners, so looks like they can be use for same task.
I also read this:
What's the difference between Event Listeners & Handlers in Java?
JavaScript - What's the difference between event handlers & listener?
Using Symfony2's AccessDeniedHandlerInterface
But only difference I've seen - that handlers often use for handle exceptions.
So, what the real difference between handler and listener in Symfony ?
Listeners are registered and called when an event occurs. Observer or PubSub patterns are used.
The Handler is more of a strategy pattern that delegates implementation details to class. Which can then be substituted. (composition over inheritance)
The patterns could maybe be interchangeable in some cases, and it's more the intention of the code that calls for one or the other then.

MVC 3/4 HttpModule or ActionFilter

I need to check some stuff (Cookies) for each request coming to my application.
In ASP.NET we've used HttpModule for this task , the question what should be used in MVC ? Some Global Filter , or I can Use HttpModuler as well, is there Any difference in Request PipeLine between MVC and regular ASP.NET ?
MVC is an abstraction over ASP.NET and therefore their "hooks" really depend at which level you want to inject your logic. An action filter will allow you to hook into MVC specific events:
OnActionExecuting – This method is called before a controller action is executed.
OnActionExecuted – This method is called after a controller action is executed.
OnResultExecuting – This method is called before a controller action result is executed.
OnResultExecuted – This method is called after a controller action result is executed.
Whereas an HttpModule only allows you to hook into ASP.NET (upon which MVC is built) specific events:
BeginRequest - Request has been started. If you need to do something at the beginning of a request (for example, display advertisement banners at the top of each page), synchronize this event.
AuthenticateRequest - If you want to plug in your own custom authentication scheme (for example, look up a user against a database to validate the password), build a module that synchronizes this event and authenticates the user in a way that you want to.
AuthorizeRequest - This event is used internally to implement authorization mechanisms (for example, to store your access control lists (ACLs) in a database rather than in the file system). Although you can override this event, there are not many good reasons to do so.
PreRequestHandlerExecute - This event occurs before the HTTP handler is executed.
PostRequestHandlerExecute - This event occurs after the HTTP handler is executed.
EndRequest - Request has been completed. You may want to build a debugging module that gathers information throughout the request and then writes the information to the page.
So it really depends on when you need to hook in your event and which events you need.
If the HttpModule worked well for you before then it will continue to with Mvc.
The other parts of your question are quite broad in scope and think you'd be as well reading a good article on asp.net-mvc pipeline and extensibility.
I've done similar things using a global action filter. It works quite well, and keeps your code integrated within your application.
An HTTP module works as well, of course, but this will mean seperating the code from your main application and maintaining it seperately. Unless your code spans multiple sites or is used in multiple applications, or needs to work with web forms sites, then I would use a global filter.

ASP.NET MVC Controllers/Views invoked

When I invoke a controller, it's possible that the view response calls other controllers. Is there a way to determine, in MVC, all of the views and/or controllers were called in a single response?
Thanks.
I don't think that the framework exposes this directly, but you could hook into the OnActionExecuted method and log each action that gets invoked along with a unique request identifier. You could hook into Application_BeginRequest in global.asax.cs to generate a GUID to use as the unique id for that request (stored in the Session, but overwritten for each new request). If you use a base controller and derive all your controllers from it, you could put the logging in the base controllers OnActionExecuted method to keep it DRY.
Alternatively, you could look at creating a custom ActionInvoker and put the logging there.
This is where I would start anyway, though, there might be a better way.
NOTE: This will only tie together actions that are invoked server-side for the request. Any AJAX requests kicked off client-side when client receives the rendered view will show up as different requests. If you need to include those as well, your unique id generation code should probably only run on non-AJAX requests, leaving the existing id alone in the session otherwise. Typically AJAX requests have an HTTP_X_Requested_With header to distinguish them.

Handling Login.Authenticate event

A) Book I’m learning from says that if we handle Login.Authenticate event, then we have to authenticate users on our own. Thus control won’t automatically validate username and password. I thought book suggested this would only happen if we override Login.OnAuthenticate() method, but it appears that even if only add an event handler for Authenticate event, the automatic authentication doesn’t happen.
But why is that? Why doesn’t event handling work like it does with Init or Load events, where you essentially have to override Page.OnInit() and Page.OnLoad() in order gain control over event handling?
B) I checked MSDN site and it basically recommends that if we do override Login.OnAuthenticate(), we should also call base.OnAuthenticate(). But then, why would we ever need to override Login.OnAuthenticate(), if we get the same effect with simply declaring an event handler for Login.Authenticate?
thanx
You should not override OnAuthenticate. This method is only used internally to either raise the Authenticate event to registered handlers if there are any or authenticate via the current MembershipProvider if there are no handlers registered. So, to implement custom authentication for the Login control, you simply register a handler for the Authenticate event and set the AuthenticateEventArgs.Authenticated property.
Event handling for the Authenticate event works exactly the same as for other events. The only difference is that the OnAuthenticate method has some logic that determines whether to use a MembershipProvider or a registered event handler for authentication.
If you do create a subclass of Login and override Login.OnAuthenticate, it is wise to call base.OnAuthenticate(...) because it contains the logic that calls the registered event handlers. If you do not call base.Authenticate(...), you should call the registered event handlers yourself. But creating a subclass of Login is probably not necessary in your situation.

Resources