Ben*_*ack 32 .net c# vb.net web-services app-config
我正在构建一个将调用外部Web服务的.NET组件.我使用"添加服务引用"对话框将Web服务添加到我的组件,该组件生成使用该服务所需的代码并将设置添加到app.config文件中.
我正在通过从Console应用程序添加对其DLL的引用并调用创建Web服务的新实例的适当方法来测试该组件:... = new MyServiceSoapClient().但是,当我这样做时,我得到以下异常:
出现InvalidOperationException
无法在ServiceModel客户端配置部分中找到引用合同"MyServicesSoap"的默认端点元素.这可能是因为没有为您的应用程序找到配置文件,或者因为在客户端元素中找不到与此合同匹配的端点元素.
这是有道理的,因为app.config没有被组件的DLL带来.如何在不必依赖App.Config中的设置的情况下调用Web服务?
Ben*_*ack 71
<system.ServiceModel>app.config文件中的设置将告诉组件如何连接到外部Web服务.xml只是对Web服务进行默认连接所需的必要类和枚举的文本表示.
例如,这是为我添加的Web服务生成的代码:
<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>
Run Code Online (Sandbox Code Playgroud)
这可以转换为如下代码:
'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)
Run Code Online (Sandbox Code Playgroud)
通常,当您使用无参数构造函数(即new MyServicesSoapClient())时,将使用app.config文件中的设置.但是,您可以通过在代码中显式设置binding和endpoint值并将这些实例传递给构造函数来绕过app.config文件.
| 归档时间: |
|
| 查看次数: |
49004 次 |
| 最近记录: |