在silverlight中使用webService如果你兩次以上刪除同一個服務同時再添加該服務的情況下,程式在調試階段會報錯。錯誤提示為:
An endpoint configuration section for contract 'ServiceReference1.Service' could not be loaded because more than one endpoint configuration for that contract was found. Please indicate the preferred endpoint configuration section by name.
因為之前沒接觸過WSDL檔案,對於WSDL檔案報錯還是有點束手無策。我就直接複製這個錯誤到google中搜尋,後來發現搜尋結果都是WCF的樣式,還是看不懂。一段時間後我再次讀這個錯誤,這個錯誤說
because more than one endpoint configuration for that contract was found
代表有多個終結點存在,需要顯示指定使用哪個。再參考WCF中的解決方案(在此不羅列)於是我找到webService中的設定檔(注意是設定檔.svcinfo為尾碼,而不是WSDL檔案)webService在vs中顯示的樣式如
查看該設定檔如下
<?xml version="1.0" encoding="utf-8"?><configurationSnapshot xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="urn:schemas-microsoft-com:xml-wcfconfigurationsnapshot"> <behaviors /> <bindings> <binding digest="System.ServiceModel.Configuration.BasicHttpBindingElement, System.ServiceModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089:<?xml version="1.0" encoding="utf-16"?><Data maxBufferSize="2147483647" name="CompareChartServiceSoap11Binding5"><security mode="None" /></Data>" bindingType="basicHttpBinding" name="CompareChartServiceSoap11Binding5" /> </bindings> <endpoints> <endpoint normalizedDigest="<?xml version="1.0" encoding="utf-16"?><Data address="http://localhost:8080/axis2/services/CompareChartService" binding="basicHttpBinding" bindingConfiguration="CompareChartServiceSoap11Binding5" contract="CompareChartService.CompareChartServicePortType" name="CompareChartServiceHttpSoap11Endpoint5" />" digest="<?xml version="1.0" encoding="utf-16"?><Data address="http://localhost:8080/axis2/services/CompareChartService" binding="basicHttpBinding" bindingConfiguration="CompareChartServiceSoap11Binding5" contract="CompareChartService.CompareChartServicePortType" name="CompareChartServiceHttpSoap11Endpoint5" />" contractName="CompareChartService.CompareChartServicePortType" name="CompareChartServiceHttpSoap11Endpoint5" /> </endpoints></configurationSnapshot>
這個檔案是沒問題的,要解決該錯誤就得在原來聲明終結點的時給建構函式指定一個參數
原來使用的是預設建構函式
ServiceEndpoint ep = new PAS.I1Service.CompareChartService.CompareChartServicePortTypeClient().Endpoint;
現在則在建構函式中明確endpoint的名稱
ServiceEndpoint ep = new PAS.I1Service.CompareChartService.CompareChartServicePortTypeClient("CompareChartServiceHttpSoap11Endpoint5").Endpoint;
從此次錯誤中發現,很多新錯誤確實從來都沒接觸過,此時不要急於地去搜尋解決方案,而是先完全理解錯誤是什麼。這樣解決錯誤的效率就會高一些!