【问题标题】:The element 'behavior' has invalid child element 'myFaultExtension' in wcf app.configwcf app.config 中的元素“行为”具有无效的子元素“myFaultExtension”
【发布时间】:2011-01-13 06:20:24
【问题描述】:

我正在尝试从 Silverlight 客户端应用程序中的 WCF 服务捕获常规异常。为此,我在我的 WCF 服务中包含了给定 in this MSDN article 的相应更改。

但是当我配置行为扩展并在端点行为中使用相同的行为时,就会出现上述错误,并且由于此错误,服务无法运行。

我将我的配置放在这里。请建议我该如何解决这个问题?

  <extensions>
      <!--Add a behavior extension within the service model-->
      <!-- Here SilverlightFaultBehavior is a class in AppServiceLib namespace -->
      <behaviorExtensions>
        <add name="myFaultExtension"
             type="AppServiceLib.SilverlightFaultBehavior,AppServiceLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
      </behaviorExtensions>
    </extensions>
   <endpointBehaviors>
        <behavior name="myFaultBehavior">
          <**myFaultExtension**/>
        </behavior>
   </endpointBehaviors>

【问题讨论】:

标签: wcf wcf-extensions


【解决方案1】:

我想添加为端点行为的自定义行为扩展出现此错误。因此,我编辑了 Visual Studio 2017 中使用的架构,以消除 web.config 文件中的警告。这与您收到的警告相同:

元素“行为”具有无效的子元素“CustomSecurity”。预期的可能元素列表:'clientVia、callbackDebug、callbackTimeouts、clear、clientCredentials、transactedBatching、dataContractSerializer、dispatcherSynchronization、remove、synchronousReceive、webHttp、enableWebScript、endpointDiscovery、soapProcessing'。

我的 web.config 有:

<system.serviceModel>
    <extensions>
        <behaviorExtensions>
            <add name="CustomSecurity"
                type="FullyQualifiedPath.MyCustomBehaviorExtension, MyAssemblyName"/>
            </behaviorExtensions>
    </extensions>
    <endpointBehaviors>
       <behavior name="CustomServiceBehavior">
          <CustomSecurity />
       </behavior>
    </endpointBehaviors>
    <endpoint address="https://SomeServer/MyService.svc/soap"
    behaviorConfiguration="CustomServiceBehavior" binding="basicHttpBinding"
    bindingConfiguration="BasicHttpBinding_IProject" contract="ProjectService.IProject"
    name="BasicHttpBinding_IProject" />

在 Visual Studio 中,CustomSecurity XML 节点下方总是有蓝色波浪线。它在错误列表窗口中显示为警告。我想摆脱它,因为每次我尝试更新服务引用时,都会因为 web.config 中的警告而失败。

因此,要修复它,您需要编辑 Visual Studio 用于验证元素的架构。因此,我打开了我的 web.config,然后在 Visual Studio 主菜单栏上选择了 XML。然后选择架构。您将获得一长串模式。找到“DotNetConfig.xsd”(或 DotNetConfig[XX].xsd,其中 XX 是 .NET Framework 版本),如下所示。

更新:您可能希望使用 DotNetConfig 文件前缀编辑任何/所有 xsd 文件。通常,您不想一次使用所有 DotNetConfigXX.xsd 文件。最好只打开一个“使用此架构”选项(在“使用”列中);否则,您可能会看到有关已定义架构元素的错误。

浏览到位置列中显示的路径并编辑 xsd 文件。搜索:&lt;xs:element name="behavior" vs:help="configuration/system.serviceModel/behaviors/endpointBehaviors/behavior"&gt;

然后在 xs:choice 节点中添加一个新的 xs:element 节点,并使用您的自定义行为扩展名;就我而言,CustomSecurity。保存文件,Visual Studio 应该会自动验证新架构,并且您不应再在 web.config 中收到警告。

<xs:element name="behavior" vs:help="configuration/system.serviceModel/behaviors/endpointBehaviors/behavior">
<xs:complexType>
<xs:annotation>
    <xs:documentation>The behavior element contains a collection of settings for the behavior of an endpoint.</xs:documentation>
