【问题标题】:Connect to a web service using a mapped configuration file使用映射的配置文件连接到 Web 服务
【发布时间】: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


    【解决方案1】:

    您正在尝试读取 exe 配置但您在 Web 上下文中,我认为您必须使用 OpenWebConfiguration

    简而言之,此命令将 Web 应用程序配置文件作为配置对象打开。

    或者,您可以将配置添加到 CRM 网站上的 web.config

    this Stackoverflow article

    MSDN

    【讨论】:

    • 插件正在异步运行。而且我相信它将使用 MSCRM Async 处理服务执行,这是一个 Windows 服务。
    • 我将在我们公司讨论我们是否会更改 crm 配置文件。问题是配置属于插件,最好保存在插件设置实体中。此外,我们有多个租户,我们最终可能不得不处理多种服务。
    • 我已经用异步信息更新了你的问题。在您的场景中,我认为最好使用带有 URL 参数的类构造函数。
    • 这里经过一番辩论。该插件本质上是通用的,并且应该能够根据插件的注册方式运行。所以同步和异步都是可以的。
    猜你喜欢
    • 2015-06-29
    • 2016-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-31
    • 2012-02-04
    • 1970-01-01
    相关资源
    最近更新 更多