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;
}

}

}

WCF 小程序案例以及序列化的使用的更多相关文章

  1. 微信小程序案例:获取微信访问用户的openid

    在微信开发项目中,获取openid是项目常遇的问题,本文通过主要讲解实现在微信小程序中如何获取用户的openid,案例实现非常简单 具体实现方法是通过登录接口获取登录凭证,然后通过request请求微 ...

  2. 微信小程序案例大全

    微信小程序demo:足球,赛事分析 小程序简易导航 小程序demo:办公审批 小程序Demo:电魔方 小程序demo:借阅伴侣 微信小程序demo:投票 微信小程序demo:健康生活 小程序demo: ...

  3. python flask豆瓣微信小程序案例

    项目步骤 定义首页模板index.html <!DOCTYPE html> <html lang="en"> <head> <meta c ...

  4. python flask框架学习(三)——豆瓣微信小程序案例(一)templates的使用,宏的使用,前端后台传数据,前端写python语句

    目录 一.templates的使用 (1)在templates里创建一个index.html (2)再在app.py里写 (3)展示效果 二.构建第一个电影评分 (1)准备好素材放进static里的i ...

  5. 【小程序案例】支付宝小程序-MQTT模器,IoT设备通过WSS接入阿里云IoT物联网平台

    支付宝小程序-MQTT模拟器通过WSS接入阿里云IoT物联网平台 小程序效果: 1. 准备工作 1.1 注册阿里云账号 开通阿里云账号,并通过支付宝实名认证 https://www.aliyun.co ...

  6. python flask框架学习(三)——豆瓣微信小程序案例(二)整理封装block,模板的继承

    我们所要实现的效果: 点击电影的更多,跳转到更多的电影页面:点击电视剧的更多,跳转到更多的电视剧页面. 三个页面的风格相同,可以设置一个模板,三个页面都继承这个模板 1.在指定模板之前,把css放在一 ...

  7. 微信小程序--学习的案例

    一.「程序秀」 「程序秀」这个小程序非常特别,因为它是一个可以帮助别人开发小程序的小程序.它以「即速应用」这个无需代码一键生成微信小程序的开发工具为基础,为广大开发爱好者提供小程序开发的最新资讯和培训 ...

  8. 个人也能申请微信小程序获得APPID和手机测试效果

    微信小程序昨晚火爆公测,我也第一时间注册了小程序账号开启公测之旅. 注册过程可以看文档:https://my.oschina.net/imhoodoo/blog/780901 进入后台之后我们其实主要 ...

  9. 史诗手册!微信小程序新手自学入门宝典!

    一.小程序官方指南 1:官方开发工具下载: https://mp.weixin.qq.com/debug/wxadoc/dev/devtools/download.html?t=201714 0.12 ...

随机推荐

  1. SDK location not found. Define location with sdk.dir in the local.properties file or with an ANDROID

    如题: SDK location not found. Define location with sdk.dir in the local.properties file or with an AND ...

  2. Java 虚拟机结构分析

    本博文主要介绍了JVM(Java Virtual Machine)的组成部分以及它们内部的工作机制和原理.需要注意的是,虽然平时我们用的大多是Sun(现已被Oracle收购)JDK提供的JVM,但是J ...

  3. Quartus 11进行编译Compile Design的时候出现错误near text ã

    1. 设计的工程在Compile Design的时候出现以下的错误,百思不得姐 Error (): Verilog HDL syntax error at div_5.v() near text ã ...

  4. 腾讯装扮下拉选项卡特效(QQ空间)

    <DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" ...

  5. 剑指Offer - 九度1511 - 从尾到头打印链表

    剑指Offer - 九度1511 - 从尾到头打印链表2013-11-29 21:08 题目描述: 输入一个链表,从尾到头打印链表每个节点的值. 输入: 每个输入文件仅包含一组测试样例.每一组测试案例 ...

  6. shell脚本获取网页快照并生成缩略图

    获取网页快照并生成缩略图可分两步进行: 1.获取网页快照 2.生成缩略图 获取网页快照 这里我们用 phantomjs 来实现.关于 phantomjs 的详细用法可参考官方网站. 1.安装 我的环境 ...

  7. MQ消息中间件

    MQ是什么? MQ是Message Queue消息队列的缩写.消息队列是一种应用程序对应用程序的通信方法.应用程序通过写和检索入列队的针对应用程序的数据(消息)来进行通信,而不需要专用连接来链接它们. ...

  8. Python学习1,代码

      看了好久的网上视频,今天尝试着写了一串代码: _author_ = "Happyboy" produce_list = [ ('Iphone',5800), ('Mac Pro ...

  9. Could not automatically select an Xcode project. Specify one in your Podfile like so

    需要将Podfile文件放置在根目录下,而不能放置在项目的里面. 更改路径即可

  10. Application_Start事件中用Timer做一个循环任务

    protected void Application_Start(object sender, EventArgs e) { System.Timers.Timer timer = new Syste ...