1

Presently, I am attempting to create a Windows Service application. When I setup the service to debug as indicated in this article, it works fine:

http://www.codeproject.com/Articles/10153/Debugging-Windows-Services-under-Visual-Studio-NET

Then, if I attempt to setup the windows service in this way:

using System;
using System.Collections.Generic;
using System.Linq;
using ServiceProcess.Helpers;
using System.ServiceProcess;
using System.Text;
using System.IO;

namespace MyNamespace
{
    static class Program
    {
        private static readonly List<ServiceBase> _servicesToRun =
            new List<ServiceBase>();

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main()
        {
            MyService service = new MyService();
            _servicesToRun.Add(service);

            if (Environment.UserInteractive)
            {
                _servicesToRun.ToArray().LoadServices();
            }
            else
            {
                ServiceBase.Run(_servicesToRun.ToArray());
            }
        }
    }
}

Then, I receive the following exception when debugging, on the _servicesToRun.ToArray().LoadServices() line:

System.AggregateException was unhandled
  HResult=-2146233088
  Message=One or more errors occurred.
  Source=mscorlib
  StackTrace:
       at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
       at System.Threading.Tasks.Task.Wait()
       at ServiceProcess.Helpers.ServiceRunner.LoadServices(IEnumerable`1 services)
       at EnvisionWatchdog.Program.Main() in c:\DevProjects\Data  Service\DataService\EnvisionWatchdog\Program.cs:line 26
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: System.Windows.Markup.XamlParseException
       HResult=-2146233087
       Message=Set property 'System.Windows.ResourceDictionary.DeferrableContent' threw an exception.
       Source=PresentationFramework
       LineNumber=0
       LinePosition=0
       StackTrace:
            at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri)
            at System.Windows.Markup.WpfXamlLoader.LoadBaml(XamlReader xamlReader, Boolean skipJournaledProperties, Object rootObject, XamlAccessLevel accessLevel, Uri baseUri)
            at System.Windows.Markup.XamlReader.LoadBaml(Stream stream, ParserContext parserContext, Object parent, Boolean closeStream)
            at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
           at ServiceProcess.Helpers.App.InitializeComponent()
            at ServiceProcess.Helpers.ServiceRunner.<>c__DisplayClass5.<LoadServices>b__1()
            at System.Threading.Tasks.Task.Execute()
       InnerException: System.NotImplementedException
            HResult=-2147467263
            Message=The method or operation is not implemented.
            Source=PresentationFramework
            StackTrace:
                 at System.Windows.Baml2006.Baml2006SchemaContext.ResolveBamlType(BamlType bamlType, Int16 typeId)
                 at System.Windows.Baml2006.Baml2006SchemaContext.GetXamlType(Int16 typeId)
                 at System.Windows.Baml2006.Baml2006Reader.Process_ConstructorParameterType()
                 at System.Windows.Baml2006.Baml2006Reader.Process_OneBamlRecord()
                 at System.Windows.Baml2006.Baml2006Reader.ReadKeys()
                 at System.Windows.ResourceDictionary.SetDeferrableContent(DeferrableContent deferrableContent)
                 at System.Windows.Baml2006.WpfSharedBamlSchemaContext.<Create_BamlProperty_ResourceDictionary_DeferrableContent>b__168(Object target, Object value)
                 at MS.Internal.Xaml.Runtime.ClrObjectRuntime.SetValue(Object inst, XamlMember property, Object value)
            InnerException: 

The odd thing is, the application is a windows service that does not contain any WPF code anywhere. Does anyone have any suggestions? TIA.

user8128167
  • 5,380
  • 6
  • 53
  • 70

1 Answers1

1

The stack trace show the presence of ServiceProcess.Helpers Windows Service Helper in your environment. According to that page, it has a dependency on reactiveui-xaml. That is probably where your WPF-related exceptions originate.

An approach with no external dependencies on NuGet packages is:

static class Program
{
    public static MyService ServiceInstance;

    static Program()
    {
        ServiceInstance = new MyService ();
    }

    static void Main()
    {
        if (System.Diagnostics.Debugger.IsAttached)
        {
            ServiceInstance.StartInAttachedDebugger();
            System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);
        }
        else
        {
            ServiceBase.Run(ServiceInstance);
        }
    }
}

Implement StartInAttachedDebugger in your MyService class.

public void StartInAttachedDebugger()
{
    OnStart(null);
}

Then you can just start debugging from Visual Studio as you expected.

Joel Allison
  • 1,951
  • 1
  • 10
  • 9
  • See also http://stackoverflow.com/questions/1196531/how-to-debug-the-net-windows-service-onstart-method. – Joel Allison May 08 '14 at 16:06
  • Thank you, that provides an alternative way to run and debug the service. That said, do you have any idea why the exception would be thrown on the _servicesToRun.ToArray().LoadServices() line? – user8128167 May 08 '14 at 16:22
  • The funny thing is, _servicesToRun.ToArray().LoadServices() used to work, then something changed at some point which caused the above error. – user8128167 May 08 '14 at 16:25
  • 1
    Yes, `LoadServices` is a method in the Windows Service Helper package. I am not too familiar with that package, but it would appear to rely on a WPF library to present the custom UI to Run, Stop, Pause the service. So in fact the service does contain WPF code. I'm not sure how to resolve that aspect of your question. – Joel Allison May 08 '14 at 16:30