【发布时间】:2016-05-27 11:05:36
【问题描述】:
我们有一个运行异步的 CRM2011 插件,它需要连接到网络服务以获取一些丢失的数据。我试图解决这个问题的方法是使用使用“添加服务引用”制作的 web 服务客户端。
但对于配置,我不想硬编码端点和绑定。所以我想我会像这样使用OpenMappedExeConfiguration 加载网络服务的配置。
var config = ConfigurationManager.OpenMappedExeConfiguration(new ExeConfigurationFileMap{ ExeConfigFilename = @"c:\Path\to\config.xml"}, ConfigurationUserLevel.None);
配置文件已加载...但是客户端看不到它。因为调用
var client = new MyDataServiceClient();
会抛出
无效操作异常:
找不到引用合约的默认端点元素 ServiceModel 客户端中的“MyDataService.IMyDataService” 配置部分。这可能是因为没有配置文件 为您的应用程序找到了,或者因为没有端点元素 可以在客户端元素中找到与此合同相匹配的内容。
我使用的配置文件是:
<?xml version="1.0"?>
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IMyDataService" 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="Transport">
<transport clientCredentialType="Ntlm" proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint
address="https://integrationservices-dev.thecompany.com/MyDataService/MyDataService.svc"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IMyDataService"
contract="MyDataService.IMyDataService"
name="BasicHttpBinding_IMyDataService" />
</client>
</system.serviceModel>
</configuration>
我知道我可以初始化绑定和端点并将它们提供给MyDataServiceClient(Binding, Endpoint) 构造函数。但我宁愿不想编写自己的配置加载器逻辑。
有没有办法让构建客户端的代码知道加载的配置?如果没有,还有什么其他选择?
【问题讨论】:
标签: c# web-services wcf asynchronous configuration