/// <summary>
        /// Create a service object.
        /// </summary>
        /// <param name="signature">Signature of the service being created.</param>
        /// <param name="user">The user for which the service is being created.
        /// <param name="serverUrl">The server to which the API calls should be
        /// made.</param>
        /// </param>
        /// <returns>An object of the desired service type.</returns>
        public override AdsClient CreateService(ServiceSignature signature, AdsUser user,
                                                Uri serverUrl)
        {
            AdWordsAppConfig awConfig = (AdWordsAppConfig)Config;

            if (serverUrl == null)
            {
                serverUrl = new Uri(awConfig.AdWordsApiServer);
            }

            if (user == null)
            {
                throw new ArgumentNullException("user");
            }

            CheckServicePreconditions(signature);

            AdWordsServiceSignature awapiSignature = signature as AdWordsServiceSignature;

            AdsClient    service  = (AdsClient)Activator.CreateInstance(awapiSignature.ServiceType);
            PropertyInfo propInfo = awapiSignature.ServiceType.GetProperty("RequestHeader");

            if (propInfo != null)
            {
                RequestHeader clonedHeader = (RequestHeader)requestHeader.Clone();
                clonedHeader.Version   = awapiSignature.Version;
                clonedHeader.GroupName = awapiSignature.GroupName;
                propInfo.SetValue(service, clonedHeader, null);
            }

            if (awConfig.Proxy != null)
            {
                service.Proxy = awConfig.Proxy;
            }
            service.Timeout = awConfig.Timeout;
            service.Url     = string.Format("{0}api/adwords/{1}/{2}/{3}",
                                            serverUrl.AbsoluteUri, awapiSignature.GroupName, awapiSignature.Version,
                                            awapiSignature.ServiceName);
            service.EnableDecompression = awConfig.EnableGzipCompression;
            service.User      = user;
            service.Signature = awapiSignature;
            return(service);
        }
        /// <summary>
        /// Fix the request header namespace in outgoing Soap Requests, so that
        /// cross namespace requests can work properly.
        /// </summary>
        /// <param name="signature">The service creation parameters.</param>
        /// <param name="service">The service object for which RequestHeader
        /// needs to be patched.</param>
        private static void FixRequestHeaderNameSpace(AdWordsServiceSignature signature,
                                                      AdsClient service)
        {
            // Set the header namespace prefix. For all /cm services, the members
            // shouldn't have xmlns. For all other services, the members should have
            // /cm as xmlns.
            object[] attributes = service.GetType().GetCustomAttributes(false);
            foreach (object attribute in attributes)
            {
                if (attribute is WebServiceBindingAttribute)
                {
                    WebServiceBindingAttribute binding = (WebServiceBindingAttribute)attribute;
                    string delimiter = "/api/adwords/";
                    string xmlns     = String.Join("", new String[] {
                        binding.Namespace.Substring(0, binding.Namespace.IndexOf(delimiter) +
                                                    delimiter.Length), "cm/", signature.Version
                    });
                    if (xmlns == binding.Namespace)
                    {
                        xmlns = "";
                    }

                    RequestHeader svcRequestHeader = null;
                    PropertyInfo  propInfo         = service.GetType().GetProperty("RequestHeader");
                    if (propInfo != null)
                    {
                        svcRequestHeader = (RequestHeader)propInfo.GetValue(service, null);

                        if (svcRequestHeader != null)
                        {
                            PropertyInfo wsPropInfo = svcRequestHeader.GetType().GetProperty("TargetNamespace");
                            if (wsPropInfo != null)
                            {
                                wsPropInfo.SetValue(svcRequestHeader, xmlns, null);
                            }
                        }
                    }
                }
            }
        }
        /// <summary>
        /// Create a service object.
        /// </summary>
        /// <param name="signature">Signature of the service being created.</param>
        /// <param name="user">The user for which the service is being created.</param>
        /// <param name="serverUrl">The server to which the API calls should be
        /// made.</param>
        /// <returns>An object of the desired service type.</returns>
        public override AdsClient CreateService(ServiceSignature signature, AdsUser user,
                                                Uri serverUrl)
        {
            AdWordsAppConfig awConfig = (AdWordsAppConfig)Config;

            if (serverUrl == null)
            {
                serverUrl = new Uri(awConfig.AdWordsApiServer);
            }

            if (user == null)
            {
                throw new ArgumentNullException("user");
            }

            CheckServicePreconditions(signature);

            AdWordsServiceSignature awapiSignature = signature as AdWordsServiceSignature;
            EndpointAddress         endpoint       = new EndpointAddress(string.Format(ENDPOINT_TEMPLATE,
                                                                                       serverUrl, awapiSignature.GroupName, awapiSignature.Version,
                                                                                       awapiSignature.ServiceName));

            // Create the binding for the service.
            BasicHttpBinding binding = new BasicHttpBinding();

            binding.Security.Mode = BasicHttpSecurityMode.Transport;
            binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
            binding.MaxReceivedMessageSize = int.MaxValue;
            binding.TextEncoding           = Encoding.UTF8;

            AdsClient service = (AdsClient)Activator.CreateInstance(
                awapiSignature.ServiceType,
                new object[] { binding, endpoint });

            ServiceEndpoint serviceEndpoint =
                (ServiceEndpoint)service.GetType().GetProperty("Endpoint").GetValue(service, null);

            AdsServiceInspectorBehavior inspectorBehavior = new AdsServiceInspectorBehavior();

            inspectorBehavior.Add(new OAuth2ClientMessageInspector(user.OAuthProvider));

            RequestHeader clonedHeader = (RequestHeader)requestHeader.Clone();

            clonedHeader.Version   = awapiSignature.Version;
            clonedHeader.GroupName = awapiSignature.GroupName;
            inspectorBehavior.Add(new AdWordsSoapHeaderInspector()
            {
                RequestHeader = clonedHeader,
                User          = (AdWordsUser)user,
            });
            inspectorBehavior.Add(new SoapListenerInspector(user, awapiSignature.ServiceName));
            inspectorBehavior.Add(new SoapFaultInspector <AdWordsApiException>()
            {
                ErrorType = awapiSignature.ServiceType.Assembly.GetType(
                    awapiSignature.ServiceType.Namespace + ".ApiException")
            });
#if NET452
            serviceEndpoint.Behaviors.Add(inspectorBehavior);
#else
            serviceEndpoint.EndpointBehaviors.Add(inspectorBehavior);
#endif

            if (awConfig.Proxy != null)
            {
                service.Proxy = awConfig.Proxy;
            }
            service.Timeout             = awConfig.Timeout;
            service.EnableDecompression = awConfig.EnableGzipCompression;
            service.User      = user;
            service.Signature = awapiSignature;
            return(service);
        }
    /// <summary>
    /// Fix the request header namespace in outgoing Soap Requests, so that
    /// cross namespace requests can work properly.
    /// </summary>
    /// <param name="signature">The service creation parameters.</param>
    /// <param name="service">The service object for which RequestHeader
    /// needs to be patched.</param>
    private static void FixRequestHeaderNameSpace(AdWordsServiceSignature signature,
        AdsClient service) {
      // Set the header namespace prefix. For all /cm services, the members
      // shouldn't have xmlns. For all other services, the members should have
      // /cm as xmlns.
      object[] attributes = service.GetType().GetCustomAttributes(false);
      foreach (object attribute in attributes) {
        if (attribute is WebServiceBindingAttribute) {
          WebServiceBindingAttribute binding = (WebServiceBindingAttribute) attribute;
          string delimiter = "/api/adwords/";
          string xmlns = String.Join("", new String[] {
              binding.Namespace.Substring(0, binding.Namespace.IndexOf(delimiter) +
                  delimiter.Length), "cm/", signature.Version});
          if (xmlns == binding.Namespace) {
            xmlns = "";
          }

          RequestHeader svcRequestHeader = null;
          PropertyInfo propInfo = service.GetType().GetProperty("RequestHeader");
          if (propInfo != null) {
            svcRequestHeader = (RequestHeader) propInfo.GetValue(service, null);

            if (svcRequestHeader != null) {
              PropertyInfo wsPropInfo = svcRequestHeader.GetType().GetProperty("TargetNamespace");
              if (wsPropInfo != null) {
                wsPropInfo.SetValue(svcRequestHeader, xmlns, null);
              }
            }
          }
        }
      }
    }