question

NandrianinaHarifidy-1537 avatar image
0 Votes"
NandrianinaHarifidy-1537 asked Bruce-SqlWork answered

Using event handler with asp.net MVC

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.



dotnet-aspnet-mvc
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

Bruce-SqlWork avatar image
0 Votes"
Bruce-SqlWork answered

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


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.