2014-05-08 100 views
1

目前,我试图创建一个Windows服务应用程序。当我安装调试服务在这篇文章中所指出的,它工作正常:System.AggregateException未处理。消息=发生一个或多个错误。 InnerException:System.Windows.Markup.XamlParseException

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

然后,如果我尝试设置这样的窗口服务:

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: 

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

回答

1

堆栈跟踪显示在您的环境中存在ServiceProcess.HelpersWindows 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); 
     } 
    } 
} 

MyService类实现StartInAttachedDebugger

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

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

+0

参见http://stackoverflow.com/questions/1196531/how-to-debug-the-net-windows-service-onstart-method。 –

+0

谢谢,它提供了另一种运行和调试服务的方式。也就是说,你是否知道为什么会在_servicesToRun.ToArray()。LoadServices()行上抛出异常? – Roger

+0

有趣的是,_servicesToRun.ToArray()。LoadServices()用于工作,然后在某个点发生了某些变化,导致了上述错误。 – Roger

相关问题