Windows Phone访问web service和WCF服务

简介:

Windows phone也像传统的.net应用一样,可以方便的调用web service和WCF服务。

首先创建一个web service服务,因为.NET Framework 4的版本没有Web Service模板,所以选择.NET Framework 3.5来创建“ASP.NET Web服务应用程序”:

 

Service.cs中的Service的类中写如下方法:

 


 
 
  1. [WebMethod]  
  2.    public byte[] GetCity()  
  3.    {  
  4.        System.IO.MemoryStream m = new System.IO.MemoryStream();  
  5.        System.Drawing.Bitmap bp = new System.Drawing.Bitmap(Server.MapPath("~")+@"/a.jpg");  
  6.        bp.Save(m, System.Drawing.Imaging.ImageFormat.Jpeg);  
  7.        return m.GetBuffer();  
  8.    }  
 

 

.NET Framework 4下创建“WCF服务应用程序”:

在IService1.cs接口中添加如下方法:


 
 
  1. [OperationContract]  
  2.  byte[] GetCity();  

在子类Service1中添加如下方法:


 
 
  1. public byte[] GetCity()  
  2. {   
  3.       System.IO.MemoryStream m = new System.IO.MemoryStream();  
  4.       System.Drawing.Bitmap bp = new System.Drawing.Bitmap(@"C:\Users\GSW\Documents\Visual Studio 2010\Projects\WebSite4\WcfService1\a.jpg");  
  5.       bp.Save(m, System.Drawing.Imaging.ImageFormat.Jpeg);  
  6.       return m.GetBuffer();  
  7.   }  

创建Windows Phone, “添加服务引用”,引入web service或wcf服务,引用服务默认名称为“ServiceReference1”,的页面后台添加代码如下:


 
 
  1. //定义服务引用对象  
  2. ServiceReference1.Service1Client serviceclient;          
  3.         public MainPage()  
  4.         {  
  5.             InitializeComponent();  
  6.             serviceclient = new ServiceReference1.Service1Client();  
  7. //为异常完成事件添加回调方法              
  8. serviceclient.GetCityCompleted += new EventHandler<ServiceReference1.GetCityCompletedEventArgs>(serviceclientc_GetCityCompleted);  
  9.  
  10.         }  
  11.         private void button1_Click(object sender, RoutedEventArgs e)  
  12.         {  
  13.            //调用异常方法  
  14.             serviceclient.GetCityAsync();  
  15.         }  
  16.  
  17.         void serviceclient_GetCityCompleted(object sender, ServiceReference1.GetCityCompletedEventArgs e)  
  18.         {  
  19. //回调方法中完成图片的加载显示  
  20.             byte[] buf = e.Result;  
  21.             System.IO.MemoryStream stream = new System.IO.MemoryStream(buf);  
  22.             BitmapImage img = new BitmapImage();  
  23.             img.SetSource(stream);  
  24.             image1.Source = img;  
  25.         }  

不管是web service还是wcf服务,Windows Phone中使用方法都是一至的,采用异步调用方式,这样用户体验要好一些。



















本文转自桂素伟51CTO博客,原文链接: http://blog.51cto.com/axzxs/866453,如需转载请自行联系原作者

相关文章
|
5月前
|
开发框架 监控 安全
Windows Defender 导致 Web IIS 服务异常停止排查
某日凌晨IIS服务异常停止,经查为Windows Defender安全补丁KB2267602触发引擎更新,导致系统资源波动,进而引发应用池回收。确认非人为操作,系统无重启。通过分析日志与监控,定位原因为Defender更新后扫描加重负载。解决方案:将IIS及.NET相关路径添加至Defender排除列表,避免业务影响。
668 116
|
5月前
|
Java 测试技术 网络安全
Burp Suite Professional 2025.10 for Windows x64 - 领先的 Web 渗透测试软件
Burp Suite Professional 2025.10 for Windows x64 - 领先的 Web 渗透测试软件
268 0
Burp Suite Professional 2025.10 for Windows x64 - 领先的 Web 渗透测试软件
|
8月前
|
安全 Linux iOS开发
Burp Suite Professional 2025.7 (macOS, Linux, Windows) - Web 应用安全、测试和扫描
Burp Suite Professional 2025.7 (macOS, Linux, Windows) - Web 应用安全、测试和扫描
695 0
Burp Suite Professional 2025.7 (macOS, Linux, Windows) - Web 应用安全、测试和扫描
|
10月前
|
人工智能 安全 网络安全
Burp Suite Professional 2025.5 for Windows x64 - 领先的 Web 渗透测试软件
Burp Suite Professional 2025.5 for Windows x64 - 领先的 Web 渗透测试软件
513 4
Burp Suite Professional 2025.5 for Windows x64 - 领先的 Web 渗透测试软件
|
10月前
|
安全 Devops 测试技术
AppSpider 7.5.018 for Windows - Web 应用程序安全测试
AppSpider 7.5.018 for Windows - Web 应用程序安全测试
213 0
AppSpider 7.5.018 for Windows - Web 应用程序安全测试
|
人工智能 Linux iOS开发
Burp Suite Professional 2025.2 (macOS, Linux, Windows) - Web 应用安全、测试和扫描
Burp Suite Professional 2025.2 (macOS, Linux, Windows) - Web 应用安全、测试和扫描
727 12
Burp Suite Professional 2025.2 (macOS, Linux, Windows) - Web 应用安全、测试和扫描
|
安全 JavaScript Java
AppSpider Pro 7.5.015 for Windows - Web 应用程序安全测试
AppSpider Pro 7.5.015 for Windows - Web 应用程序安全测试
231 12
AppSpider Pro 7.5.015 for Windows - Web 应用程序安全测试
|
自然语言处理 安全 测试技术
HCL AppScan Standard 10.8.0 (Windows) - Web 应用程序安全测试
HCL AppScan Standard 10.8.0 (Windows) - Web 应用程序安全测试
833 0
HCL AppScan Standard 10.8.0 (Windows) - Web 应用程序安全测试
|
Linux iOS开发 MacOS
Gitea Enterprise 23.4.0 (Linux, macOS, Windows) - 本地部署的企业级 Git 服务
Gitea Enterprise 23.4.0 (Linux, macOS, Windows) - 本地部署的企业级 Git 服务
379 0
Gitea Enterprise 23.4.0 (Linux, macOS, Windows) - 本地部署的企业级 Git 服务
|
监控 搜索推荐 开发工具
2025年1月9日更新Windows操作系统个人使用-禁用掉一下一些不必要的服务-关闭占用资源的进程-禁用服务提升系统运行速度-让电脑不再卡顿-优雅草央千澈-长期更新
2025年1月9日更新Windows操作系统个人使用-禁用掉一下一些不必要的服务-关闭占用资源的进程-禁用服务提升系统运行速度-让电脑不再卡顿-优雅草央千澈-长期更新
2481 2
2025年1月9日更新Windows操作系统个人使用-禁用掉一下一些不必要的服务-关闭占用资源的进程-禁用服务提升系统运行速度-让电脑不再卡顿-优雅草央千澈-长期更新