Using event handler with asp.net MVC

Nanand 1 Reputation point
2021-06-11T01:22:48.527+00:00

Hi,
I am facing a kind of problem, and I would like some help.
I would like to display a real time value (statistic) from a server. There s already a dll to implente to manipulate the server (connection, getting values,...)
The dll is generally:

public Class ServerClass{
// attributs
// constructor
// methods
public event EventHandler Received;
}

I try this code on windows form, and it works perfectly. I get the values instantly.

using dll_name;
private void BtnGet_Click(object sender, RoutedEventArgs e){
// class instantiation
ServerClass c=new ServerClass() // with parameters
// set attributs
c.Received+=OnReceivedResponse;
}

private void OnReceivedResponse(object sender, EventArgs e){
listBox.Items.Add(e.toSteing());
}

Now I want to do the same in asp.net mvc, but I can't get the values.
What I have tried:

// global.asax
public class WebAppli: HttpApplication{
public static ServerClass c;

protected void Application_Start(object sender, EventArgs e){
c= new serverClass() // with params
}
}

// controller
public void Result(){
Response.ContentType="text/stream";
// codes 
WebAppli.c.Received+=(sender,ev)=>
{
// All code inside this function doesn t work
Response.Write("data:{0}\n\n, ev.toString())
Response.flush
}
}

Is there something I should set in global.asax (asp.net life cycle)
Thank you.

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,598 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 74,296 Reputation points
    2021-06-11T17:14:04.397+00:00

    Web applications are request / response. This means a controller can on send data during a request. To send data to the client from the web server outside the request/response you need a persistent connection and a different protocol than http, typically websockets.

    You can code to websockets directly with asp.net core, or use signal/r.

    signalr

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.