Events from ServiceHost instance ?
-
Hi all, I've been struggling with this for quite a time now - maybe one of you can help me. Imagine this scenario: I have an instance of a "Engine" class, which in turn has a ServiceHost instance, hosting a WCF service of type ProcessingEngine. I want the "Engine" to be notified from the service when it has received a message (or whatever) - but I cant figure out how to emit custom events from the service. Here is some code to illustrate:
/* Host */
public class Engine
{public void Start() { // Create service host m\_processingServiceHost = new ServiceHost(typeof(ProcessingServices.ProcessingService)); // Sign up for events // ??? // Start the service try { m\_processingServiceHost.Open(); } catch (Exception ex) { FireErrorDetected(ex.Message); } }
}
/* Service */
public class ProcessingService : IProcessingService
{
public void SendData(int value)
{
// Send an event
// ???
}
}Any ideas ? Best regards, Jan Hansen
Do you know why it's important to make fast decisions? Because you give yourself more time to correct your mistakes, when you find out that you made the wrong one. Chris Meech on deciding whether to go to his daughters graduation or a Neil Young concert
-
Hi all, I've been struggling with this for quite a time now - maybe one of you can help me. Imagine this scenario: I have an instance of a "Engine" class, which in turn has a ServiceHost instance, hosting a WCF service of type ProcessingEngine. I want the "Engine" to be notified from the service when it has received a message (or whatever) - but I cant figure out how to emit custom events from the service. Here is some code to illustrate:
/* Host */
public class Engine
{public void Start() { // Create service host m\_processingServiceHost = new ServiceHost(typeof(ProcessingServices.ProcessingService)); // Sign up for events // ??? // Start the service try { m\_processingServiceHost.Open(); } catch (Exception ex) { FireErrorDetected(ex.Message); } }
}
/* Service */
public class ProcessingService : IProcessingService
{
public void SendData(int value)
{
// Send an event
// ???
}
}Any ideas ? Best regards, Jan Hansen
Do you know why it's important to make fast decisions? Because you give yourself more time to correct your mistakes, when you find out that you made the wrong one. Chris Meech on deciding whether to go to his daughters graduation or a Neil Young concert
If I understand what you're trying to do correctly, you could add an event to the service...
/* Host */
public class Engine
{
public void Start()
{
// Create service host
m_processingServiceHost = new ServiceHost(typeof(ProcessingServices.ProcessingService));// Sign up for events ProcessingServices.ProcessingService.CustomEvent += SomeMethod; // Start the service try { m\_processingServiceHost.Open(); } catch (Exception ex) { FireErrorDetected(ex.Message); } } private void SomeMethod(object sender, CustomEventArgs e) { // Handle event - this is on the service thread }
}
/* Service */
public class ProcessingService : IProcessingService
{
public static event EventHandler<CustomEventArgs> CustomEvent;public void SendData(int value) { // Send an event if (CustomEvent != null) CustomEvent(null, new CustomEventArgs()); }
}
Note that the event is static because by default the service behavior is per call.
-
If I understand what you're trying to do correctly, you could add an event to the service...
/* Host */
public class Engine
{
public void Start()
{
// Create service host
m_processingServiceHost = new ServiceHost(typeof(ProcessingServices.ProcessingService));// Sign up for events ProcessingServices.ProcessingService.CustomEvent += SomeMethod; // Start the service try { m\_processingServiceHost.Open(); } catch (Exception ex) { FireErrorDetected(ex.Message); } } private void SomeMethod(object sender, CustomEventArgs e) { // Handle event - this is on the service thread }
}
/* Service */
public class ProcessingService : IProcessingService
{
public static event EventHandler<CustomEventArgs> CustomEvent;public void SendData(int value) { // Send an event if (CustomEvent != null) CustomEvent(null, new CustomEventArgs()); }
}
Note that the event is static because by default the service behavior is per call.
Brian, That hit the spot - thank you. So simple, and yet so far away from my mindset. I owe you a :beer:, remind me if we ever meet :-D /Jan
Do you know why it's important to make fast decisions? Because you give yourself more time to correct your mistakes, when you find out that you made the wrong one. Chris Meech on deciding whether to go to his daughters graduation or a Neil Young concert
-
If I understand what you're trying to do correctly, you could add an event to the service...
/* Host */
public class Engine
{
public void Start()
{
// Create service host
m_processingServiceHost = new ServiceHost(typeof(ProcessingServices.ProcessingService));// Sign up for events ProcessingServices.ProcessingService.CustomEvent += SomeMethod; // Start the service try { m\_processingServiceHost.Open(); } catch (Exception ex) { FireErrorDetected(ex.Message); } } private void SomeMethod(object sender, CustomEventArgs e) { // Handle event - this is on the service thread }
}
/* Service */
public class ProcessingService : IProcessingService
{
public static event EventHandler<CustomEventArgs> CustomEvent;public void SendData(int value) { // Send an event if (CustomEvent != null) CustomEvent(null, new CustomEventArgs()); }
}
Note that the event is static because by default the service behavior is per call.
Dear Brian Griggs, Thanks a lot. I was also searching for the same and I got the solution from your answer. I would also like to thank the poster of this question. I have implemented and it is working fine. This is what I need too. :) Once again thanks.
Best Regards, M. J. Jaya Chitra
-
If I understand what you're trying to do correctly, you could add an event to the service...
/* Host */
public class Engine
{
public void Start()
{
// Create service host
m_processingServiceHost = new ServiceHost(typeof(ProcessingServices.ProcessingService));// Sign up for events ProcessingServices.ProcessingService.CustomEvent += SomeMethod; // Start the service try { m\_processingServiceHost.Open(); } catch (Exception ex) { FireErrorDetected(ex.Message); } } private void SomeMethod(object sender, CustomEventArgs e) { // Handle event - this is on the service thread }
}
/* Service */
public class ProcessingService : IProcessingService
{
public static event EventHandler<CustomEventArgs> CustomEvent;public void SendData(int value) { // Send an event if (CustomEvent != null) CustomEvent(null, new CustomEventArgs()); }
}
Note that the event is static because by default the service behavior is per call.
Just what I was looking for. Thanks
-
If I understand what you're trying to do correctly, you could add an event to the service...
/* Host */
public class Engine
{
public void Start()
{
// Create service host
m_processingServiceHost = new ServiceHost(typeof(ProcessingServices.ProcessingService));// Sign up for events ProcessingServices.ProcessingService.CustomEvent += SomeMethod; // Start the service try { m\_processingServiceHost.Open(); } catch (Exception ex) { FireErrorDetected(ex.Message); } } private void SomeMethod(object sender, CustomEventArgs e) { // Handle event - this is on the service thread }
}
/* Service */
public class ProcessingService : IProcessingService
{
public static event EventHandler<CustomEventArgs> CustomEvent;public void SendData(int value) { // Send an event if (CustomEvent != null) CustomEvent(null, new CustomEventArgs()); }
}
Note that the event is static because by default the service behavior is per call.
Thank you both so much.... I was running into the same issue........