在不依赖 app.config 的情况下使用 SOAP Web 服务 [英] Consume a SOAP web service without relying on the app.config

查看:30
本文介绍了在不依赖 app.config 的情况下使用 SOAP Web 服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个将调用外部 Web 服务的 .NET 组件.我使用了添加服务参考"将 Web 服务添加到我的组件的对话框,这会生成使用服务所需的代码并将设置添加到 app.config 文件中.

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 generates the code needed to consume the service and adds the settings to the app.config file.

我正在通过从控制台应用程序添加对其 DLL 的引用并调用适当的方法来创建 Web 服务的新实例来测试该组件:... = new MyServiceSoapClient().但是,当我这样做时,出现以下异常:

I'm testing the component by adding a reference to its DLL from a Console application and calling the appropriate method that creates a new instance of the web service: ... = new MyServiceSoapClient(). However, when I do this, I get the following exception:

无效操作异常

在 ServiceModel 客户端配置部分中找不到引用合同MyServicesSoap"的默认端点元素.这可能是因为找不到您的应用程序的配置文件,或者因为在客户端元素中找不到与此合同匹配的端点元素.

Could not find default endpoint element that references contract 'MyServicesSoap' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element.

这是有道理的,因为 app.config 没有被带入组件的 DLL.如何在不依赖 App.Config 中的设置的情况下调用 Web 服务?

This makes sense since the app.config isn't being brought over with the component's DLL. How can I call the web service without having to rely on the settings in the App.Config?

推荐答案

app.config文件中中的设置会告诉组件如何连接外部网络服务.xml 只是与 Web 服务建立默认连接所需的必要类和枚举的文本表示.

The settings in <system.ServiceModel> 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.

例如,这是为我添加的 Web 服务生成的代码:

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

<system.serviceModel>
 <bindings>
  <basicHttpBinding>
   <binding name="MyServicesSoap" closeTimeout="00:01:00" openTimeout="00:01:00"
     receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false"
     bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
     maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
     messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
     useDefaultWebProxy="true">
     <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
       maxBytesPerRead="4096" maxNameTableCharCount="16384" />
     <security mode="None">
      <transport clientCredentialType="None" proxyCredentialType="None"
        realm="" />
      <message clientCredentialType="UserName" algorithmSuite="Default" />
     </security>
    </binding>
   </basicHttpBinding>
  </bindings>
 <client>
  <endpoint address="http://services.mycompany.com/WebServices/MyServices.asmx"
    binding="basicHttpBinding" bindingConfiguration="MyServicesSoap"
    contract="MyServices.MyServicesSoap" name="MyServicesSoap" />
 </client>
</system.serviceModel>

这可以翻译成这样的代码:

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)

通常,当您使用无参数构造函数(即 new MyServicesSoapClient())时,将使用 app.config 文件中的设置.但是,您可以通过在代码中显式设置 bindingendpoint 值并将这些实例传递给构造函数来绕过 app.config 文件.

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.

这篇关于在不依赖 app.config 的情况下使用 SOAP Web 服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