轉自url:http://greatverve.cnblogs.com/archive/2011/11/30/silverlight-wcf-pub.htmlhtml
咱們在編寫Silverlight程序時,大多狀況下都須要藉助WCF來訪問咱們的後端數據庫,在開發過程當中訪問數據庫都是正常的,可是當把整個silverlight項目連同WCF發佈到IIS上以後,會遇到這樣一個問題:在host IIS的服務器上可以正常訪問數據庫,可是當經過client端執行程序時卻沒法訪問數據庫,也沒有錯誤信息,調用頁面都是正常的。web
應該有不少人都遇到了這個問題,在網上也有不少關於這個問題的解決方法,可是絕大部分都是從跨域訪問的角度來解決這個問題的,而後再嘗試添加了clientaccesspolicy.xml和crossdomain.xml這兩個配置文件,而且添加了IIS中MIME配置後,依然有不少人沒有解決,我就是其中之一,花了一些時間研究了一下WCF的原理,最後從新對WCF進行了配置檢查,更改了對WCF綁定和調用的方式,這才得以把問題完全解決,下面和你們一塊兒分享一下具體步驟。數據庫
檢查你的ServiceReferences.ClientConfig文件,這個文件是你在Silverlight程序中添加服務引用時自動生成的,檢查裏面綁定WCF的方式是否爲BasicHttpBinding,具體代碼以下:後端
- <configuration>
- <system.serviceModel>
- <bindings>
- <basicHttpBinding>
- <binding name="BasicHttpBinding_Gxpt_Service" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">
- <security mode="None"/>
- </binding>
- </basicHttpBinding>
- </bindings>
- <client>
- <endpoint address="http://localhost:9545/Gxpt_Service.svc" binding="basicHttpBinding"
- bindingConfiguration="BasicHttpBinding_Gxpt_Service" contract="GxptServiceReference.Gxpt_Service"
- name="BasicHttpBinding_Gxpt_Service" />
- </client>
- </system.serviceModel>
- </configuration>
在你的Web工程下找到web.config文件,一樣檢查裏面的WCF配置信息,綁定方式是否爲"basicHttpBinding"方式,而且契約的名稱是否填寫正確,其具體代碼以下:跨域
- <system.serviceModel>
- <behaviors>
- <serviceBehaviors>
- <behavior name="">
- <serviceMetadata httpGetEnabled="true" />
- <serviceDebug includeExceptionDetailInFaults="true" />
- <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
- </behavior>
- </serviceBehaviors>
- </behaviors>
- <bindings>
- <basicHttpBinding>
- <binding name="BasicHttpBinding_Gxpt_Service" maxBufferPoolSize="2147483647"
- maxReceivedMessageSize="2147483647" maxBufferSize="2147483647">
- <readerQuotas maxArrayLength="2147483647" maxBytesPerRead="2147483647"
- maxDepth="2147483647" maxNameTableCharCount="2147483647"
- maxStringContentLength="2147483647" />
- </binding>
- </basicHttpBinding>
- </bindings>
- <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
- multipleSiteBindingsEnabled="true" />
- <services>
- <service name="PubilshTest.Web.Gxpt_Service">
- <endpoint address="" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_Gxpt_Service"
- contract="PubilshTest.Web.Gxpt_Service" />
- <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
- </service>
- </services>
- </system.serviceModel>
最後,修改一下你調用WebService的代碼:服務器
- BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.None);
- binding.MaxReceivedMessageSize = int.MaxValue;
- binding.MaxBufferSize = int.MaxValue;
- GxptServiceReference.Gxpt_ServiceClient client = new GxptServiceReference.Gxpt_ServiceClient(binding, new EndpointAddress(
- new Uri(Application.Current.Host.Source, "../Gxpt_Service.svc")));
- client.GetDataAsync();
- client.GetDataCompleted += new EventHandler<GxptServiceReference.GetDataCompletedEventArgs>(client_GetDataCompleted);
OK,能夠再從新編譯以後發佈一下,別忘了加上必不可少的clientaccesspolicy.xml和crossdomain.xml這兩個文件,不然你是沒法經過IP訪問到WCF的。dom
但願你看完以後,煩惱多日的問題可以迎刃而解。
url