c# - Windows Server 2012 和 IIS 8.5 上的自定义 ServiceHostFactory 失败

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

我在使用 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)情况:

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端口共享

最佳答案

解决方法:

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

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

关于c# - Windows Server 2012 和 IIS 8.5 上的自定义 ServiceHostFactory 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29792296/

相关文章:

c# - 如何为 Func 创建最小起订量模拟

C# 泛型约束 : Interface

c# - Seemann 依赖注入(inject), "Three Calls Pattern"与服务定位器反模式

c# - SharePoint 2010 - 使用 SOAP 网络服务

asp.net - 可以从 jQuery 触发 asp.net 验证吗?

c# - 与 WCF 通信的 Android 应用程序

c# - Entity Framework 错误延迟加载

c# - 脚本不在代码后面工作

wcf - 在ASP.Net网站上使用WCF服务的教程?

c# - Azure 服务总线中继 - 启用压缩