【问题标题】:Custom ServiceHostFactory fails on Windows Server 2012 and IIS 8.5自定义 ServiceHostFactory 在 Windows Server 2012 和 IIS 8.5 上失败
【发布时间】:2015-06-29 19:11:57
【问题描述】:

我在使用 IIS 版本 8.5.9600.16384 的 Windows 2012 Server R2 Standard 上的 WCF 服务遇到一个非常奇怪的错误。正如您在下面的代码中看到的,我已经实现了一个自定义 ServiceHostFactory。我使用 Visual Studio 2013 Update 4 编译代码并将其发布到我的本地计算机上。 该服务在当地运作良好。但是当我尝试部署相同的代码(只需将所有文件从我的机器复制到服务器)时,我得到一个 HttpCompileException 异常。应用程序池是使用 .NET 版本 v4.0.30319 设置的,托管管道模式在我的机器和服务器中都是“集成的”。 我不明白为什么相同的代码可以在我的机器上运行,而不是在服务器上运行。两者之间的唯一区别是操作系统和 IIS 版本。我的机器运行 Windows 7 和 IIS 7。

MyService.svc 包含以下内容:

<%@ServiceHost
    Language="C#"
    Debug="false"
    Service="MyService"
    Factory="CustomServiceHostFactory"%>

这是我的 CustomServiceHostFactory 实现,它扩展了 UnityServiceHostFactory。

public class CustomServiceHostFactory : UnityServiceHostFactory
  {
      protected override void InitializeLocalDependencies(IUnityContainer container)
      {
          var initializer = new ContainerInitializer();
          initializer.Initialize(container);
      //do stuff
      }
  }

这是我对 UnityServiceHostFactory 的实现,它扩展了 System.ServiceMode.Activation.ServiceHostFactory

public class UnityServiceHostFactory : ServiceHostFactory
   {
       private readonly IUnityContainer _container;

       public UnityServiceHostFactory()
       {
           _container = new UnityContainer(); 
          //do stuff    
       }

这是一个例外:

Exception information: 
    Exception type: HttpCompileException 
    Exception message: The CLR Type 'CustomServiceHostFactory' could not be loaded during service compilation. Verify that this type is either defined in a source file located in the application's \\App_Code directory, contained in a compiled assembly located in the application's \\bin directory, or present in an assembly installed in the Global Assembly Cache. Note that the type name is case-sensitive and that the directories such as \\App_Code and \\bin must be located in the application's root directory and cannot be nested in subdirectories.
   at System.Runtime.AsyncResult.End[TAsyncResult](IAsyncResult result)
   at System.ServiceModel.Activation.HostedHttpRequestAsyncResult.End(IAsyncResult result)
   at System.Web.HttpApplication.AsyncEventExecutionStep.OnAsyncEventCompletion(IAsyncResult ar)
The CLR Type ‘CustomServiceHostFactory' could not be loaded during service compilation. Verify that this type is either defined in a source file located in the application's \\App_Code directory, contained in a compiled assembly located in the application's \\bin directory, or present in an assembly installed in the Global Assembly Cache. Note that the type name is case-sensitive and that the directories such as \\App_Code and \\bin must be located in the application's root directory and cannot be nested in subdirectories.

如果有人能帮助我解决这个问题,我将不胜感激。

提前致谢。

编辑:

您将在下面找到我在 Windows Server 2012 上安装的 .NET 版本和工具的列表。我的项目是使用 .NET Framework 4.5 编译的。

特点:

  • NET Framework 3.5 功能
  • Net Framework 3.5(包括 .NET 2.0 和 3.0)
  • HTTP 激活
  • NET Framework 4.5 功能
  • .Net Framework 4.5 ASP.NET 4.5
  • WCF 服务
  • HTTP 激活
  • 命名管道动作
  • TCP 动作
    -TCP 端口共享

【问题讨论】:

  • 这似乎是一个 .NET 框架问题,因为它说 CLR 类型无法加载,请检查您服务器上的 .NET 版本

标签: c# asp.net wcf visual-studio-2012 iis


【解决方案1】:

解决方案:

  • 在您的项目中添加对 Microsoft.Practices.Unity.dll 的引用。

我的项目缺少对 Microsoft.Practices.Unity.dll 的引用。我的本地机器必须在其他地方有那个 dll 文件,这样服务才能在本地正常工作。但是服务器没有它。所以它的反应是给我一个非常无益的消息,告诉我我缺少参考。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-23
    • 2017-05-11
    相关资源
    最近更新 更多