title: C# Apple Bonjour - how to monitor service records within Windows
body: <p>I'm using Apples Bonjour to pronounce some client information. </p>
  
  <pre><code>class BonjourExample 
  {
     private Bonjour.DNSSDEventManager m_eventManager;;
     private Bonjour.DNSSDService      m_service;
     private Bonjour.DNSSDService      m_browser;
     private Bonjour.DNSSDService      m_resolver;
  
     private Init() 
     {
        m_service      = new DNSSDService();
        m_eventManager = new DNSSDEventManager();
        m_eventManager.ServiceFound        += new _IDNSSDEvents_ServiceFoundEventHandler(this.ServiceFound);
        m_eventManager.ServiceLost         += new _IDNSSDEvents_ServiceLostEventHandler(this.ServiceLost);
        m_eventManager.ServiceResolved     += new _IDNSSDEvents_ServiceResolvedEventHandler(this.ServiceResolved);
        m_eventManager.QueryRecordAnswered += new _IDNSSDEvents_QueryRecordAnsweredEventHandler(this.QueryAnswered);
        m_eventManager.OperationFailed     += new _IDNSSDEvents_OperationFailedEventHandler(this.OperationFailed);
  
       m_bonjourBrowser = m_bonjourService.Browse( 0, 0, "_xyz._tcp", null, m_eventManager );
     }
  
     private void ServiceFound(DNSSDService sref, DNSSDFlags flags, uint ifIndex, String serviceName, String regType, String domain )
     {
        m_resolver = m_service.Resolve( 0, ifIndex, serviceName, regType, domain, m_eventManager ) );
     }
  
     public void ServiceLost( DNSSDService sref, DNSSDFlags flags, uint ifIndex, String serviceName, String regType, String domain ) 
     {
     }
  
     public void ServiceResolved( DNSSDService sref, DNSSDFlags flags, uint ifIndex, String serviceName, String hostName, ushort port, TXTRecord txtRecord ) 
     {
         //... get the information ...
  
         //
         // Stop the resolve to reduce the burden on the network // &lt;- copied from Apples examples
         //
         m_resolver.Stop(); // (AAAA) &lt;- just a merker ;-)
         m_resolver = null;
     }
  }
  </code></pre>
  
  <p>I modiified the source code to keep things simple. </p>
  
  <p>Goal: get noticed as soon as a txtRecord of a service changes.</p>
  
  <p>My actual problem is the (AAAA) line. Without stopping the resolver everything works as wanted. At every TxtRecord change ServiceResolved is called. But "to reduce the burden on the network" the resolver shall be stopped (otherwise our windows event log is flodded by Bonjour errors: resolver > 2 min...)</p>
  
  <p>Thus I took a look at Apples FAQs, because I a continous solution:</p>
  
  <blockquote>
    <p>There are some rare applications that need to keep a Resolve running in order to monitor for TXT record changes. iChat, for example, continuously monitors for changes to a buddy's status message, which is stored in the Bonjour TXT record. If your application requires this type of functionality, starting in Mac OS X 10.4 you can now monitor for TXT records using CFNetServiceMonitor and/or [NSNetService startMonitoring]
    <a href="https://developer.apple.com/library/mac/qa/qa1297/_index.html" rel="nofollow">https://developer.apple.com/library/mac/qa/qa1297/_index.html</a></p>
  </blockquote>
  
  <p>Bad thing that there does not seem to exist a monitoring interface at the C# Bonjour Wrapper.</p>
  
  <blockquote>
    <p>Note: The function DNSServiceQueryRecord will allow you to restrict your query to only TXT records, which is more efficient than using the regular Resolve operations which send a query for SRV, TXT and address records.</p>
  </blockquote>
  
  <p>Okay, let's test the Query function:</p>
  
  <pre><code>   public void ServiceResolved( DNSSDService sref, DNSSDFlags flags, uint ifIndex, String serviceName, String hostName, ushort port, TXTRecord txtRecord ) 
     {
         m_resolver.Stop(); 
         m_resolver = null;
  
         // just to test the query interface:
         m_service.QueryRecord( 0, 0, serviceName, DNSSDRRType.kDNSSDType_TXT, DNSSDRRClass.kDNSSDClass_IN, m_bonjourEventManager ) );
     }
  
     public void QueryRecordAnswered( DNSSDService service, DNSSDFlags flags, uint ifIndex, string fullname, DNSSDRRType rrtype, DNSSDRRClass rrclass, object rdata, uint ttl )
     {
        // DAMM, how to get a TXTRecord-objet of rdata? Using binaryFormatter serializiation fails
  
        var arrBytes = (Byte[])rdata;
        var txt = System.Text.Encoding.ASCII.GetString( bytes); /es, its the record
     }
  </code></pre>
  
  <p>Damm, to good API-description of what's the correct DNSSDRRType, but TXT seems the right one. But how do I get an TXTRecord object out of it?</p>
  
  <p>Any better clue, how to get triggered at a service TxtRecord change or at least how to convert rdata to a vaild TXTRecord object?</p>