How to create a WCF client without settings in config file?

后端 未结 3 1161
梦谈多话
梦谈多话 2021-01-04 02:54

I just start work on WCF a month ago. Please forgive me if I ask something already answered. I try to search first but found nothing.

I read this article, WCF File T

3条回答
  •  执念已碎
    2021-01-04 03:31

    This is totally code based configuration and working code. You dont need any configuration file for client. But at least you need one config file there (may be automatically generated, you dont have to think about that). All the configuration setting is done here in code.

    public class ValidatorClass
    {
        WSHttpBinding BindingConfig;
        EndpointIdentity DNSIdentity;
        Uri URI;
        ContractDescription ConfDescription;
    
        public ValidatorClass()
        {  
            // In constructor initializing configuration elements by code
            BindingConfig = ValidatorClass.ConfigBinding();
            DNSIdentity = ValidatorClass.ConfigEndPoint();
            URI = ValidatorClass.ConfigURI();
            ConfDescription = ValidatorClass.ConfigContractDescription();
        }
    
    
        public void MainOperation()
        {
            var Address = new EndpointAddress(URI, DNSIdentity);
            var Client = new EvalServiceClient(BindingConfig, Address);
            Client.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.PeerTrust;
            Client.Endpoint.Contract = ConfDescription;
            Client.ClientCredentials.UserName.UserName = "companyUserName";
            Client.ClientCredentials.UserName.Password = "companyPassword";
            Client.Open();
    
            string CatchData = Client.CallServiceMethod();
    
            Client.Close();
        }
    
    
    
        public static WSHttpBinding ConfigBinding()
        {
            // ----- Programmatic definition of the SomeService Binding -----
            var wsHttpBinding = new WSHttpBinding();
    
            wsHttpBinding.Name = "BindingName";
            wsHttpBinding.CloseTimeout = TimeSpan.FromMinutes(1);
            wsHttpBinding.OpenTimeout = TimeSpan.FromMinutes(1);
            wsHttpBinding.ReceiveTimeout = TimeSpan.FromMinutes(10);
            wsHttpBinding.SendTimeout = TimeSpan.FromMinutes(1);
            wsHttpBinding.BypassProxyOnLocal = false;
            wsHttpBinding.TransactionFlow = false;
            wsHttpBinding.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
            wsHttpBinding.MaxBufferPoolSize = 524288;
            wsHttpBinding.MaxReceivedMessageSize = 65536;
            wsHttpBinding.MessageEncoding = WSMessageEncoding.Text;
            wsHttpBinding.TextEncoding = Encoding.UTF8;
            wsHttpBinding.UseDefaultWebProxy = true;
            wsHttpBinding.AllowCookies = false;
    
            wsHttpBinding.ReaderQuotas.MaxDepth = 32;
            wsHttpBinding.ReaderQuotas.MaxArrayLength = 16384;
            wsHttpBinding.ReaderQuotas.MaxStringContentLength = 8192;
            wsHttpBinding.ReaderQuotas.MaxBytesPerRead = 4096;
            wsHttpBinding.ReaderQuotas.MaxNameTableCharCount = 16384;
    
            wsHttpBinding.ReliableSession.Ordered = true;
            wsHttpBinding.ReliableSession.InactivityTimeout = TimeSpan.FromMinutes(10);
            wsHttpBinding.ReliableSession.Enabled = false;
    
            wsHttpBinding.Security.Mode = SecurityMode.Message;
            wsHttpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate;
            wsHttpBinding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.None;
            wsHttpBinding.Security.Transport.Realm = "";
    
            wsHttpBinding.Security.Message.NegotiateServiceCredential = true;
            wsHttpBinding.Security.Message.ClientCredentialType = MessageCredentialType.UserName;
            wsHttpBinding.Security.Message.AlgorithmSuite = System.ServiceModel.Security.SecurityAlgorithmSuite.Basic256;
           // ----------- End Programmatic definition of the SomeServiceServiceBinding --------------
    
           return wsHttpBinding;
    
       }
    
       public static Uri ConfigURI()
       {
           // ----- Programmatic definition of the Service URI configuration -----
           Uri URI = new Uri("http://localhost:8732/Design_Time_Addresses/TestWcfServiceLibrary/EvalService/");
    
           return URI;
       }
    
       public static EndpointIdentity ConfigEndPoint()
       {
           // ----- Programmatic definition of the Service EndPointIdentitiy configuration -----
           EndpointIdentity DNSIdentity = EndpointIdentity.CreateDnsIdentity("tempCert");
    
           return DNSIdentity;
       }
    
    
       public static ContractDescription ConfigContractDescription()
       {
            // ----- Programmatic definition of the Service ContractDescription Binding -----
            ContractDescription Contract = ContractDescription.GetContract(typeof(IEvalService), typeof(EvalServiceClient));
    
            return Contract;
       }
    }
    

提交回复
热议问题