(转)发布Silverlight+WCF程序到IIS后,客户端访问数据库失败的解决方案

转自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,具体代码以下:后端

  1. <configuration>
  2. <system.serviceModel>
  3. <bindings>
  4. <basicHttpBinding>
  5. <binding name="BasicHttpBinding_Gxpt_Service" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">
  6. <security mode="None"/>
  7. </binding>
  8. </basicHttpBinding>
  9. </bindings>
  10. <client>
  11. <endpoint address="http://localhost:9545/Gxpt_Service.svc" binding="basicHttpBinding"
  12. bindingConfiguration="BasicHttpBinding_Gxpt_Service" contract="GxptServiceReference.Gxpt_Service"
  13. name="BasicHttpBinding_Gxpt_Service" />
  14. </client>
  15. </system.serviceModel>
  16. </configuration>


在你的Web工程下找到web.config文件,一样检查里面的WCF配置信息,绑定方式是否为"basicHttpBinding"方式,而且契约的名称是否填写正确,其具体代码以下:跨域

  1. <system.serviceModel>
  2. <behaviors>
  3. <serviceBehaviors>
  4. <behavior name="">
  5. <serviceMetadata httpGetEnabled="true" />
  6. <serviceDebug includeExceptionDetailInFaults="true" />
  7. <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
  8. </behavior>
  9. </serviceBehaviors>
  10. </behaviors>
  11. <bindings>
  12. <basicHttpBinding>
  13. <binding name="BasicHttpBinding_Gxpt_Service" maxBufferPoolSize="2147483647"
  14. maxReceivedMessageSize="2147483647" maxBufferSize="2147483647">
  15. <readerQuotas maxArrayLength="2147483647" maxBytesPerRead="2147483647"
  16. maxDepth="2147483647" maxNameTableCharCount="2147483647"
  17. maxStringContentLength="2147483647" />
  18. </binding>
  19. </basicHttpBinding>
  20. </bindings>
  21. <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
  22. multipleSiteBindingsEnabled="true" />
  23. <services>
  24. <service name="PubilshTest.Web.Gxpt_Service">
  25. <endpoint address="" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_Gxpt_Service"
  26. contract="PubilshTest.Web.Gxpt_Service" />
  27. <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
  28. </service>
  29. </services>
  30. </system.serviceModel>


最后,修改一下你调用WebService的代码:服务器

  1. BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.None);
  2. binding.MaxReceivedMessageSize = int.MaxValue;
  3. binding.MaxBufferSize = int.MaxValue;
  4. GxptServiceReference.Gxpt_ServiceClient client = new GxptServiceReference.Gxpt_ServiceClient(binding, new EndpointAddress(
  5. new Uri(Application.Current.Host.Source, "../Gxpt_Service.svc")));
  6. client.GetDataAsync();
  7. client.GetDataCompleted += new EventHandler<GxptServiceReference.GetDataCompletedEventArgs>(client_GetDataCompleted);

 

OK,能够再从新编译以后发布一下,别忘了加上必不可少的clientaccesspolicy.xml和crossdomain.xml这两个文件,不然你是没法经过IP访问到WCF的。dom

但愿你看完以后,烦恼多日的问题可以迎刃而解。
url

相关文章
相关标签/搜索