关于Silverlight调用天气预报接口问题

问题:因Silverlight客户端不能直接调用webservice接口(外网天气接口),调用会出现跨域访问的问题,即使添加了跨域文件也不好使。解决方法如下

解决方法一:1.在服务端建立一个wcf服务端,在wcf里调用webservice接口(外网天气接口)

wcf服务接口IWeatherService.cs

关于Silverlight调用天气预报接口问题
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Runtime.Serialization;
 5 using System.ServiceModel;
 6 using System.Text;
 7 
 8 namespace WcfServiceWeather
 9 {
10     // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IWeatherService”。
11     [ServiceContract]
12     public interface IWeatherService
13     {
14         [OperationContract]
15         string[] GetCityWeather(string CityName);
16     }
17 }
View Code

wcf服务实现类WeatherService.cs

关于Silverlight调用天气预报接口问题
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Runtime.Serialization;
 5 using System.ServiceModel;
 6 using System.Text;
 7 
 8 namespace WcfServiceWeather
 9 {
10     // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码、svc 和配置文件中的类名“WeatherService”。
11     // 注意: 为了启动 WCF 测试客户端以测试此服务,请在解决方案资源管理器中选择 WeatherService.svc 或 WeatherService.svc.cs,然后开始调试。
12     public class WeatherService : IWeatherService
13     {
14         public string[] GetCityWeather(string CityName)
15         {
16             ServiceRefWeather.WeatherWebServiceSoapClient client = new ServiceRefWeather.WeatherWebServiceSoapClient("WeatherWebServiceSoap");
17             string[] cityNameWeather = client.getWeatherbyCityName(CityName);
18             return cityNameWeather;
19         }
20     }
21 }
View Code

添加完成后预览服务,将会在配置文件中自动生成相应的配置system.serviceModel

关于Silverlight调用天气预报接口问题
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <configuration>
 3 
 4   <appSettings>
 5     <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
 6   </appSettings>
 7   <system.web>
 8     <compilation debug="true" targetFramework="4.5" />
 9     <httpRuntime targetFramework="4.5"/>
10   </system.web>
11   <system.serviceModel>
12     <bindings>
13       <basicHttpBinding>
14         <binding name="WeatherWebServiceSoap" />
15       </basicHttpBinding>
16       <customBinding>
17         <binding name="WeatherWebServiceSoap12">
18           <textMessageEncoding messageVersion="Soap12" />
19           <httpTransport />
20         </binding>
21       </customBinding>
22     </bindings>
23     <client>
24       <endpoint address="http://www.webxml.com.cn/WebServices/WeatherWebService.asmx"
25         binding="basicHttpBinding" bindingConfiguration="WeatherWebServiceSoap"
26         contract="ServiceRefWeather.WeatherWebServiceSoap" name="WeatherWebServiceSoap" />
27       <endpoint address="http://www.webxml.com.cn/WebServices/WeatherWebService.asmx"
28         binding="customBinding" bindingConfiguration="WeatherWebServiceSoap12"
29         contract="ServiceRefWeather.WeatherWebServiceSoap" name="WeatherWebServiceSoap12" />
30     </client>
31     <behaviors>
32       <serviceBehaviors>
33         <behavior>
34           <!-- 为避免泄漏元数据信息,请在部署前将以下值设置为 false -->
35           <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
36           <!-- 要接收故障异常详细信息以进行调试,请将以下值设置为 true。在部署前设置为 false 以避免泄漏异常信息 -->
37           <serviceDebug includeExceptionDetailInFaults="false"/>
38         </behavior>
39       </serviceBehaviors>
40     </behaviors>
41     <protocolMapping>
42         <add binding="basicHttpsBinding" scheme="https" />
43     </protocolMapping>    
44     <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
45   </system.serviceModel>
46   <system.webServer>
47     <modules runAllManagedModulesForAllRequests="true"/>
48     <!--
49         若要在调试过程中浏览 Web 应用程序根目录,请将下面的值设置为 True。
50         在部署之前将该值设置为 False 可避免泄露 Web 应用程序文件夹信息。
51       -->
52     <directoryBrowse enabled="true"/>
53   </system.webServer>
54 
55 </configuration>
View Code

2.Silverlight客户端添加服务引用(此处添加的是wcf的服务引用)

关于Silverlight调用天气预报接口问题
 1 ServiceReference1.WeatherServiceClient client = new ServiceReference1.WeatherServiceClient();
 2                 client.GetCityWeatherCompleted += (s, e) =>
 3                 {
 4                     try
 5                     {
 6                         if (e.Error == null)
 7                         {
 8                             var result=e.Result;//此处处理结果就ok了
 9                          }
10                     catch (Exception ex)
11                     {
12                         lbltitle1.Content = ex.Message;
13                     }
14 
15                 };
16                 client.GetCityWeatherAsync("北京");
View Code

3.添加跨域文件clientaccesspolicy.xml

关于Silverlight调用天气预报接口问题
 1 <?xml version="1.0" encoding="utf-8" ?>    
 2 <access-policy>    
 3   <cross-domain-access>    
 4     <policy>    
 5       <allow-from http-request-headers="*">    
 6         <domain uri="*"/>    
 7       </allow-from>    
 8       <grant-to>    
 9         <resource include-subpaths="true" path="/"/>    
10       </grant-to>    
11     </policy>    
12   </cross-domain-access>    
13 </access-policy>
View Code

crossdomain.xml

关于Silverlight调用天气预报接口问题
1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd" >
3 <cross-domain-policy>
4   <site-control permitted-cross-domain-policies="all" />
5   <allow-access-from domain="*" />
6   <allow-http-request-headers-from domain="*" headers="*"/>
7 </cross-domain-policy>
View Code

 

解决方法二:直接在Silverlight的web中添加wcf无法文件、添加(外网天气服务接口)处理天气接口,然后在客户端直接调用自己创建的wcf,此时客户端会产生一个ServiceReferences.ClientConfig的文件

关于Silverlight调用天气预报接口问题
 1 <configuration>
 2     <system.serviceModel>
 3         <bindings>
 4             <basicHttpBinding>
 5                 <binding name="BasicHttpBinding_IWeatherService" maxBufferSize="2147483647"
 6                     maxReceivedMessageSize="2147483647">
 7                     <security mode="None" />
 8                 </binding>
 9             </basicHttpBinding>
10         </bindings>
11         <client>
12             <endpoint address="http://192.168.0.161:8080/bjsw/WCF/WeatherService.svc"
13                 binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IWeatherService"
14                 contract="ServiceReference1.IWeatherService" name="BasicHttpBinding_IWeatherService" />
15         </client>
16     </system.serviceModel>
17 </configuration>
View Code

在客户端直接调用就ok了

 


  

 

 

 

关于Silverlight调用天气预报接口问题,布布扣,bubuko.com

关于Silverlight调用天气预报接口问题

上一篇:VisualStudio2012轻松把JSON数据转换到POCO的代码


下一篇:JS正则表达式获取分组内容实例