Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WCF and WF
  4. Events from ServiceHost instance ?

Events from ServiceHost instance ?

Scheduled Pinned Locked Moved WCF and WF
csharpwcfhostinghelptutorial
6 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jan R Hansen
    wrote on last edited by
    #1

    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

    B 1 Reply Last reply
    0
    • J Jan R Hansen

      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

      B Offline
      B Offline
      Brian Griggs
      wrote on last edited by
      #2

      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.

      J M C G 4 Replies Last reply
      0
      • B Brian Griggs

        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.

        J Offline
        J Offline
        Jan R Hansen
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • B Brian Griggs

          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.

          M Offline
          M Offline
          M J Jaya Chitra
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • B Brian Griggs

            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.

            C Offline
            C Offline
            cpalmisciano
            wrote on last edited by
            #5

            Just what I was looking for. Thanks

            1 Reply Last reply
            0
            • B Brian Griggs

              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.

              G Offline
              G Offline
              gglover3158
              wrote on last edited by
              #6

              Thank you both so much.... I was running into the same issue........

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups