Posts in category service

WCF inside Windows Service: HTTP could not register URL http://+:8000

Following the  usual-subject kind of MSDN tutorials to make a Windows Service able to communicate with a WPF application (that is have a WCF service hosted inside your Windows Service) I ended with this kind of app.config section:

<services>
      <service name="ConSense.Sensors.Service.ConSenseSensorsWcfService"
               behaviorConfiguration="ConSenseSensorsWcfServiceBehavior">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8000/ConSense/service"/>
          </baseAddresses>
        </host>
        <!-- this endpoint is exposed at the base address provided by host: http://localhost:8000/ConSense/service  -->
        <endpoint address=""
                  binding="wsHttpBinding"
                  contract="ConSense.Sensors.Service.IConSenseSensorsWcfService" />
        <!-- the mex endpoint is explosed at http://localhost:8000/ConSense/service/mex -->
        <endpoint address="mex"
                  binding="mexHttpBinding"
                  contract="IMetadataExchange" />
      </service>
    </services>

In a  UAC environment (Vista/Win7) this leads to the error message shown in the title of this post: HTTP could not register URL http://+:8000 -meaning your LocalService account is not allowed to open such an endpoint.

Maybe due to SOA hype or whatever every tutorial and helpfile around seems to assume you want to build a supercool SOAP Business Service exposed over the internet the moment you set eyes on WCF. This leads to the top 10 Google solutions regarding this exception to suggest you should just run Visual Studio with admin privileges and be done with it. The better answer (at least for my scenario) was to skip the http-stuff and use a named pipe as a WCF endpoint instead:

<services>
      <service name="ConSense.Sensors.Service.ConSenseSensorsWcfService"
               behaviorConfiguration="ConSenseSensorsWcfServiceBehavior">
        <host>
          <baseAddresses>
            <add baseAddress="net.pipe://localhost/ConSenseSensorsService"/>
          </baseAddresses>
        </host>
        <!-- this endpoint is exposed at the base address provided by host: net.pipe://localhost/ConSenseSensorsService  -->
        <endpoint 
                  address="net.pipe://localhost/ConSenseSensorsService"
                  binding="netNamedPipeBinding"
                  bindingConfiguration="netNamedPipeBindingUnsecure"
                  contract="ConSense.Sensors.Service.IConSenseSensorsWcfService" />
        <!-- this mex endpoint is exposed at the base address provided by host: net.pipe://localhost/ConSenseSensorsService/mex -->
        <endpoint address="mex"
                  binding="mexNamedPipeBinding"
                  contract="IMetadataExchange" />
      </service>
    </services>

    <bindings>
      <netNamedPipeBinding>
        <binding name="netNamedPipeBindingUnsecure" >
          <security mode = "None">
          </security>
        </binding >
      </netNamedPipeBinding>
    </bindings>

    <!--For debugging purposes set the 
        includeExceptionDetailInFaults attribute to true-->
    <behaviors>
      <serviceBehaviors>
        <behavior name="ConSenseSensorsWcfServiceBehavior">
          <serviceMetadata />
          <serviceDebug includeExceptionDetailInFaults="True" />
        </behavior>
      </serviceBehaviors>
    </behaviors>

Debugging a Windows Service

This may be blatantly obvious for most developers, but just to save someone the time to figure this out by himself: To attach the debugger to your process put some sleep into the service's Main method so you got enough time to attach before whatever may be causing an exception

System.Threading.Thread.Sleep(10000);

Log4Net in a Windows Service

http://intelligentforms.net/cms/wp-content/themes/iF/imgs/page_laptoppillows_log_red.jpgRequirement: I want a Windows Service

Seems easy, but wut - no logfile created!

The help at  Apache FAQ: Why doesn't the logging in my service work? is fine, but maybe I just skipped over it - it took me a while to figure out that the problem of log4net not showing any results (meaning: writing out a logfile) was related to the windows service not being able to find my application.config file (cause the service was called from a different location than my output dir).

The Apache FAQ suggests using AppDomain.BaseDirectory which unfortunately fails when I want to initialize the logger in the Services Main() method due to Main being static. Copying the app.config to lets say c: and using

log4net.Config.XmlConfigurator.Configure(new FileInfo("c:\\app.config"));

works, but isnt exactly what we want - at least it should use some temporary or application-specific dir in %appdata%


edit: time to bang head against something again - a failure in the configuration file caused the problem.

This line allowed logging to a client-specific directory:

<file type="log4net.Util.PatternString" value="%env{APPDATA}\\ConSense\\Logs\\ConSense.Sensors.Service.ConsoleStart.Log.txt" />

Btw - a very nice introduction to Log4Net: