代码之家  ›  专栏  ›  技术社区  ›  CodingWithSpike

WCF在创建ServiceHost时为“System.ServiceModel”抛出FileNotFound异常

  •  1
  • CodingWithSpike  · 技术社区  · 17 年前

    我在WCF上遇到了一个非常奇怪的问题。我在一个解决方案中有两个项目,每个项目都启动了一些自托管的WCF服务。

    项目1:

    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

    *** 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.
    

    3 回复  |  直到 17 年前
        1
  •  2
  •   CodingWithSpike    17 年前

    发现我的问题:

    对于这两个项目(让我们称之为MyProj1和MyProj2),我在.dll.config文件中有这一行:

    <serviceAuthorization principalPermissionMode="Custom" serviceAuthorizationManagerType="System.ServiceModel.ServiceAuthorizationManager, System.ServiceModel" />
    

    Proj1运行良好,Proj2失败,出现上述异常。由于某种原因,仅在Proj2中,当尝试解析“System.ServiceModel”时,它只会在我的private\bin文件夹中查找。

    如果我将上述行更改为具有完整的程序集名称:

    <serviceAuthorization principalPermissionMode="Custom" serviceAuthorizationManagerType="System.ServiceModel.ServiceAuthorizationManager, System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
    

    一切又恢复了正常。真奇怪。..我不知道为什么完全相同的配置引用适用于一个项目而不适用于另一个项目,但是嘿。..至少我现在可以回去工作了:)

        2
  •  0
  •   marc_s MisterSmith    17 年前

    几个问题:

    • 至少两个项目都要有目标。NET framework 3.0或更高版本?
    • 您能向我们展示这两个服务实现的接口,以及服务实现类本身的大致轮廓吗?
        3
  •  0
  •   Spence    17 年前

    我会尝试删除并将第二个项目中的引用重新添加到系统中。ServiceModel程序集。也许另一个开发人员使用本地文件而不是GAC版本添加了它,这在您的本地环境中造成了问题?