当前位置:web 服务器 > IIS >>

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

 

我们在编写Silverlight程序时,大多情况下都需要借助WCF来访问我们的后端数据库,在开发过程中访问数据库都是正常的,但是当把整个silverlight项目连同WCF发布到IIS上之后,会遇到这样一个问题:在host IIS的服务器上能够正常访问数据库,但是当通过client端执行程序时却无法访问数据库,也没有错误信息,调用页面都是正常的。

应该有很多人都遇到了这个问题,在网上也有很多关于这个问题的解决方法,但是绝大部分都是从跨域访问的角度来解决这个问题的,然后再尝试添加了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"方式,并且契约的名称是否填写正确,其具体代码如下:

www.zzzyk.com

<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的。

希望你看完之后,烦恼多日的问题能够迎刃而解

摘自 徐悦 Tech Blog

补充:Web开发 , ASP.Net ,
Apache
IIS
Nginx
Tomcat
如果你遇到web 服务器难题:
请访问www.zzzyk.com 试试
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,