This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> | |
<s:Header> | |
<callback s:mustUnderstand="1">http://www.server.com/</callback> | |
</s:Header> | |
<s:Body /> | |
</s:Envelope> |
By default a Wcf service will validate all incoming mustUnderstand headers a client sends. If it does not understand them it will throw the famous 'Did not understand "MustUnderstand" header' exception. Typically you would instruct Wcf not to validate these headers like this:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[System.ServiceModel.ServiceBehavior(ValidateMustUnderstand = false)] | |
public class MyService : IServiceContract | |
{ | |
// ... | |
} |
But this kind of "hard codes" this behavior to the service. Wouldn't it be nice to decide at the configuration level if we want such a behavior or not?
All we need to do is define this class:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ValidateMustUnderstandElement : BehaviorExtensionElement | |
{ | |
public override Type BehaviorType | |
{ | |
get { return typeof (MustUnderstandBehavior); } | |
} | |
protected override object CreateBehavior() | |
{ | |
return new MustUnderstandBehavior(false); | |
} | |
} |
Then in the config register it:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<system.serviceModel> | |
<extensions> | |
<behaviorExtensions> | |
<add name="validateMustUnderstand" type="WCF_Self_Hosted_Service48.ValidateMustUnderstandElement, WCF Self Hosted Service48, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" /> | |
</behaviorExtensions> | |
</extensions> | |
... |
And we can now configure our endpoint(s) with this behavior:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<behaviors> | |
<endpointBehaviors> | |
<behavior name="NewBehavior"> | |
<validateMustUnderstand /> | |
</behavior> | |
</endpointBehaviors> | |
... |
4 comments:
Hi Yaron,
Kindly need help you advise about my problem.
I've tried to add MustunderstandBehaviour to my service
MustUnderstandBehavior mustUnderstand = new MustUnderstandBehavior(false);
mustUnderstand.ValidateMustUnderstand = false;
client.ChannelFactory.Endpoint.Behaviors.Add(mustUnderstand);
but it seems the mustundertand = "1", i want to change from "1" to "0"
Thanks.
to set mustunderstand to 0 you should implement a custom encoder. I believe you want to set it for outgoing requests while this post deals with incoming responses.
Hi Yaron,
Yes, I've tried to make a custom encoder to make it became 0, but after im save the modified xml. I got the canonical issue. It seems the format should be followed canonical format.
Need your advise!!
not sure I understand, what is the canonical issue?
Post a Comment