取消

无法加载为扩展“Microsoft.VisualStudio.Diagnostics.ServiceModelSink.Behavior”注册的类型

一天,某用户反馈过来说我们的软件无法运行,我一看异常信息看到了这个:“System.Configuration.ConfigurationErrorsException: 无法加载为扩展“Microsoft.VisualStudio.Diagnostics.ServiceModelSink.Behavior”注册的类型“Microsoft.VisualStudio.Diagnostics.ServiceModelSink.Behavior, Microsoft.VisualStudio.Diagnostics.ServiceModelSink, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”。 (C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config\machine.config line 232)”。


异常

异常的完整堆栈如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
System.Configuration.ConfigurationErrorsException: 无法加载为扩展“Microsoft.VisualStudio.Diagnostics.ServiceModelSink.Behavior”注册的类型“Microsoft.VisualStudio.Diagnostics.ServiceModelSink.Behavior, Microsoft.VisualStudio.Diagnostics.ServiceModelSink, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”。 (C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config\machine.config line 232)
    System.Configuration.BaseConfigurationRecord.EvaluateOne(String[] keys, SectionInput input, Boolean isTrusted, FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentResult)
    System.Configuration.BaseConfigurationRecord.Evaluate(FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentResult, Boolean getLkg, Boolean getRuntimeObject, Object& result, Object& resultRuntimeObject)
    System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, Object& resultRuntimeObject)
    System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, Object& resultRuntimeObject)
    System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, Object& resultRuntimeObject)
    System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, Object& resultRuntimeObject)
    System.Configuration.BaseConfigurationRecord.GetSection(String configKey)
    System.Configuration.ClientConfigurationSystem.System.Configuration.Internal.IInternalConfigSystem.GetSection(String sectionName)
    System.Configuration.ConfigurationManager.GetSection(String sectionName)
    System.ServiceModel.Activation.AspNetEnvironment.UnsafeGetSectionFromConfigurationManager(String sectionPath)
    System.ServiceModel.Activation.AspNetEnvironment.UnsafeGetConfigurationSection(String sectionPath)
    System.ServiceModel.Configuration.ConfigurationHelpers.UnsafeGetAssociatedSection(ContextInformation evalContext, String sectionPath)
    System.ServiceModel.Description.ConfigLoader.LookupCommonBehaviors(ContextInformation context)
    System.ServiceModel.Description.ConfigLoader.LoadServiceDescription(ServiceHostBase host, ServiceDescription description, ServiceElement serviceElement, Action`1 addBaseAddress, Boolean skipHost)
    System.ServiceModel.ServiceHostBase.LoadConfigurationSectionInternal(ConfigLoader configLoader, ServiceDescription description, ServiceElement serviceSection)
    System.ServiceModel.ServiceHostBase.ApplyConfiguration()
    System.ServiceModel.ServiceHost.ApplyConfiguration()
    System.ServiceModel.ServiceHostBase.InitializeDescription(UriSchemeKeyedCollection baseAddresses)
    System.ServiceModel.ServiceHost.InitializeDescription(Type serviceType, UriSchemeKeyedCollection baseAddresses)
    System.ServiceModel.ServiceHost..ctor(Type serviceType, Uri[] baseAddresses)
    Walterlv.DemoLib.IPC.WCF.Duplex.Pipe.Server..ctor(Uri address, String serverId, IClientInfoBuilder clientInfoBuilder)
    Walterlv.DemoApp.IPCLinks.IPCCloudLinkProvider..ctor(String identity, IClientInfoBuilder clientInfoBuilder)
    Walterlv.DemoApp.IPCLinks.IPCLinkProviderFactory.Build(IIPCLinkEnvironment environment, IClientInfoBuilder clientInfoBuilder)
    Walterlv.DemoApp.Tasks.IPCLinkInitializeStartup.RunAsync(IStartupContext context)
    Walterlv.DemoApp.Startup.Core.StartupTask.<>c__DisplayClass0_0.<<JoinAsync>b__0>d.MoveNext()
--- 引发异常的上一位置中堆栈跟踪的末尾 ---
    System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
    System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
    Walterlv.DemoApp.Startup.Core.StartupTask.<JoinAsync>d__0.MoveNext()
--- 引发异常的上一位置中堆栈跟踪的末尾 ---
    System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
    System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
    Walterlv.DemoApp.Startup.Core.StartupTaskWrapper.<>c__DisplayClass36_0.<<ExecuteTask>b__1>d.MoveNext()

初步探索

这个异常消息提示基本已经把表层原因说得很明白了:

1
System.Configuration.ConfigurationErrorsException: 无法加载为扩展“Microsoft.VisualStudio.Diagnostics.ServiceModelSink.Behavior”注册的类型“Microsoft.VisualStudio.Diagnostics.ServiceModelSink.Behavior, Microsoft.VisualStudio.Diagnostics.ServiceModelSink, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”。 (C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config\machine.config line 232)

即“C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config\machine.config”文件的 232 行有一个关于 Microsoft.VisualStudio.Diagnostics.ServiceModelSink.Behavior 注册的类型无法加载。我打开那个文件,看到了相关行:

1
<commonBehaviors><endpointBehaviors><Microsoft.VisualStudio.Diagnostics.ServiceModelSink.Behavior/></endpointBehaviors><serviceBehaviors><Microsoft.VisualStudio.Diagnostics.ServiceModelSink.Behavior/></serviceBehaviors></commonBehaviors></system.serviceModel>

修复方法

将这一行里面的 Microsoft.VisualStudio.Diagnostics.ServiceModelSink.Behavior 部分删除后问题即解决。

也就是说,这一行会变成:

1
<commonBehaviors><endpointBehaviors></endpointBehaviors><serviceBehaviors></serviceBehaviors></commonBehaviors></system.serviceModel>

至于元素开闭不匹配的问题不用关心,放到整个文件中是匹配的。(不知道是什么程序写成这样的格式化乱的 XML 文件。)

记得要以管理员权限保存。如果目标电脑没有好用的编辑器,可将其复制到桌面等低权限的目录下,编辑好再放回去。

额外说明

无需担心删除这一行会造成什么不良影响,因为正常情况下没有装 Visual Studio 的电脑上,这个文件本就不应该有这一行的。(感谢 @kkwpsv 在 Win7/10 虚拟机中的试验。)

至于目标电脑上究竟是为什么会导致没有 Visual Studio 时注册了一个 WCF 的行为扩展,这就不得而知了……(如果你知道,欢迎评论区教教我!)


参考资料

本文会经常更新,请阅读原文: https://blog.walterlv.com/post/wcf-exception-microsoft-visualstudio-diagnostics-servicemodelsink-behavior.html ,以避免陈旧错误知识的误导,同时有更好的阅读体验。

知识共享许可协议

本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可。欢迎转载、使用、重新发布,但务必保留文章署名 吕毅 (包含链接: https://blog.walterlv.com ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请 与我联系 ([email protected])

登录 GitHub 账号进行评论