简体   繁体   English

通过.config文件设置自定义WCF绑定行为 - 为什么这不起作用?

[英]Setting custom WCF-binding behaviour via .config file - why doesn't this work?

I am attempting to insert a custom behavior into my service client, following the example here. 我试图在我的服务客户端中插入自定义行为,遵循此处的示例

I appear to be following all of the steps, but I am getting a ConfigurationErrorsException . 我似乎遵循所有步骤,但我得到一个ConfigurationErrorsException Is there anyone more experienced than me who can spot what I'm doing wrong? 有没有比我更有经验的人可以发现我做错了什么?

Here is the entire app.config file. 这是整个app.config文件。

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <behaviors>
            <endpointBehaviors>
                <behavior name="ClientLoggingEndpointBehaviour">
                    <myLoggerExtension />
                </behavior>
            </endpointBehaviors>
        </behaviors>
        <extensions>
            <behaviorExtensions>
                <add name="myLoggerExtension"
                     type="ChatClient.ClientLoggingEndpointBehaviourExtension, ChatClient, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"/>
            </behaviorExtensions>
        </extensions>
        <bindings>
        </bindings>
        <client>
            <endpoint
                behaviorConfiguration="ClientLoggingEndpointBehaviour"
                name="ChatRoomClientEndpoint"
                address="http://localhost:8016/ChatRoom"
                binding="wsDualHttpBinding"
                contract="ChatRoomLib.IChatRoom"
                />
        </client>
    </system.serviceModel>
</configuration>

Here is the exception message: 这是异常消息:

An error occurred creating the configuration section handler for system.serviceModel/behaviors: Extension element 'myLoggerExtension' cannot be added to this element. 为system.serviceModel / behavior创建配置节处理程序时发生错误:无法将扩展元素“myLoggerExtension”添加到此元素。 Verify that the extension is registered in the extension collection at system.serviceModel/extensions/behaviorExtensions. 验证扩展是否已在system.serviceModel / extensions / behaviorExtensions的扩展集合中注册。 Parameter name: element (C:\\Documents and Settings\\Andrew Shepherd\\My Documents\\Visual Studio 2008\\Projects\\WcfPractice\\ChatClient\\bin\\Debug\\ChatClient.vshost.exe.config line 5) 参数名称:element(C:\\ Documents and Settings \\ Andrew Shepherd \\ My Documents \\ Visual Studio 2008 \\ Projects \\ WcfPractice \\ ChatClient \\ bin \\ Debug \\ ChatClient.vshost.exe.config第5行)

I know that I've correctly written the reference to the ClientLoggingEndpointBehaviourExtensionobject, because through the debugger I can see it being instantiated. 我知道我已经正确地写了对ClientLoggingEndpointBehaviourExtensionobject的引用,因为通过调试器我可以看到它被实例化了。

This is a bit of a random thought, but maybe not: reverse the order of the elements in your config so extensions come before behaviors. 这是一个随意的想法,但可能不是:反转配置中元素的顺序,因此扩展行为之前

-Oisin -Oisin

It turns out that I didn't get the assembly qualified name EXACTLY right. 事实证明,我没有得到装配合格的名称。 The assembly qualified name was correct enough for the .NET framework to load, but then the WCF framework performs a naive character-by-character comparison when matching the behavior configurations. 程序集限定名称对于.NET框架的加载是正确的,但是在匹配行为配置时,WCF框架会执行一个简单的逐字符比较。

To finally get the exact type name, I wrote code to create an instance of ClientLoggingEndpointBehaviourExtension object, and wrote the AssemblyQualifiedName property to a local variable, which I then copy-and-pasted from the debug window into the .config file. 为了最终获得确切的类型名称,我编写了代码来创建ClientLoggingEndpointBehaviourExtension对象的实例,并将AssemblyQualifiedName属性写入局部变量,然后将其从调试窗口复制并粘贴到.config文件中。

That I had to do all this is considered to be a bug in the WCF framework. 我不得不做所有这些被认为是WCF框架中的一个错误。 (See this link ) Apparently it's fixed in .NET 4.0. (见此链接 )显然它已在.NET 4.0中修复。

Also see this article. 另见本文。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM