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

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

wcf服务接口IWeatherService.cs

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text; namespace WcfServiceWeather
{
// 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IWeatherService”。
[ServiceContract]
public interface IWeatherService
{
[OperationContract]
string[] GetCityWeather(string CityName);
}
}

wcf服务实现类WeatherService.cs

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text; namespace WcfServiceWeather
{
// 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码、svc 和配置文件中的类名“WeatherService”。
// 注意: 为了启动 WCF 测试客户端以测试此服务,请在解决方案资源管理器中选择 WeatherService.svc 或 WeatherService.svc.cs,然后开始调试。
public class WeatherService : IWeatherService
{
public string[] GetCityWeather(string CityName)
{
ServiceRefWeather.WeatherWebServiceSoapClient client = new ServiceRefWeather.WeatherWebServiceSoapClient("WeatherWebServiceSoap");
string[] cityNameWeather = client.getWeatherbyCityName(CityName);
return cityNameWeather;
}
}
}

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

 <?xml version="1.0" encoding="utf-8"?>
<configuration> <appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5"/>
</system.web>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="WeatherWebServiceSoap" />
</basicHttpBinding>
<customBinding>
<binding name="WeatherWebServiceSoap12">
<textMessageEncoding messageVersion="Soap12" />
<httpTransport />
</binding>
</customBinding>
</bindings>
<client>
<endpoint address="http://www.webxml.com.cn/WebServices/WeatherWebService.asmx"
binding="basicHttpBinding" bindingConfiguration="WeatherWebServiceSoap"
contract="ServiceRefWeather.WeatherWebServiceSoap" name="WeatherWebServiceSoap" />
<endpoint address="http://www.webxml.com.cn/WebServices/WeatherWebService.asmx"
binding="customBinding" bindingConfiguration="WeatherWebServiceSoap12"
contract="ServiceRefWeather.WeatherWebServiceSoap" name="WeatherWebServiceSoap12" />
</client>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- 为避免泄漏元数据信息,请在部署前将以下值设置为 false -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<!-- 要接收故障异常详细信息以进行调试,请将以下值设置为 true。在部署前设置为 false 以避免泄漏异常信息 -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<!--
若要在调试过程中浏览 Web 应用程序根目录,请将下面的值设置为 True。
在部署之前将该值设置为 False 可避免泄露 Web 应用程序文件夹信息。
-->
<directoryBrowse enabled="true"/>
</system.webServer> </configuration>

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

 ServiceReference1.WeatherServiceClient client = new ServiceReference1.WeatherServiceClient();
client.GetCityWeatherCompleted += (s, e) =>
{
try
{
if (e.Error == null)
{
var result=e.Result;//此处处理结果就ok了
}
catch (Exception ex)
{
lbltitle1.Content = ex.Message;
} };
client.GetCityWeatherAsync("北京");

3.添加跨域文件clientaccesspolicy.xml

 <?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 include-subpaths="true" path="/"/>
</grant-to>
</policy>
</cross-domain-access>
</access-policy>

crossdomain.xml

 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd" >
<cross-domain-policy>
<site-control permitted-cross-domain-policies="all" />
<allow-access-from domain="*" />
<allow-http-request-headers-from domain="*" headers="*"/>
</cross-domain-policy>

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

 <configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IWeatherService" maxBufferSize=""
maxReceivedMessageSize="">
<security mode="None" />
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://192.168.0.161:8080/bjsw/WCF/WeatherService.svc"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IWeatherService"
contract="ServiceReference1.IWeatherService" name="BasicHttpBinding_IWeatherService" />
</client>
</system.serviceModel>
</configuration>

在客户端直接调用就ok了

  

关于Silverlight调用天气预报接口问题的更多相关文章

  1. webservice通信调用天气预报接口实例

    转载:http://www.cnblogs.com/warrior4236/p/5668449.html 一:环境搭建 1:新建一个java project工程weatherInf 2:引入相应的ja ...

  2. 5. webservice通信调用天气预报接口实例

    转自:https://blog.csdn.net/xiejuan6105/article/details/78452605 一:环境搭建 1:新建一个java project工程weatherInf ...

  3. java调用天气预报接口案例

    免费天气接口:http://mobile.weather.com.cn/data/sk/城市ID.html 例如: http://mobile.weather.com.cn/data/sk/10124 ...

  4. 基于JAVA的全国天气预报接口调用示例

    step1:选择本文所示例的接口"全国天气预报接口" url:https://www.juhe.cn/docs/api/id/39/aid/87step2:每个接口都需要传入一个参 ...

  5. 【转载】C#调用国家气象局天气预报接口

    一.需求 最近,刚好项目中有天气预报查询功能的需求,要求录入城市名称,获取该城市今日天气信息及相关气象生活辅助信息等. 例如:查询北京市天气 结果为: 今日北京天气:15℃~5℃ 阵雨转阴,北风3-4 ...

  6. C#调用国家气象局天气预报接口

    原文:C#调用国家气象局天气预报接口 一.需求 最近,刚好项目中有天气预报查询功能的需求,要求录入城市名称,获取该城市今日天气信息及相关气象生活辅助信息等. 例如:查询北京市天气 结果为: 今日北京天 ...

  7. Java调用webservice接口方法

                             java调用webservice接口   webservice的 发布一般都是使用WSDL(web service descriptive langu ...

  8. Silverlight调用网站项目的Session

    项目中遇到Silverlight调网站Session的问题了,试了几种方法,用这种方法获取到了,如果有不对不恰当的地方,还望各路大神给指正出来. 解决方法: 1.Silverlight调用网站的接口 ...

  9. PHP调用API接口实现天气查询功能

    天气预报查询接口API,在这里我使用的是国家气象局天气预报接口 使用较多的还有:新浪天气预报接口.百度天气预报接口.google天气接口.Yahoo天气接口等等. 1.查询方式 根据地名查询各城市天气 ...

随机推荐

  1. WPF 之 自定义窗体标题栏

    在WPF中自定义窗体标题栏,首先需要将窗体的WindowStyle属性设置为None,隐藏掉WPF窗体的自带标题栏.然后可以在窗体内部自定义一个标题栏. 例如,标题栏如下: <WrapPanel ...

  2. Mispelling4

    Problem Description Misspelling is an art form that students seem to excel at. Write a program that ...

  3. Unity3D 使用 UI 的 Grid Layout Group 组件。

    1.首先创建一个容器,用于存放列表项的内容. 这里使用 Panel 来做为容器. 这里要注意! “Grid Layout Group”是要增加在容器的游戏对象里. 同时,只有容器对象的子对象才有排列效 ...

  4. DPKG命令与软件安装、APT

    ====Linux软件包==== Linux系统中,软件通常以源代码或者预编译包的形式提供. 软件的源代码通常需要编译为二进制代码才可使用,安装比较耗时.用户可以自行调节编译选项,决定需要的功能或组件 ...

  5. 重构6-Push Down Field(字段下移)

    与上移字段相反的重构是下移字段.同样,这也是一个无需多言的简单重构. public abstract class Task { protected String _resolution; } publ ...

  6. ubuntu下安装redis

    (1)进去 /usr/local目录下   cd /usr/local  若没有local这个文件夹则创建一个    sudo mkdir /usr/local     sudo chmod 777 ...

  7. Ubuntu14.04 搭建 node.js 环境(Binaries方式)

    从官网下载 http://nodejs.org/download/ Linux Binaries (.tar.gz)  下载下来的是node-v0.10.29-linux-x64.tar.gz文件 解 ...

  8. 对象(类)的封装方式(面向对象的js基本知识)

    上一个月一直忙于项目,没写过笔记,今天稍微空下来了一点 前几天在写项目的时候关于怎么去封装每一个组件的时候思考到几种方式,这里总结一下: 1.构造函数方式(类似java写类的方式):把所有的属性和方法 ...

  9. bind() to 0.0.0.0:80 failed (98: Address already in use)

    You can kill it using: sudo fuser -k 80/tcp And then try restarting nginx again: service nginx start

  10. AngularJS尝鲜一

    第一个小例子,体验一下: <!DOCTYPE html> <html> <head> <title>Index</title> </h ...