zoukankan      html  css  js  c++  java
  • WCF 小程序案例以及序列化的使用

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.Serialization;
    using System.ServiceModel;
    using System.ServiceModel.Channels;
    using System.Text;
    using System.Threading.Tasks;

    namespace ConsoleTest
    {
    class Program
    {
    static void Main(string[] args)
    {
    //发布服务端的主机服务
    string url = "http://localhost:8080";
    ServiceHost sh = new ServiceHost(typeof(MyService));
    Binding bind = new BasicHttpBinding();
    sh.AddServiceEndpoint(typeof(IService), bind, url);
    sh.Open();

    //客户端发送消息,使用服务端的服务
    ChannelFactory<IService> cf = new ChannelFactory<IService>(bind);
    EndpointAddress ea = new EndpointAddress(url);
    IService iservice = cf.CreateChannel(ea);
    Student ss = iservice.GetStudent();
    Console.WriteLine(ss.StuID);
    Console.WriteLine(ss.Name);
    Console.WriteLine(ss.Age);
    Console.Read();
    }
    }

    /// <summary>
    /// 定义服务的协定接口
    /// </summary>
    [ServiceContract]
    public interface IService
    {
    [OperationContract]
    Student GetStudent();
    }

    /// <summary>
    /// 实现服务接口
    /// </summary>
    public class MyService : IService
    {
    public Student GetStudent()
    {
    Student s = new Student();
    s.StuID = 123456;
    s.Name = "哈哈哈哈";
    s.Age = 20;
    return s;
    }
    }

    /// <summary>
    /// 序列化
    /// </summary>
    [DataContract]
    public class Student
    {
    private long stuID;
    [DataMember]
    public long StuID
    {
    get { return stuID; }
    set { stuID = value; }
    }

    private string name;
    [DataMember]
    public string Name
    {
    get { return name; }
    set { name = value; }
    }

    private int age;
    [DataMember]
    public int Age
    {
    get { return age; }
    set { age = value; }
    }

    /// <summary>
    /// 反序列化时,执行此方法
    /// </summary>
    /// <param name="ss"></param>
    [OnSerializing]
    public void hhha(StreamingContext ss)
    {
    this.Age = this.Age == 20 ? 50 : 88;
    }

    }


    }

  • 相关阅读:
    9.1做JS的题目(2)
    9.1做JS的题目
    8.31做JS的题目
    8.30做JS的题目
    扫码跳转微信小程序(服务端微信API生成二维码)
    扫码跳转微信小程序(微信公众平台配置测试二维码)
    项目配置:maven下载与配置、tomcat下载与配置
    Java基础:常用工具_API
    Java基础: 抽象类、接口、final关键字、static关键字
    java基础: 封装、继承、多态
  • 原文地址:https://www.cnblogs.com/1175429393wljblog/p/6637151.html
Copyright © 2011-2022 走看看