.net - 为什么找不到我的自定义 WCF 行为扩展元素类型?

标签 .net wcf exception wcf-extensions

我有一个包含两个项目的解决方案。一个项目是 ASP.NET Web 应用程序项目,另一个项目是类库。 Web 应用程序具有对类库的项目引用。这些都不是强名称。

在我称之为“Framework”的类库中,我有一个端点行为(IEndpointBehavior 实现)和一个配置元素(从BehaviorExtensionsElement 派生的类)。配置元素使我可以通过配置将端点行为附加到服务。

在 Web 应用程序中,我有一个支持 AJAX 的 WCF 服务。在 web.config 中,我将 AJAX 服务配置为使用我的自定义行为。配置的 system.serviceModel 部分非常标准,如下所示:

<system.serviceModel>
 <behaviors>
  <endpointBehaviors>
   <behavior name="MyEndpointBehavior">
    <enableWebScript />
    <customEndpointBehavior />
   </behavior>
  </endpointBehaviors>
 </behaviors>
 <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
 <services>
 <service name="WebSite.AjaxService">
  <endpoint
           address=""
           behaviorConfiguration="MyEndpointBehavior"
           binding="webHttpBinding"
           contract="WebSite.AjaxService" />
  </service>
 </services>
 <extensions>
  <behaviorExtensions>
   <add
       name="customEndpointBehavior"
       type="Framework.MyBehaviorExtensionsElement, Framework, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
  </behaviorExtensions>
 </extensions>
</system.serviceModel>

在运行时,这工作得很好。支持 AJAX 的 WCF 服务正确使用我的自定义配置端点行为。

问题是当我尝试添加新的 AJAX WCF 服务时。如果我执行“添加”->“新项目...”并选择“启用 AJAX 的 WCF 服务”,我可以看到它添加 .svc 文件和代码隐藏,但是当它更新 web.config 文件时,我收到此错误:

The configuration file is not a valid configuration file for WCF Service Library.

The type 'Framework.MyBehaviorExtensionsElement, Framework, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' registered for extension 'customEndpointBehavior' could not be loaded.

显然,该配置是完全有效的,因为它在运行时完美运行。如果我暂时从行为配置中删除该元素,然后添加支持 AJAX 的 WCF 服务,一切都会顺利进行。

不幸的是,在一个较大的项目中,我们将拥有具有各种配置的多个服务,暂时删除所有自定义行为很容易出错。虽然我意识到我可以不使用向导并手动完成所有操作,但不是每个人都可以,而且如果能够按照产品的预期使用该产品(向导等等),那就太好了。

为什么找不到我的自定义 WCF 行为扩展元素类型?

更新/说明:

  • 它确实可以在运行时运行,但不能在设计时运行。
  • 当我尝试添加服务时,框架程序集位于 Web 项目的 bin 文件夹中。
  • 虽然我可以手动添加服务(“无需配置”),但我需要开箱即用的项目模板才能工作 - 这就是问题的全部目标。
  • 此问题在 Visual Studio 2008 中出现。在 VS 2010 中,此问题似乎已得到解决。

I filed this issue on Microsoft Connect事实证明,您要么必须将自定义配置元素放入 GAC 中,要么将其放入 IDE 文件夹中。他们不会修复它,至少现在是这样。我已经发布了他们提供的解决方法作为此问题的“答案”。

最佳答案

the workaround Microsoft 在 the Connect issue 上发布我已提交此申请,这是一个已知问题,至少在当前版本中不会有任何解决方案:

The reason for failing to add a new service item: When adding a new item and updating the configuration file, the system will try to load configuration file, so it will try to search and load the assembly of the cusom extension in this config file. Only in the cases that the assembly is GACed or is located in the same path as vs exe (Program Files\Microsoft Visual Studio 9.0\Common7\IDE), the system can find it. Otherwise, the error dialog will pop up and "add a new item" will fail.

I understand your pain points. Unfortunately we cannot take this change in current release. We will investigate it in later releases and try to provide a better solution then,such as providing a browse dialog to enable customers to specify the path, or better error message to indicate some work around solution, etc...

Can you try the work around in current stage: GAC your custom extension assembly or copy it to "Program Files\Microsoft Visual Studio 9.0\Common7\IDE"?

We will provide the readme to help other customers who may run into the same issue.

不幸的是,看来我在这件事上运气不佳。

关于.net - 为什么找不到我的自定义 WCF 行为扩展元素类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/169342/

相关文章:

.net - 根据另一个可选功能更改属于强制 WiX 功能的 web.config

jquery - Google AdSense 和 AdBlock

wcf - Delphi 客户端使用的 WCF 中生成的 WSDL

java - 如何解决 'Define and throw a dedicated exception instead of using a generic one.'

c# - Web API 将自定义异常对象转换为基本异常

java - 线程 "main"java.lang.OutOfMemoryError 中的异常

c# - 如何从 .NET 应用程序使用 WinFax Pro COM 对象?

c# - Task.WhenAny 取消未完成的任务和超时

c# - WCF 代理使用

wcf - WCF 中的 ABC 代表什么?