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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/169342/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 10:14:24  来源:igfitidea点击:

Why isn't my custom WCF behavior extension element type being found?

.netwcfexception-handlingwcf-extensions

提问by Travis Illig

I have a solution that contains two projects. One project is an ASP.NET Web Application Project, and one is a class library. The web application has a project reference to the class library. Neither of these is strongly-named.

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

In the class library, which I'll call "Framework," I have an endpoint behavior (an IEndpointBehavior implementation) and a configuration element (a class derived from BehaviorExtensionsElement). The configuration element is so I can attach the endpoint behavior to a service via configuration.

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

In the web application, I have an AJAX-enabled WCF service. In web.config, I have the AJAX service configured to use my custom behavior. The system.serviceModel section of the configuration is pretty standard and looks like this:

在 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>

At runtime, this works perfectly. The AJAX enabled WCF service correctly uses my custom configured endpoint behavior.

在运行时,这完美地工作。启用 AJAX 的 WCF 服务正确使用我自定义配置的端点行为。

The problem is when I try to add a new AJAX WCF service. If I do Add -> New Item... and select "AJAX-enabled WCF Service," I can watch it add the .svc file and codebehind, but when it gets to updating the web.config file, I get this error:

问题是当我尝试添加新的 AJAX WCF 服务时。如果我执行 Add -> New Item... 并选择“AJAX-enabled WCF Service”,我可以看到它添加了 .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.

配置文件不是 WCF 服务库的有效配置文件。

无法加载为扩展“customEndpointBehavior”注册的类型“Framework.MyBehaviorExtensionsElement、Framework、Version=1.0.0.0、Culture=neutral、PublicKeyToken=null”。

Obviously the configuration is entirely valid since it works perfectly at runtime. If I remove the element from my behavior configuration temporarily and then add the AJAX-enabled WCF Service, everything goes without a hitch.

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

Unfortunately, in a larger project where we will have multiple services with various configurations, removing all of the custom behaviors temporarily is going to be error prone. While I realize I could go without using the wizard and do everything manually, not everyone can, and it'd be nice to be able to just use the product as it was meant to be used - wizards and all.

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

Why isn't my custom WCF behavior extension element type being found?

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

Updates/clarifications:

更新/说明:

  • It does work at runtime, just not design time.
  • The Framework assembly is in the web project's bin folder when I attempt to add the service.
  • While I could add services manually ("without configuration"), I need the out-of-the-box item template to work - that's the whole goal of the question.
  • This issue is being seen in Visual Studio 2008. In VS 2010 this appears to be resolved.
  • 它确实在运行时有效,只是在设计时无效。
  • 当我尝试添加服务时,框架程序集位于 web 项目的 bin 文件夹中。
  • 虽然我可以手动添加服务(“无需配置”),但我需要开箱即用的项目模板才能工作 - 这就是问题的全部目标。
  • 此问题在 Visual Studio 2008 中出现在 VS 2010 中,这似乎已解决。

I filed this issue on Microsoft Connectand it turns out you either have to put your custom configuration element in the GAC or put it in the IDE folder. They won't be fixing it, at least for now. I've posted the workaround they provided as the "answer" to this question.

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

采纳答案by Travis Illig

Per the workaroundthat Microsoft posted on the Connect issueI filed for this, it's a known issue and there won't be any solution for it, at least in the current release:

根据Microsoft 在我为此提交的 Connect 问题上发布的解决方法,这是一个已知问题,至少在当前版本中没有任何解决方案:

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.

添加新服务项失败的原因: 添加新服务项和更新配置文件时,系统会尝试加载配置文件,因此会尝试在此配置文件中搜索并加载 cusom 扩展程序集。只有在程序集被GACed 或与vs exe 位于同一路径(Program Files\Microsoft Visual Studio 9.0\Common7\IDE)的情况下,系统才能找到它。否则,将弹出错误对话框,“添加新项目”将失败。

我理解你的痛点。不幸的是,我们无法在当前版本中进行此更改。我们将在以后的版本中对其进行调查并尝试提供更好的解决方案,例如提供浏览对话框以允许客户指定路径,或者提供更好的错误消息以指示一些解决方案等...

您能否尝试在当前阶段解决此问题:GAC 您的自定义扩展程序集或将其复制到“Program Files\Microsoft Visual Studio 9.0\Common7\IDE”?

我们将提供自述文件以帮助可能遇到相同问题的其他客户。

Unfortunately, it appears I'm out of luck on this one.

不幸的是,我似乎在这方面不走运。

回答by Developer

I just used

我刚用

[assembly: AssemblyVersion("1.0.*")]
//[assembly: AssemblyVersion("1.0.0.0")]
//[assembly: AssemblyFileVersion("1.0.0.0")] 

So I have new assembly build number every time.

所以我每次都有新的程序集版本号。

But we have

但是我们有

   <add name="clientCredential" type="Client.ClientCredentialElement, Client, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />

where Version=1.0.0.0THIS IS WRONG!!!

其中Version=1.0.0.0这是错误的!!!

So you have 2 options

