2012-08-08 76 views
0

我有一個未公開的Web服務,我需要使用用Visual C#編寫的客戶端來獲取此Web服務的版本。問題是我太過於菜鳥。試圖使C#客戶端與本地主機上的Web服務通信

我在Visual Studio中添加了服務引用,所以我得到了一個代理文件和output.config文件。

我從VS此行啓動類的新實例:

DentalScannerServiceClient client = new DentalScannerServiceClient(); 

所以我把這個在我的控制檯應用程序:

DentalScannerServiceClient client = new DentalScannerServiceClient(); 
client.GetSoftwareVersion(); 

得到錯誤「,沒有超載的方法'GetSoftwareVersion'接受0個參數「。 Intelisens告訴我這當我開始輸入client.GetSoftwareVersion:

狀態DentalScannerServiceClient.GetSoftwareVersion(出字符串版本

所以我試試這個代碼:

DentalScannerServiceClient client = new DentalScannerServiceClient(); 
    string oo; 
    client.GetSoftwareVersion(out oo); 

,然後打印字符串但是當我運行代碼時出現此錯誤:

「InvalidOperationException is unhandled」 「無法在ServiceModel客戶端配置部分中找到引用合同'IDentalScannerService'的默認端點元素。這可能是因爲沒有找到適用於您的應用程序的配置文件,或者因爲在客戶端元素中找不到匹配此合同的端點元素。 ?

任何想法如何解決這個問題或者從哪裏開始尋找我感謝所有幫助也許這是一些簡單用C#我一點經驗以及

的app.config:。

<?xml version="1.0"?> 
<configuration> 
<configSections> 
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> 
     <section name="ThisIsTest.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/> 
    </sectionGroup> 
</configSections> 
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup><applicationSettings> 
     <ThisIsTest.Properties.Settings> 
      <setting name="ThisIsTest_localhost_DentalScannerService" serializeAs="String"> 
       <value>http://localhost:8731/DentalServiceLib/DentalScannerService/</value> 
      </setting> 
     </ThisIsTest.Properties.Settings> 
    </applicationSettings> 
    <system.serviceModel> 
     <bindings> 
      <basicHttpBinding> 
       <binding name="BasicEndPoint" 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://localhost:8731/DentalServiceLib/DentalScannerService/" 
       binding="basicHttpBinding" bindingConfiguration="BasicEndPoint" 
       contract="Scanner.IDentalScannerService" name="BasicEndPoint" /> 
     </client> 
    </system.serviceModel> 
</configuration> 

output.config(得到這個從Wsdl.exe用,只是做了項目 - >添加現有項添加它:

<?xml version="1.0" encoding="utf-8"?> 
<configuration> 
    <system.serviceModel> 
     <bindings> 
      <basicHttpBinding> 
       <binding name="BasicEndPoint" 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://localhost:8731/DentalServiceLib/DentalScannerService/" 
       binding="basicHttpBinding" bindingConfiguration="BasicEndPoint" 
       contract="IDentalScannerService" name="BasicEndPoint" /> 
     </client> 
    </system.serviceModel> 
</configuration> 
+1

您是否在dll中編寫代碼?如果是這樣,您需要將配置複製到應用程序配置文件。 – 2012-08-08 14:39:50

+0

你可以發佈你的配置文件嗎? – retslig 2012-08-08 19:28:52

+0

我在一個基本的Program.cs,控制檯應用程序中編寫代碼。 – 2012-08-09 07:34:16

回答

0

因爲我無法得到這種方法工作,我做到了另一種方式:

  1. 使用WSDL所產生的.wsdl文件。exe文件在SDK
  2. 下載的soapUI,並開始一個新的項目與WSDL文件作爲初始文件
  3. 的soapUI創造了所有的Web服務
  4. 測試在soapUI的不同調用對象的SOAP請求並得到響應住
  5. 推門進去,C#VS,使我的控制檯應用程序第一次啓動Web服務,然後使用HttpWebRequest的發送簡單的SOAP請求
  6. 賓果

這種方法的效果非常好!

0

這很可能是由於端點不止一次出現在您的配置文件中。找到部分<system.serviceModel><client></client></system.serviceModel>並檢查重複項(基於名稱)

如果客戶端部分不存在於您的配置文件中,您將需要添加它。 按照這個模板

<configuration> 
    <system.serviceModel> 
     <client> 
      <endpoint address="<address here>" binding="basicHttpBinding" contract="<full qualifeid class name to client interface>" name="<some name here>" /> 
     </client> 
    </system.serviceModel> 
</configuration> 
+0

那麼,我有兩個配置文件。 app.config和output.config。那是問題嗎? (在兩個客戶端標記) – 2012-08-09 07:35:28

+0

唯一一個重要的是他app.config爲可執行文件或web.config爲您的網站運行 – iamkrillin 2012-08-09 13:08:10