Bug 50908 - ApplyClientBehavior Not Called on when implementing IClientMessageInspector and IEndpointBehavior
Summary: ApplyClientBehavior Not Called on when implementing IClientMessageInspecto...
Status: NEW
Alias: None
Product: Class Libraries
Classification: Mono
Component: WCF assemblies ()
Version: unspecified
Hardware: Other Other
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
Depends on:
Reported: 2016-12-27 03:59 UTC by ToddG
Modified: 2016-12-27 03:59 UTC (History)
1 user (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report for Bug 50908 on GitHub or Developer Community if you have new information to add and do not yet see a matching new report.

If the latest results still closely match this report, you can use the original description:

  • Export the original title and description: GitHub Markdown or Developer Community HTML
  • Copy the title and description into the new report. Adjust them to be up-to-date if needed.
  • Add your new information.

In special cases on GitHub you might also want the comments: GitHub Markdown with public comments

Related Links:

Description ToddG 2016-12-27 03:59:41 UTC
In order to work-around the known Xamarin issues related to an Android WCF client passing authentication information to a WCF service I attempted to implement all the proposed work-arounds detailed in (https://bugzilla.xamarin.com/show_bug.cgi?id=8020#c5) but I wasn't able to get any of them to work on Android.  All of the various reasons (except one) were well documented in various forums or Bugzilla.

The one that failed for no determinable cause was implementing IClientMessageInspector and IEndpointBehavior in order to pass the authentication to the WCF service in the message header.  In the code detailed below, the ApplyClientBehavior never invoked when the call to serviceClient is made.

        public static class CongoWebServiceClientExtensionsDroid {

            public static CongoWebServiceClient APIServiceClientFactory(ApplicationAbstract application) {
                return APIServiceClientFactory(application.LoginCookie);

            public static CongoWebServiceClient APIServiceClientFactory(HttpCookie cookie) {

                BasicHttpBinding tBinding = new BasicHttpBinding(BasicHttpSecurityMode.Transport) {                     
                    MaxBufferSize = 10485760,
                    MaxReceivedMessageSize = 10485760,
                    AllowCookies = true,
                tBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
                var client = CongoWebServiceClient.APIServiceClientFactory(tBinding);

                /*  This code SHOULD allow us to add the SubscriptionCookie to the headers, howerver the 
                 *  ApplyClientBehavior() method on the SubscriptionCookieBehavior instance is never
                 *  invoked

                SubscriptionCookieBehavior subscriptBehavior = new SubscriptionCookieBehavior(cookie);

                return client;

        public class SubscriptionCookieBehavior : IEndpointBehavior {
            private HttpCookie cookie;

            public SubscriptionCookieBehavior(HttpCookie cookie) {
                this.cookie = cookie;

            public void AddBindingParameters(ServiceEndpoint serviceEndpoint, BindingParameterCollection bindingParameters) { }

            public void ApplyClientBehavior(ServiceEndpoint serviceEndpoint, ClientRuntime clientRuntime) {
                System.Diagnostics.Debugger.Break();  /*  TO WORK THIS MUST GET CALLED BUT NEVER GETS INVOKED  */
                clientRuntime.MessageInspectors.Add(new CookieMessageInspector(cookie));

            public void ApplyDispatchBehavior(ServiceEndpoint serviceEndpoint,EndpointDispatcher endpointDispatcher) { }

            public void Validate(ServiceEndpoint serviceEndpoint) { }

        public class CookieMessageInspector : IClientMessageInspector {
            private HttpCookie cookie;

            public CookieMessageInspector(HttpCookie cookie) {
                this.cookie = cookie;

            public void AfterReceiveReply(ref Message reply, object correlationState) { }

            public object BeforeSendRequest(ref Message request, IClientChannel channel) {
                HttpRequestMessageProperty httpRequestMessage;
                object httpRequestMessageObject;
                if (request.Properties.TryGetValue(HttpRequestMessageProperty.Name, out httpRequestMessageObject)) {
                    httpRequestMessage = httpRequestMessageObject as HttpRequestMessageProperty;
                    if (string.IsNullOrEmpty(httpRequestMessage.Headers["Cookie"])) {
                        httpRequestMessage.Headers["Cookie"] = cookie.ToString() ;
                else {
                    httpRequestMessage = new HttpRequestMessageProperty();
                    httpRequestMessage.Headers.Add("Cookie", cookie.ToString() );
                    request.Properties.Add(HttpRequestMessageProperty.Name, httpRequestMessage);

                return null;

NOTE:  using  Assembly System.ServiceModel, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35