WCF 在创建 ServiceHost 时抛出“System.ServiceModel”的 FileNotFound 异常
-
06-09-2019 - |
题
我在使用 WCF 时遇到了一个非常奇怪的问题。我的解决方案中有 2 个项目,每个项目都启动一些自托管的 WCF 服务。
项目一:
myService = new ServiceHost(typeof(MyService1));
运行良好。但是,当我尝试运行我的第二个项目时,其中包含:
meService = new ServiceHost(typeof(MyOtherService));
我得到例外:
FileNotFoundException
Could not load file or assembly 'System.ServiceModel' or one of its dependencies.
The system cannot find the file specified.":"System.ServiceModel
融合日志:
=== Pre-bind state information ===
LOG: User = removed\removed
LOG: DisplayName = System.ServiceModel
(Partial)
LOG: Appbase = file:///C:/Work/MySln/MyProj/bin/Dev
LOG: Initial PrivatePath = NULL
Calling assembly : System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\Work\MySln\MyProj\bin\Dev\MyProj.dll.temp.config
LOG: Using machine configuration file from c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\config\machine.config.
LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind).
LOG: Attempting download of new URL file:///C:/Work/MySln/MyProj/bin/Dev/System.ServiceModel.DLL.
LOG: Attempting download of new URL file:///C:/Work/MySln/MyProj/bin/Dev/System.ServiceModel/System.ServiceModel.DLL.
LOG: Attempting download of new URL file:///C:/Work/MySln/MyProj/bin/Dev/System.ServiceModel.EXE.
LOG: Attempting download of new URL file:///C:/Work/MySln/MyProj/bin/Dev/System.ServiceModel/System.ServiceModel.EXE.
真正奇怪的部分是,在调试器中,当我收到此异常时,我可以进入 Visual Studio 中的“QuickWatch”并输入:
new MyOtherService() // returns new instance properly
new ServiceHost() // returns new instance properly
new ServiceHost(typeof(MyOtherService)) // throws exception, same as above.
我尝试在调试时运行 SysInternals 文件系统观察器,但除了 devenv.exe 尝试跟踪 .pdb 文件以生成堆栈跟踪之外,它没有显示“FileNotFound”错误。
有人对看什么还有其他想法吗?
更新#1
我检查了该过程的融合日志,似乎发生了一些奇怪的事情。System.ServiceModel 有 2 个连续的日志条目:
*** Assembly Binder Log Entry (6/1/2009 @ 10:26:48 AM) ***
The operation was successful.
Bind result: hr = 0x0. The operation completed successfully.
Assembly manager loaded from: c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\mscorwks.dll
Running under executable C:\Program Files\TestDriven.NET 2.0\ProcessInvocation.exe
--- A detailed error log follows.
=== Pre-bind state information ===
LOG: User = removed\removed
LOG: DisplayName = System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
(Fully-specified)
LOG: Appbase = file:///C:/Work/MySln/PFWebIntgTests/bin/Dev
LOG: Initial PrivatePath = NULL
LOG: Dynamic Base = NULL
LOG: Cache Base = C:\Documents and Settings\removed\Local Settings\Temp\TestDrivenShadowCopy\633794488082894732
LOG: AppName = domain-nunit.addin.dll
Calling assembly : PFWebIntgTests, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\Work\MySln\PFWebIntgTests\bin\Dev\PFWebIntgTests.dll.temp.config
LOG: Using machine configuration file from c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\config\machine.config.
LOG: Post-policy reference: System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
LOG: Found assembly by looking in the GAC.
LOG: Binding succeeds. Returns assembly from C:\WINDOWS\assembly\GAC_MSIL\System.ServiceModel\3.0.0.0__b77a5c561934e089\System.ServiceModel.dll.
LOG: Assembly is loaded in default load context.
然后是第二个条目:
*** Assembly Binder Log Entry (6/1/2009 @ 10:26:52 AM) ***
The operation failed.
Bind result: hr = 0x80070002. The system cannot find the file specified.
Assembly manager loaded from: c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\mscorwks.dll
Running under executable C:\Program Files\TestDriven.NET 2.0\ProcessInvocation.exe
--- A detailed error log follows.
=== Pre-bind state information ===
LOG: User = removed\removed
LOG: DisplayName = System.ServiceModel
(Partial)
LOG: Appbase = file:///C:/Work/MySln/PFWebIntgTests/bin/Dev
LOG: Initial PrivatePath = NULL
LOG: Dynamic Base = NULL
LOG: Cache Base = C:\Documents and Settings\removed\Local Settings\Temp\TestDrivenShadowCopy\633794488082894732
LOG: AppName = domain-nunit.addin.dll
Calling assembly : System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\Work\MySln\PFWebIntgTests\bin\Dev\PFWebIntgTests.dll.temp.config
LOG: Using machine configuration file from c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\config\machine.config.
LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind).
LOG: Attempting download of new URL file:///C:/Work/MySln/PFWebIntgTests/bin/Dev/System.ServiceModel.DLL.
LOG: Attempting download of new URL file:///C:/Work/MySln/PFWebIntgTests/bin/Dev/System.ServiceModel/System.ServiceModel.DLL.
LOG: Attempting download of new URL file:///C:/Work/MySln/PFWebIntgTests/bin/Dev/System.ServiceModel.EXE.
LOG: Attempting download of new URL file:///C:/Work/MySln/PFWebIntgTests/bin/Dev/System.ServiceModel/System.ServiceModel.EXE.
LOG: All probing URLs attempted and failed.
所以看起来它尝试加载 System.ServiceModel 两次,一次是从本地应用程序目录而不是 GAC 加载?我很困惑...
解决方案
<强>发现我的问题:强>
有关这两个项目(允许调用它们MyProj1和MyProj2)我在.dll.config文件这一行:
<serviceAuthorization principalPermissionMode="Custom" serviceAuthorizationManagerType="System.ServiceModel.ServiceAuthorizationManager, System.ServiceModel" />
Proj1运行正常,失败Proj2与上述异常。出于某种原因,在Proj2而已,它试图解决“System.ServiceModel”时,只着眼在我的私人\ bin文件夹。
如果更改上面的行有充分的组件名称:
<serviceAuthorization principalPermissionMode="Custom" serviceAuthorizationManagerType="System.ServiceModel.ServiceAuthorizationManager, System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
一切再次工作。真奇怪......我不知道为什么完全一样的配置参考书一个项目而不是另一个,但嘿......至少我能回到现在的工作:)
其他提示
有几个问题:
- 您的两个项目都至少以 .NET Framework 3.0 或更高版本为目标吗?
- 您能否向我们展示这两个服务实现的接口以及服务实现类本身的大致轮廓?
我想尝试删除并重新添加在第二个项目到System.ServiceModel组件的引用。也许又是一个设备已使用本地文件,而不是造成对当地的环境问题GAC版本中添加呢?