Event Aggregaotr for ASP.NET MVC - asp.net-mvc-4

I need something like EventAggregator in WPF PRISM but for ASP.NET MVC4.
I have following problem. When some action is done, for example commend is added, I need to send 2 mails and SMS message. Now I would like to take it off comment processing action, and to do it somewhere in the background.
One way I could do it is to subscribe to events in Global.asax, and than to execute list of subscribers for specific event when needed (by creating IEventAggregator, using dependency injection, like in PRISM). But then, it will run in same thread as comment processing action.
Is there any other way, and are background tasks and background workers even possible in ASP.NET MVC4?
NOTE: Currently I can't create separate services (WCF for example), which would do that work, because site is running on shared hosting.

hm, have you hear about signalR (background tasks r possible)? http://www.asp.net/signalr/overview/getting-started/introduction-to-signalr

Related

How to subscribe to Topic in Dapr for .Net Core outside Controllers

I just started with Dapr a few days back and although I am able to publish and subscribe to events in Dapr, the way I am doing so is using the Topic method attribute on an action method within a controller such as this...
And while this works I prefer not to mix integration events with service API. This is the Swagger...
I get that the topic name is long but it's so I can ensure unique topics.
What I'd prefer is to position the Handler outside of any Controller. Something like this..
Is this even possible?
I derived a solution out of the .Net Dapr client routing sample.
For each event I add a MapPost endpoint similar to the following
The RequestDelegator receives the event for a given Topic, it will resolve the handler class from the interface and topic attributes and then invoke it's handle method, passing in the event data from Dapr.
My services follow the CQRS and EventSourcing patterns, as such integration events will rarely be the same shape as the command inputs. In my case, events are typically much lighter than a commands, consisting of mostly related Ids.

How and where do I implement my recurring Hangfire job in the solution structure? Any sample apps?

I have recently started a working on my first project using the ABP Framework
I am implementing it as a module with no UI.
I would like to use Hangfire for recurring tasks, having advantage of the Hangfire UI/Dashboard that comes with it.
There is currently no documentation on Hangfire integration. The documentation page is empty. I understand ABP is relatively new, but I imagine someone has implemented it correctly.
The best I could find so far are Github issues(some of them still open) where users are trying to do the same.
Configuring Hangfire as Background Job Manager #2166
Use Volo.Abp.Hangfire Module ERROR #1313
I also noticed that there are two ABP Nuget packages for Hangfire:
Volo.Abp.HangFire
Volo.Abp.BackgroundJobs.HangFire
So my two main questions are:
By ABP convention, is it correct to implement the Hangfire job in my Name.Space.Domain project?
How do implement it as a recurring job?
In the mean time, I will patch up what I have drawn from the links I have shared and will happily share my solution if it works.
For the first question, it is totally fine to implement the business logic in the domain layer. Hence the Hangfire could handle some business logic.
For the second question, you can add this code in Configure function in start up:
app.UseHangfireDashboard();
app.UseHangfireServer();
RecurringJob.AddOrUpdate<yourjobsclass>(x => x.yourJobFunction, Cron.MinuteInterval(5));
Below are some resources that I think could help:
ASP.NET Core Applications: https://docs.hangfire.io/en/latest/getting-started/aspnet-core-applications.html
ASP.NET Applications: https://docs.hangfire.io/en/latest/getting-started/aspnet-applications.html

Zend Framework 3 singletons

