2011-01-13 23 views
7

我试图从Silverlight客户端应用程序中的WCF服务中捕获常规异常。为此,我在WCF服务中包含了相应的更改,如in this MSDN article所示。元素的'行为'在wcf app.config中有无效的子元素'myFaultExtension'

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

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

<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> 
+0

http://connect.microsoft.com/VisualStudio/feedback/details/619106/wcf-fails-to-find-custom-behaviorextensionelement-if-type-attribute-doesnt-match-exactly – nologo

+0

可能重复[听力“元素'行为'具有无效的子元素”应该被忽略,但由于它更新服务引用而被阻止](http://stackoverflow.com/questions/9482091/hearing-element-behavior-has-invalid-子元素应该被忽略 - 但公关) – McGarnagle

回答

0

你可以在Visual Studio中创建的WCF配置文件时,因为VS编辑器不知道该扩展可以得到这个错误。

但这是否也在运行时发生?

你如何以及在哪里使用这种行为?在服务器端?在客户端?都?

0

我遇到了同样的问题。我的解决方案实际上是在前面提到的重复发帖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"/> 
+0

没有为我工作。 –

+0

@Akira:检查我的答案,它可能会帮助你。 –

2

这会在汇编编译/构建过程中自动增加程序集的版本时导致问题。

自.NET 4.0以来已修复。版本/文化/公钥令牌可能被丢弃,以便配置不再需要该版本的自动增量值。

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

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

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

enter image description here

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

enter image description here

然后在(常规)下点击左边的小按钮,选择新的行为。它会为你编写app.config中的完整类型名称。现在

enter image description here

你可以看到你的新的行为<extensions>标签下方,与正确的类型名称的app.config。

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" /> 

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

所以,要解决这个问题,您需要编辑Visual Studio用于验证元素的架构。所以,我打开了我的web.config,然后在主Visual Studio菜单栏上选择了XML。然后选择模式。你会得到一个长长的模式列表。找到如下所示的“DotNetConfig.xsd”。 enter image description here

浏览到显示的路径并编辑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> 
相关问题