</xs:annotation>
<xs:choice minOccurs="0" maxOccurs="unbounded">
    <xs:element name="CustomSecurity" vs:help="configuration/system.serviceModel/behaviors/endpointBehaviors/behavior/CustomSecurity">
        <xs:complexType>
            <xs:annotation>
                <xs:documentation>Specifies the behavior extension class applied to the endpoint.</xs:documentation>
            </xs:annotation>
            <xs:anyAttribute namespace="http://schemas.microsoft.com/XML-Document-Transform" processContents="strict" />
        </xs:complexType>
    </xs:element>
    <xs:element name="clientVia" vs:help="configuration/system.serviceModel/behaviors/endpointBehaviors/behavior/clientVia">
        <xs:complexType>
            <xs:annotation>
                <xs:documentation>Specifies the URI for which the transport channel should be created.</xs:documentation>
            </xs:annotation>
            <xs:attribute name="viaUri" type="xs:string" use="optional">
                <xs:annotation>
                    <xs:documentation>A string that specifies a URI that indicates the route a message should take.</xs:documentation>
                </xs:annotation>
            </xs:attribute>
            <xs:attribute name="lockAttributes" type="xs:string" use="optional" />
            <xs:attribute name="lockAllAttributesExcept" type="xs:string" use="optional" />
            <xs:attribute name="lockElements" type="xs:string" use="optional" />
            <xs:attribute name="lockAllElementsExcept" type="xs:string" use="optional" />
            <xs:attribute name="lockItem" type="boolean_Type" use="optional" />
            <xs:anyAttribute namespace="http://schemas.microsoft.com/XML-Document-Transform" processContents="strict" />
        </xs:complexType>
    </xs:element>

【讨论】:

  • 这种方法也适用于我,修复了关于 cors 的警告。我的项目使用 4.6.1,所以我编辑了 Schemas/1033/DotNetConfig.xsd 和 Schemas/DontNetConfig45.xsd(没有 461.xsd)。似乎成功了。
  • 我可以在 VS 2017 中构建而无需这样做,但波浪线让我抓狂。谢谢!
【解决方案2】:

当程序集的版本在程序集编译/构建期间自动递增时,这会导致问题。

自 .NET 4.0 起已修复。 Version/Culture/PublicKeyToken 可能被删除,以便配置不再需要版本的自动递增值。

<behaviorExtensions>
    <add name="serviceKeyBehavior"  
     type="MyNamespace.ServiceKeyBehaviorExtensionElement, MyAssembly"/>
</behaviorExtensions>

【讨论】:

    【解决方案3】:

    我遇到了同样的问题。我的解决方案实际上是在上述重复的帖子Hearing "element 'behavior' has invalid child element" should be ignored, but prevented from updating service reference because of it 中提供的。原来“类型”字段非常敏感。最后使用在另一篇文章中提到的Console.WriteLine(typeof(BetterErrorMessagesFaultBehavior).AssemblyQualifiedName); 作为answer 来获得我需要的确切类型。

        <add name="myFaultExtension"
             type="AppServiceLib.SilverlightFaultBehavior,AppServiceLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
    

    【讨论】:

    • @Akira:看看我的回答,可能对你有帮助。
    【解决方案4】:

    尝试使用编辑器在 web.config 中定义您的 WCF,以防止出错。 (特别是当您需要编写整个类型名称时)。

    右键单击 web.config,然后编辑 WCF 配置:

    然后转到:高级-->扩展-->行为元素扩展-->新建

    然后在(常规)下单击左侧小按钮并选择新行为。它将为您在 app.config 中写入完整的类型名称。

    现在您可以在 app.config 中的 &lt;extensions&gt; 标记下以正确的类型名称查看您的新行为。

    【讨论】:

    • 每当我打开这个 GUI 编辑器时,它都会告诉我它找不到程序集。当我再次添加它时,保存文件,关闭编辑器,然后再次打开 GUI 编辑器,它说的是同样的事情。有什么线索吗?
    • @S.tenBrinke 当您打开该工具时,它会淡化 .config 文件并根据此数据构建屏幕。也就是说,如果您的配置文件中有任何语法错误,它将失败并显示错误消息。请注意,有时标签上会出现错误,表明它们的兼容程序集不在 GAC 内。例如:自定义 WCF 行为。 VS 编辑器用下划线标记它们。在这种情况下,您必须在打开此工具之前将它们移至 GAC 或将标签注释掉。
    【解决方案5】:

    iCode 的解决方案对我有用,但我必须编辑在 ...Xml/Schemas/ 中找到的所有版本的 DotNetCofig4x.xsd 文件。 4x.xsd 中的 x 表示 40.xsd、45.xsd、47.xsd 和 471.xsd

    【讨论】:

      猜你喜欢
      • 2016-10-23
      • 2021-03-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-15
      • 1970-01-01
      • 1970-01-01
      • 2021-05-28
      • 2012-05-02
      相关资源
      最近更新 更多