예제 #1
0
 private static void CloseConnection()
 {
     if (_myClient != null && (_myClient.State == System.ServiceModel.CommunicationState.Opened))
     {
         _myClient.Close();
     }
     _myClient = null;
 }
예제 #2
0
        private static void OpenConnection(common.configuration.wsConnectionInfo wsInfo)
        {
            if (_myClient != null)
            {
                _myClient.Abort();
            }
            _myClient = new ServiceReference1.StockServiceClient();

            System.ServiceModel.WSHttpBinding binding = (_myClient.Endpoint.Binding as System.ServiceModel.WSHttpBinding);

            binding.OpenTimeout  = TimeSpan.FromSeconds(wsInfo.timeoutInSecs);
            binding.CloseTimeout = TimeSpan.FromSeconds(wsInfo.timeoutInSecs);
            binding.SendTimeout  = TimeSpan.FromSeconds(wsInfo.timeoutInSecs);

            binding.MaxReceivedMessageSize = constMaxReceivedMessageSize;
            binding.ReaderQuotas.MaxStringContentLength = constMaxStringContentLength;
            binding.ReaderQuotas.MaxBytesPerRead        = constMaxBytesPerRead;

            //Proxy  must befor setting Endpoint ?
            if (wsInfo.useProxy)
            {
                //Chua loi proxy http://chrishaas.wordpress.com/2009/11/02/fixing-the-remote-server-returned-an-error-417-expectation-failed/

                ServicePointManager.Expect100Continue = false;

                if (wsInfo.useDefaultProxy)
                {
                    binding.UseDefaultWebProxy = true;
                }
                else
                {
                    if (wsInfo.proxyAddress.Trim() != "" && wsInfo.proxyPort.Trim() != "")
                    {
                        binding.ProxyAddress       = new Uri(wsInfo.proxyAddress.Trim() + ":" + wsInfo.proxyPort.Trim());
                        binding.BypassProxyOnLocal = true;
                        binding.UseDefaultWebProxy = false;
                    }
                }
            }
            //Endpoint settings
            _myClient.Endpoint.Address = new System.ServiceModel.EndpointAddress(wsInfo.URI);
            if (wsInfo.isWindowAuthentication)
            {
                _myClient.ClientCredentials.Windows.ClientCredential.UserName = wsInfo.account;
                _myClient.ClientCredentials.Windows.ClientCredential.Password = wsInfo.password;
            }
            else
            {
                _myClient.ClientCredentials.UserName.UserName = wsInfo.account;
                _myClient.ClientCredentials.UserName.Password = wsInfo.password;
            }
            //For testing
            //_myClient.Endpoint.Address = new System.ServiceModel.EndpointAddress("http://localhost:8731/wsServices/DataLibs/?wsdl");
            //_myClient.Endpoint.Address = new System.ServiceModel.EndpointAddress("http://localhost/DataLibs.svc");
            //_myClient.Endpoint.Address = new System.ServiceModel.EndpointAddress("http://localhost:8731/wsServices/DataLibs");
            //_myClient.ClientCredentials.Windows.ClientCredential.UserName = "";
            //_myClient.ClientCredentials.Windows.ClientCredential.Password = "";
            //ServicePointManager.UseNagleAlgorithm = true;
            //ServicePointManager.CheckCertificateRevocationList = true;
            //ServicePointManager.DefaultConnectionLimit = ServicePointManager.DefaultPersistentConnectionLimit;

            _myClient.Open();
        }