/// <summary>
        /// Creates a service of the given type.
        /// </summary>
        public T GetService <T>() where T : AdsClient
        {
            Type   serviceType = typeof(T);
            string version     = serviceType.Namespace.Split('.').Last();
            AdManagerServiceSignature serviceSignature =
                new AdManagerServiceSignature(version, serviceType.Name);

            return((T)GetService(serviceSignature));
        }
Exemplo n.º 2
0
        /// <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)
        {
            AdManagerAppConfig adManagerConfig = (AdManagerAppConfig)Config;

            if (serverUrl == null)
            {
                serverUrl = new Uri(adManagerConfig.AdManagerApiServer);
            }

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

            CheckServicePreconditions(signature);

            AdManagerServiceSignature adManagerSignature = signature as AdManagerServiceSignature;
            EndpointAddress           endpoint           = new EndpointAddress(string.Format(ENDPOINT_TEMPLATE,
                                                                                             serverUrl, adManagerSignature.Version, adManagerSignature.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(adManagerSignature.ServiceType,
                                                                    new object[]
            {
                binding,
                endpoint
            });

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

            AdsServiceInspectorBehavior inspectorBehavior = new AdsServiceInspectorBehavior();

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

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

            clonedHeader.Version = adManagerSignature.Version;
            inspectorBehavior.Add(new AdManagerSoapHeaderInspector()
            {
                RequestHeader = clonedHeader,
                Config        = adManagerConfig
            });
            inspectorBehavior.Add(new SoapListenerInspector(user, adManagerSignature.ServiceName));
            inspectorBehavior.Add(new SoapFaultInspector <AdManagerApiException>()
            {
                ErrorType =
                    adManagerSignature.ServiceType.Assembly.GetType(
                        adManagerSignature.ServiceType.Namespace + ".ApiException"),
            });
#if NET452
            serviceEndpoint.Behaviors.Add(inspectorBehavior);
#else
            serviceEndpoint.EndpointBehaviors.Add(inspectorBehavior);
#endif

            if (adManagerConfig.Proxy != null)
            {
                service.Proxy = adManagerConfig.Proxy;
            }

            service.EnableDecompression = adManagerConfig.EnableGzipCompression;
            service.Timeout             = adManagerConfig.Timeout;
            service.UserAgent           = adManagerConfig.GetUserAgent();

            service.Signature = signature;
            service.User      = user;
            return(service);
        }