元素'behavior'在wcf app.config中具有无效的子元素'myFaultExtension'

pad*_*thi 7 wcf wcf-extensions

我试图在Silverlight客户端应用程序中捕获来自WCF服务的常规异常.为此,我已经在我的WCF服务中包含了相应的更改,如本MSDN文章中所述.

但是,当我配置行为扩展并在端点行为中使用相同的行为时,上面提到的错误即将发生,并且由于此错误,服务无法运行.

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

  <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>
Run Code Online (Sandbox Code Playgroud)

iCo*_*ode 5

我的自定义行为扩展出现了这个错误,我想将其添加为端点行为。因此,我编辑了 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" />
Run Code Online (Sandbox Code Playgroud)

在 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 文件。搜索:<xs:element name="behavior" vs:help="configuration/system.serviceModel/behaviors/endpointBehaviors/behavior">

然后在 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>
Run Code Online (Sandbox Code Playgroud)


Sky*_*002 1

我遇到了同样的问题。对我来说的解决方案实际上是在上述重复发布中提供的,听证会“元素‘行为’具有无效的子元素”应该被忽略,但因此无法更新服务引用。事实证明“类型”字段非常敏感。最终使用Console.WriteLine(typeof(BetterErrorMessagesFaultBehavior).AssemblyQualifiedName);提到的内容作为另一篇文章的答案来获得我需要的确切类型。

    <add name="myFaultExtension"
         type="AppServiceLib.SilverlightFaultBehavior,AppServiceLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
Run Code Online (Sandbox Code Playgroud)