System.AggregateException未处理。消息=发生了一个或多个错误。 InnerException:System.Windows.Markup.XamlParseException

时间:2014-05-08 15:30:06

标签: c# service

目前,我正在尝试创建Windows服务应用程序。当我按照本文所述设置要调试的服务时,它可以正常工作:

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

然后,如果我尝试以这种方式设置Windows服务:

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());
            }
        }
    }
}

然后,在_servicesToRun.ToArray()上调试时收到以下异常.LoadServices()行:

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: 

奇怪的是,该应用程序是一个Windows服务,在任何地方都不包含任何WPF代码。有没有人有什么建议? TIA。

1 个答案:

答案 0 :(得分:1)

堆栈跟踪显示您的环境中存在ServiceProcess.Helpers Windows Service Helper。根据该页面,它依赖于reactiveui-xaml。这可能是您的WPF相关异常的起源。

对NuGet包没有外部依赖性的方法是:

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);
        }
    }
}

StartInAttachedDebugger课程中实施MyService

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

然后您可以按预期从Visual Studio开始调试。