代码参考-->SilverlightWCF.Web SilverlightWCF 一.项目说明: 1.一个承载于网站的Silverlight解决方案 2.在网站工程里建立了一个Wcf,简单调用数据库 3.在Silverlight工程里添加该Wcf的引用,并调用该Wcf显示数据 ========================================================================================= 二.部署方法: 1.把网站工程生成一下 2.在网站工程上右键选择生成部署包(说明:包的位置可以在网站工程属性的打包/发布Web选项的创建包位置上看到) D:\VS2010Pro\SilverlightWCF.Web\obj\Debug\Package\PackageTmp 把这个目录在IIS上新建一个虚拟目录,然后选择这个虚拟目录,右键选择SilverlightWCFTestPage.html就可以浏览了 3.把Silverlight工程上的Wcf引用重新添加(注意:使用http://192.168.18.205:82/SilverlightWCF/wcfMain.svc 前往的方式,不是发现的方式,这个是部署到IIS上的虚拟目录的路径) 4.重新生成解决方案 5.在网站工程上右键选择重新生成部署包(主要是为了重新指向一下Wcf引用,让他指向IIS上的Wcf路径) 6.注意需要cmd命令行执行iisreset重启动IIS,防止没有应用最新的文件 ========================================================================================= 三.遇到的问题 1.单击按钮发现下拉框是有数据的,但是显示一片空白,检查xaml文件 <ComboBox.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <TextBlock Text="{Binding FName}" /> <TextBlock Text="," /> <TextBlock Text="{Binding FCaption}" /> </StackPanel> </DataTemplate> </ComboBox.ItemTemplate> 发现绑定没错 结果是Wcf定义的接口文件IwcfMain.cs ,FName和FCaption 属性没有加[DataMember]导致客户端无法读取该属性 [DataContract] public class FieldInfo { [DataMember] public string FName { get; set; } [DataMember] public string FCaption { get; set; } } 2.解决尝试向 URI“http://localhost:19464/wcfMain.svc”发出请求时出错。这可能是由于试图以跨域方式访问服务而又没有正确的跨域策略,或策略不适用于 SOAP 服务。您可能需要与该服务的所有者联系,以发布跨域策略文件并确保该文件允许发送 SOAP 相关的 HTTP 标头。出现此错误也可能是由于使用的是 Web 服务代理中的内部类型而没有使用 InternalsVisibletoAttribute 属性。有关详细信息,请参阅内部异常。 解决方法: 一、在WCF项目根目录下添加clientaccesspolicy.xml文件 Code <?xml version="1.0" encoding="utf-8" ?> <access-policy> <cross-domain-access> <policy> <allow-from http-request-headers="*"> <domain uri="*"/> </allow-from> <grant-to> <resource path="/" include-subpaths="true"/> </grant-to> </policy> </cross-domain-access> </access-policy> 二、在silverlight项目中添加一个中介类ServerManager.cs Code public class ServerManager { private static ServiceWcfClient servicePicture = new ServiceWcfClient(); internal static ServiceWcfClient GetPox() { if (servicePicture.State == System.ServiceModel.CommunicationState.Created) { servicePicture.Endpoint.Address = new System.ServiceModel.EndpointAddress("http://localhost:52207/ServiceWcf.svc"); return servicePicture; } else { return servicePicture; } } } 三、实例化实体类的时候一般是这样:ServiceWcfClient clientWcf = new ServiceWcfClient(); 换成:ServiceWcfClient clientWcf = ServerManager.GetPox(); 3.如果第2步都已经处理正确了,还提示跨域 则有可能是IIS没有重新启动的原因,可以在代码中加入提示框进行跟踪,验证代码是不是最新的 4.如果出现远程服务器返回错误 not Found 很可能是代码写错了 例如Pigeon项目 //sConnectionStr = System.Configuration.ConfigurationManager. // ConnectionStrings["PigeonConnectionString"] // .ConnectionString; 取这个连接串有错误 后来改成手工连接串就好了 sConnectionStr = "Data Source=192.168.18.205;Database=Pigeon;Uid=sa;Pwd=kicpassword"; 不够WCF返回的错误不好跟踪问题 5.遇到程序代码调试时可以正常读取web.config数据库连接串 System.Configuration.ConfigurationManager.ConnectionStrings["PigeonConnectionString"].ToString() 但部署到IIS上,却数据库连接失败,经过写日志跟踪发现读取的连接串为$(Replacabletoken_PigeonConnectionString-Web.config Connection String_0) 解决方法 需要修改项目文件,注意是项目文件(即:D:\VS2010Pro\Pigeon.Web\Pigeon.Web.csproj 在PropertyGroup里添加如下属性: <AutoparameterizationWebConfigConnectionStrings>False</AutoparameterizationWebConfigConnectionStrings> 这样就会正常生成自定义的连接串 以下是截取的片段 <?xml version="1.0" encoding="utf-8"?> <Project Toolsversion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <PropertyGroup> <AutoparameterizationWebConfigConnectionStrings>False</AutoparameterizationWebConfigConnectionStrings> <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。