当前位置:编程学习 > wap >>

WinPhone7使用调用WCF传递对象

很少做手机开发,今天没事,做了个小小的实验,一般来讲,我学东西,只要能连上后台数据,基本上就可以了,因为前台很多东西要么不难,要么需要的是创意和美工,这都是我的薄弱之处,也就没兴趣深入了.

WCF大家估计都必须交熟悉,代码如下:

[csharp]
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Runtime.Serialization; 
using System.ServiceModel; 
using System.ServiceModel.Web; 
using System.Text; 
 
namespace WcfService4 

    // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IService1”。  
    [ServiceContract] 
    public interface IService1 
    { 
 
        [OperationContract] 
        string GetData(int value); 
 
        [OperationContract] 
        CompositeType GetDataUsingDataContract(CompositeType composite); 
 
        // TODO: 在此添加您的服务操作  
    } 
 
 
    // 使用下面示例中说明的数据约定将复合类型添加到服务操作。  
    [DataContract] 
    public class CompositeType 
    { 
        bool boolValue = true; 
        string stringValue = "Hello "; 
 
        [DataMember] 
        public bool BoolValue 
        { 
            get { return boolValue; } 
            set { boolValue = value; } 
        } 
 
        [DataMember] 
        public string StringValue 
        { 
            get { return stringValue; } 
            set { stringValue = value; } 
        } 
    } 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace WcfService4
{
    // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IService1”。
    [ServiceContract]
    public interface IService1
    {

        [OperationContract]
        string GetData(int value);

        [OperationContract]
        CompositeType GetDataUsingDataContract(CompositeType composite);

        // TODO: 在此添加您的服务操作
    }


    // 使用下面示例中说明的数据约定将复合类型添加到服务操作。
    [DataContract]
    public class CompositeType
    {
        bool boolValue = true;
        string stringValue = "Hello ";

        [DataMember]
        public bool BoolValue
        {
            get { return boolValue; }
            set { boolValue = value; }
        }

        [DataMember]
        public string StringValue
        {
            get { return stringValue; }
            set { stringValue = value; }
        }
    }
}

直接在客户端添加服务引用,是一种方法,但这种方法有个问题,就是如果有复杂对象,就会出问题,没法生成服务所需要的代理代码.解决对象穿越问题,当然可以自己利用序列化和反序列化,中间利用字符串来传递,但这种方法工作量比较大.另外一种方式就是共享service服务接口和实体,这个非常方便,但问题是winphone和一般桌面的环境还是差很远,在wp7中引用实体解决还好点,要引用服务段的wcf契约接口问题很大,而且wp7里的servicemodel的版本和服务端的版本也不一致,调用也非常麻烦,最后找到一种利用silverlight的wcf ria service生成客户端代码工具来生成客户端代理得以解决,一般命令为:SlSvcUtil.exe 服务地址.把生成的文件引入到wp7工程即可用.生成的代码如下,其实大家可以学习它生成的代码,自己整一个通用的方法也是可行的:

 

 

[csharp]
//------------------------------------------------------------------------------  
// <auto-generated>  
//     此代码由工具生成。  
//     运行时版本:4.0.30319.17379  
//  
//     对此文件的更改可能会导致不正确的行为,并且如果  
//     重新生成代码,这些更改将会丢失。  
// </auto-generated>  
//------------------------------------------------------------------------------  
 
//   
// This code was auto-generated by SlSvcUtil, version 5.0.61118.0  
//   
namespace WcfService4 

    using System.Runtime.Serialization; 
     
     
    [System.Diagnostics.DebuggerStepThroughAttribute()] 
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")] 
    [System.Runtime.Serialization.DataContractAttribute(Name="CompositeType", Namespace="http://schemas.datacontract.org/2004/07/WcfService4")] 
    public partial class CompositeType : object 
    { 
         
        private bool BoolValueField; 
         
        private string StringValueField; 
         
        [System.Runtime.Serialization.DataMemberAttribute()] 
        public bool BoolValue 
        { 
            get 
     

补充:移动开发 , Windows Phone ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,