簡體   English   中英

使用映射的配置文件連接到Web服務

[英]Connect to a web service using a mapped configuration file

我們有一個運行異步的CRM2011插件,該插件需要連接到Web服務以獲取一些丟失的數據。 我試圖解決此問題的方法是使用通過“添加服務參考”制作的Web服務客戶端。

但是對於配置,我不想對端點和綁定進行硬編碼。 所以我想我會像這樣使用OpenMappedExeConfiguration加載Web服務的配置。

var config = ConfigurationManager.OpenMappedExeConfiguration(new ExeConfigurationFileMap{ ExeConfigFilename = @"c:\Path\to\config.xml"}, ConfigurationUserLevel.None);

配置文件已加載...但是客戶端不可見。 因為打電話

var client = new MyDataServiceClient();

會拋出

InvalidOperationException:

在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)構造函數。 但是我寧願不想編寫自己的配置加載程序邏輯。

有沒有一種方法可以使構造客戶端的代碼知道已加載的配置? 如果沒有,還有哪些其他選擇?

您正在嘗試讀取exe配置,但您使用的是Web Context,我認為您必須使用OpenWebConfiguration

簡而言之,此命令將Web應用程序配置文件作為配置對象打開。

或者,您可以將配置添加到CRM網站上的web.config中

請參閱此Stackoverflow文章

參見MSDN

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM