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 (show other bugs)
Version: unspecified
Hardware: Other Other
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2016-12-27 03:59 UTC by ToddG
Modified: 2016-12-27 03:59 UTC (History)
1 user (show)

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


Attachments

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);
                client.Endpoint.Behaviors.Add(subscriptBehavior);                

                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=2.0.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35

Note You need to log in before you can comment on or make changes to this bug.