Consume a SOAP web service without relying on the app.config

后端 未结 3 1605
借酒劲吻你
借酒劲吻你 2020-12-01 02:07

I\'m building a .NET component that will call an external web service. I used the "Add Service Reference" dialog to add the web service to my component, which gen

3条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-01 02:35

    The settings in in the app.config file will tell the component how to connect to the external web service. The xml is simply a textual representation of the necessary classes and enumerations required to make the default connection to the web service.

    For example, this is the code that was generated for the web service that I added:

    
     
      
       
         
         
          
          
         
        
       
      
     
      
     
    
    

    This can be translated to code like so:

        'Set up the binding element to match the app.config settings '
        Dim binding = New BasicHttpBinding()
        binding.Name = "MyServicesSoap"
        binding.CloseTimeout = TimeSpan.FromMinutes(1)
        binding.OpenTimeout = TimeSpan.FromMinutes(1)
        binding.ReceiveTimeout = TimeSpan.FromMinutes(10)
        binding.SendTimeout = TimeSpan.FromMinutes(1)
        binding.AllowCookies = False
        binding.BypassProxyOnLocal = False
        binding.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard
        binding.MaxBufferSize = 65536
        binding.MaxBufferPoolSize = 524288
        binding.MessageEncoding = WSMessageEncoding.Text
        binding.TextEncoding = System.Text.Encoding.UTF8
        binding.TransferMode = TransferMode.Buffered
        binding.UseDefaultWebProxy = True
    
        binding.ReaderQuotas.MaxDepth = 32
        binding.ReaderQuotas.MaxStringContentLength = 8192
        binding.ReaderQuotas.MaxArrayLength = 16384
        binding.ReaderQuotas.MaxBytesPerRead = 4096
        binding.ReaderQuotas.MaxNameTableCharCount = 16384
    
        binding.Security.Mode = BasicHttpSecurityMode.None
        binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None
        binding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.None
        binding.Security.Transport.Realm = ""
        binding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName
        binding.Security.Message.AlgorithmSuite = Security.SecurityAlgorithmSuite.Default
    
        'Define the endpoint address'
        Dim endpointStr = "http://services.mycompany.com/WebServices/MyServices.asmx"
        Dim endpoint = New EndpointAddress(endpointStr)
        'Instantiate the SOAP client using the binding and endpoint'
        'that were defined above'
        Dim client = New MyServicesSoapClient(binding, endpoint)
    

    Usually, when you use the parameterless constructor (i.e. new MyServicesSoapClient()), the settings in the app.config file will be used. However, you can bypass the app.config file by explicitly setting the binding and endpoint values in code and passing those instances into the constructor.

提交回复
热议问题