所以你有2个选择

  1. Back to

    //[assembly: AssemblyVersion("1.0.*")] 
    [assembly: AssemblyVersion("1.0.0.0")]  Keep it manually.
    [assembly: AssemblyFileVersion("1.0.0.0")] 
    
  2. Every build manually replace Version=1.0.0.0with a correct number.

  1. 回到

    //[assembly: AssemblyVersion("1.0.*")] 
    [assembly: AssemblyVersion("1.0.0.0")]  Keep it manually.
    [assembly: AssemblyFileVersion("1.0.0.0")] 
    
  2. 每个构建手动将Version=1.0.0.0替换为正确的数字。

回答by cdmdotnet

As an FYI to anyone who stumbles across this these days a possible solution is to FULLY qualify your assembly in your app.config/web.config. EG if you had

作为对这些天偶然发现此问题的任何人的仅供参考,一个可能的解决方案是在您的 app.config/web.config 中完全限定您的程序集。EG 如果你有

<system.serviceModel>
    <extensions>
        <behaviorExtensions>
            <add name="clientCredential" type="Client.ClientCredentialElement, Client" />
        </behaviorExtensions>
    </extensions>

try - replacing the values as necassary

尝试 - 根据需要替换值

<system.serviceModel>
    <extensions>
        <behaviorExtensions>
            <add name="clientCredential" type="Client.ClientCredentialElement, Client, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
        </behaviorExtensions>
    </extensions>

this particular solution worked for me.

这个特殊的解决方案对我有用。

回答by Derek Atlansky

I tried this with a new project just to make sure it wasn't your specific project/config and had the exact same issue.

我在一个新项目中尝试了这个,只是为了确保它不是您的特定项目/配置并且有完全相同的问题。

Using fusion logs, it appears that the system looks for the behavior extensions ONLY in the IDE directory (C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE). Copying the assembly to this directory in a post-build step works, but is ugly.

使用融合日志,系统似乎仅在 IDE 目录 (C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE) 中查找行为扩展。在构建后步骤中将程序集复制到此目录有效,但很难看。

回答by James Bender

Do you have a copy of Framework.dll with your custom behavior in the bin directory of your web project? If not that is probably the problem. Visual Studio is looking for the implementation of the behavior. Since it's listed in your config it doesn't think to look in the other projects; it expects to find the assembly in the bin.

您的 Web 项目的 bin 目录中是否有包含自定义行为的 Framework.dll 副本?如果不是,那可能是问题所在。Visual Studio 正在寻找行为的实现。由于它已在您的配置中列出,因此不会考虑查看其他项目;它希望在 bin 中找到该程序集。

Depending on how your project is setup, it may be able to run in debug without this assembly being put in the bin, although VS usually builds it and puts it there. But again, it depends on how things are setup.

根据您的项目的设置方式,它可能能够在调试中运行,而无需将此程序集放入 bin 中,尽管 VS 通常会构建它并将其放在那里。但同样,这取决于事物的设置方式。

Anyway, might just want to double check at that the assembly is available at design time.

无论如何,可能只想仔细检查程序集在设计时是否可用。

回答by abatishchev

Here's the list of steps worked for me:

这是对我有用的步骤列表:

  • Install dll into GAC, i.e. gacutil /i Bla.dll
  • Get FQN of dll, i.e. gacutil /l Bla
  • Copy resulting FQN into Web.config
  • Add new service in VS
  • Uninstall dll from GAC, i.e. gacutil /u Bla
  • 将dll安装到GAC中,即gacutil /i Bla.dll
  • 获取 dll 的 FQN,即 gacutil /l Bla
  • 将生成的 FQN 复制到 Web.config
  • 在 VS 中添加新服务
  • 从 GAC 卸载 dll,即 gacutil /u Bla

All together only.

都在一起而已

回答by Joe

Putting the assembly in the GAC would probably help, but I appreciate this isn't the answer you're looking for. Not sure where else VS will look for assemblies apart from the GAC and the directory containing devenv.exe.

将程序集放在 GAC 中可能会有所帮助,但我很欣赏这不是您正在寻找的答案。除了 GAC 和包含 devenv.exe 的目录之外,不知道 VS 还会在哪里查找程序集。

回答by Ryan

I solved this by commenting out the relevant sections in the web.config including the element that used the custom extension, the element and the element.

我通过注释掉 web.config 中的相关部分解决了这个问题,包括使用自定义扩展的元素、元素和元素。

After that I was able to add a WCF service to the project, add the lines back into the web.config and publish the project.

之后,我能够向项目添加 WCF 服务,将这些行添加回 web.config 并发布项目。

回答by MSallal

if you are using framework 3.5 the Culture=neutralin small not Culture=Neutral in CAPITAL

如果您使用的是框架 3.5,则文化=中性小而不是文化=中性大写

回答by jrmack

I had the extension class within the same project (dll) as my service class and could not get it to work. Once I moved it to another project and referenced it from the service project it worked. Just in case anyone else runs into this issue.

我在与我的服务类相同的项目 (dll) 中拥有扩展类,但无法使其工作。一旦我将它移动到另一个项目并从它工作的服务项目中引用它。以防万一其他人遇到这个问题。