I'm creating a new application in Zend Framework 3 and i have a question about a design pattern
Without entering in much details this application will have several Services, as in, will be connecting to external APIs and even in multiple databases, the workflow is also very complex, a single will action can have multiple flows depending on several external information (wich user logged in, configs, etc).
I know about dependency injections and Zend Framework 3 Service Manager, however i am worried about instanciating sereval services when the flow will actually use only a few of them in certain cases, also we will have services depending on other services aswell, for this, i was thinking about using singletons.
Is singleton really a solution here? I was looking a way to user singletons in Zend Framework 3 and haven't figured out a easy way since i can't find a way to user the Service Manager inside a service, as I can't retrive the instance of the Service Manager outside of the Factory system.
What is an easy way to implement singletons in Zend Framework 3?
Why use singletons?
You don't need to worry about too many services in your service manager since they are started only when you get them from the service manager.
Also don't use the service manager inside another class except a factory. In ZF3 it's removed from the controllers for a reason. One of them is testability. If all services are inject with a factory, you can easily write tests. Also if you read your code next year, you can easily see what dependencies are needed inside a class.
If you find there are too many services being injected inside a class which are not always needed you can:
Use the ProxyManager. This lazy loads a service but doesn't start it until a method is called.
Split the service: Move some parts from a service into a new service. e.g. You don't need to place everything in an UserService. You can also have an UserRegisterService, UserEmailService, UserAuthService and UserNotificationsService.
In stead of ZF3, you can also think about zend-expressive. Without getting into too much detail, it is a lightweight middleware framework. You can use middleware to detect what is needed for a request and route to the required action to process the request. Something like this can probably also done in ZF3 but maybe someone else can explain how to do it there.

Where to put calls to 3rd party APIs in Apigility/ZF2?

I have just completed my first API in Apigility. Right now it is basically a gateway to a database, storing and retrieving multi-page documents uploaded through an app.
Now I want to run some processing on the documents, e.g. process them through a 3rd party API or modify the image quality etc., and return them to the app users.
Where (in which class) do I generally put such logic? My first reflex would be to implement such logic in the Resource-Classes. However I feel that they will become quite messy, obstructing a clear view on the API's interface in the code and creating a dependency on a foreign API. Also, I feel limited because each method corresponds to an API call.
What if there is a certain processing/computing time? Meaning I cannot direct respond the result through a GET request. I thought about running an asynchronous process and send a push notification to the app, once the processing is complete. But again, where in the could would I ideally implement such processing logic?
I would be very happy to receive some architectural advice from someone who is more seasoned in developing APIs. Thank you.
You are able to use the zf-rest resource events to connect listeners with your additional custom logic without polluting your resources.
These events are fired in the RestController class (for example a post.create event here on line 382).
When you use Apigility-Doctrine module you can also use the events triggered in the DoctrineResource class (for example the DoctrineResourceEvent::EVENT_CREATE_POST event here on line 361) to connect your listeners.
You can use a queueing service like ZendQueue or something (a third party module) built on top of ZendQueue for managing that. You can find different ZF2 queueing systems/modules using Google.
By injecting the queueing service into your listener you can simply push your jobs directly into your queue.

WCF Workflow service application and state machine in WF 4.0

I am working a project where i need to create a state machine work flow in 4.0. I have installed .net framework 4 platform updates and i am able to get state machine and states in toolbox to work with.
I have created a WCF workflow service application. I have currently 3 states in this.
State1 which executes a extenal process.
State2 which updates a database.
State3 populates some entity class.
Each state will be having a receiveandsend activity. I need to trigger each states individually from client. Cancreateinstance of each receive activity is made true.
I have to call this from a silverlight application. So i have added the service referance to this workflow in silverlight application.
when i execute the first state, its works fine and external process is executed.
But when i try to execute the next state by calling the next receive activity from client, its showing error as below.
"Operation 'ExecuteExternal|{http://tempuri.org/}IService' on service instance with identifier '4732d197-32c0-4591-87bc-fa0adb7ec43a' cannot be performed at this time. Please ensure that the operations are performed in the correct order and that the binding in use provides ordered delivery guarantees."
I think the WF is not waiting after first state is finished. Can anyone help me to solve this?
Your state machine is not waiting for the message that you are sending. The best way to troubleshoot this is with tracking. I've added a behavior that will help you to Microsoft.Activities. See my blog post Using Workflow Services and Silverlight Together
Working with Correlation handle helped me to solve this issue.
Please find the answer in the below post
http://social.msdn.microsoft.com/Forums/en-US/wfprerelease/thread/6a7d9069-d5a7-4410-b5dd-16325d0d8dc9