/// <summary>
        /// Invokes the specified method of the named service.
        /// </summary>
        /// <typeparam name="T">The expected return type.</typeparam>
        /// <param name="serviceName">The name of the service to use.</param>
        /// <param name="methodName">The name of the method to call.</param>
        /// <param name="args">The arguments to the method.</param>
        /// <returns>The return value from the web service method.</returns>
        public object InvokeMethod(string serviceName, string methodName, params object[] args)
        {
            // Create an instance of the specified service
            // and invoke the method
            //
            System.ServiceModel.Channels.Binding defaultBinding = new BasicHttpBinding(BasicHttpSecurityMode.None);

            if (this.credentials != null)
            {
                ((BasicHttpBinding)defaultBinding).Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;
                ((BasicHttpBinding)defaultBinding).Security.Transport.ClientCredentialType = HttpClientCredentialType.Ntlm;
            }

            object obj = this.webServiceAssembly.CreateInstance(serviceName, false, BindingFlags.CreateInstance, null, new object[] { defaultBinding, new EndpointAddress(this.webServiceUri.ToString()) }, null, null);

            Type type = obj.GetType();

            if (this.credentials != null)
            {
                PropertyInfo            piClientCreds  = type.GetProperty("ClientCredentials");
                ClientCredentials       creds          = (ClientCredentials)piClientCreds.GetValue(obj, null);
                PropertyInfo            piWindowsCreds = creds.GetType().GetProperty("Windows");
                WindowsClientCredential windowsCreds   = (WindowsClientCredential)piWindowsCreds.GetValue(creds, null);
                PropertyInfo            piAllowNtlm    = windowsCreds.GetType().GetProperty("AllowNtlm");
                piAllowNtlm.SetValue(windowsCreds, true, null);
                PropertyInfo piCredentials = windowsCreds.GetType().GetProperty("ClientCredential");
                piCredentials.SetValue(windowsCreds, credentials, null);
                PropertyInfo piImpersonation = windowsCreds.GetType().GetProperty("AllowedImpersonationLevel");
                piImpersonation.SetValue(windowsCreds, System.Security.Principal.TokenImpersonationLevel.Impersonation, null);
            }

            return(type.InvokeMember(methodName, BindingFlags.InvokeMethod, null, obj, args));
        }
Esempio n. 2
0
        /// <summary>
        /// Invokes the specified method of the named service.
        /// </summary>
        /// <typeparam name="T">The expected return type.</typeparam>
        /// <param name="serviceName">The name of the service to use.</param>
        /// <param name="methodName">The name of the method to call.</param>
        /// <param name="args">The arguments to the method.</param>
        /// <returns>The return value from the web service method.</returns>
        public object InvokeMethod(string serviceName, string methodName, params object[] args)
        {
            // create an instance of the specified service
            // and invoke the method
            BasicHttpBinding defaultBinding = new BasicHttpBinding(BasicHttpSecurityMode.None);


            //System.ServiceModel.Channels.Binding  defaultBinding = new BasicHttpBinding(BasicHttpSecurityMode.None);


            defaultBinding.OpenTimeout                         = new TimeSpan(0, 5, 0);
            defaultBinding.CloseTimeout                        = new TimeSpan(0, 5, 0);
            defaultBinding.SendTimeout                         = new TimeSpan(0, 5, 0);
            defaultBinding.MaxBufferSize                       = Int32.MaxValue;
            defaultBinding.MaxReceivedMessageSize              = Int32.MaxValue;
            defaultBinding.MaxBufferPoolSize                   = Int32.MaxValue;
            defaultBinding.ReaderQuotas.MaxArrayLength         = Int32.MaxValue;
            defaultBinding.ReaderQuotas.MaxStringContentLength = Int32.MaxValue;
            defaultBinding.ReaderQuotas.MaxArrayLength         = Int32.MaxValue;
            defaultBinding.ReaderQuotas.MaxBytesPerRead        = Int32.MaxValue;
            defaultBinding.ReaderQuotas.MaxDepth               = Int32.MaxValue;
            defaultBinding.ReaderQuotas.MaxNameTableCharCount  = Int32.MaxValue;



            if (this.credentials != null)
            {
                ((BasicHttpBinding)defaultBinding).Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;
                ((BasicHttpBinding)defaultBinding).Security.Transport.ClientCredentialType = HttpClientCredentialType.Ntlm;
                ((BasicHttpBinding)defaultBinding).MaxBufferSize          = Int32.MaxValue;
                ((BasicHttpBinding)defaultBinding).MaxReceivedMessageSize = Int32.MaxValue;
            }



            object obj = this.webServiceAssembly.CreateInstance(serviceName, false, BindingFlags.CreateInstance, null, new object[] { defaultBinding, new EndpointAddress(this.webServiceUri.ToString()) }, null, null);

            Type type = obj.GetType();

            if (this.credentials != null)
            {
                PropertyInfo            piClientCreds  = type.GetProperty("ClientCredentials");
                ClientCredentials       creds          = (ClientCredentials)piClientCreds.GetValue(obj, null);
                PropertyInfo            piWindowsCreds = creds.GetType().GetProperty("Windows");
                WindowsClientCredential windowsCreds   = (WindowsClientCredential)piWindowsCreds.GetValue(creds, null);
                PropertyInfo            piAllowNtlm    = windowsCreds.GetType().GetProperty("AllowNtlm");
                piAllowNtlm.SetValue(windowsCreds, true, null);
                PropertyInfo piCredentials = windowsCreds.GetType().GetProperty("ClientCredential");
                piCredentials.SetValue(windowsCreds, credentials, null);
                PropertyInfo piImpersonation = windowsCreds.GetType().GetProperty("AllowedImpersonationLevel");
                piImpersonation.SetValue(windowsCreds, System.Security.Principal.TokenImpersonationLevel.Impersonation, null);
            }

            return(type.InvokeMember(methodName, BindingFlags.InvokeMethod, null, obj, args));
        }