This is a very simple demo which can help you create a wcf applition quickly.

Create a Solution

Open Vistual Stuido and create a solution named WCFDemoJoey. It looks like this:

Crate a Entity

using System.Runtime.Serialization;
namespace Entities
{
[DataContract]
public class Person
{
[DataMember]
public int PersonId { get; set; } [DataMember]
public string Name { get; set; } [DataMember]
public int Age { get; set; }
}
}

The attribute DataConract and DataMember represents a way of Serialization.

Create a Service Contract

A contract is a interface which defines some remote methods:

namespace Service.Interface
{
[ServiceContract(Name = "PeopleOperatorService", Namespace ="http://zzy0471.cnblogs.com")]
public interface IPeopleOperator
{
[OperationContract]
Person GetPerson();
}
}

We can identify a interface as a contract by using attribute ServiceContract

Create a Service

A service is a implement of a contract:

using Service.Interface;
using Entities; namespace Service
{
public class PeopleService : IPeopleOperator
{
public Person GetPerson()
{
return new Person { PersonId = 1, Name = "Joey", Age = 30 };
}
}
}

Create a Host

WCF must be in a process which be called Host:

namespace Host
{
class Program
{
static void Main(string[] args)
{
using (ServiceHost host = new ServiceHost(typeof(PeopleService)))
{
host.AddServiceEndpoint(typeof(IPeopleOperator),
new WSHttpBinding(),
"http://localhost:9999/PeopleService"); if (host.Description.Behaviors.Find<ServiceMetadataBehavior>() == null)
{
ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();
behavior.HttpGetEnabled = true;
behavior.HttpGetUrl = new Uri("http://localhost:9999/PeopleService/PeopleService/metadata");
host.Description.Behaviors.Add(behavior);
} host.Opened += (s, e) => Console.WriteLine("Service is running, press any key to stop");
host.Open();
Console.Read();
}
}
}
}

Create a Clinet

using Service.Interface;
using System;
using System.ServiceModel; namespace Client
{
class Program
{
static void Main(string[] args)
{
using (ChannelFactory<IPeopleOperator> channelFactory = new ChannelFactory<IPeopleOperator>(new WSHttpBinding(),
"http://localhost:9999/PeopleService"))
{
IPeopleOperator proxy = channelFactory.CreateChannel(); var person = proxy.GetPerson();
Console.WriteLine(person.Name + " " + person.Age);
Console.Read();
}
}
}
}

There are three points we must pay close attention to:

  1. Address
  2. Binding
  3. Contract

Address represents where should we visit the service.

Binding represents how do we Transfer information, the WSHttpBinding is one of the ways.

Contract represents what romate mehods we can call.

Download the sourcecode

Learning WCF:A Simple Demo的更多相关文章

  1. Learning WCF:Life Cycle of Service instance

    示例代码下载地址:WCFDemo1Day 概述 客户端向WCF服务发出请求后,服务端会实例化一个Service对象(实现了契约接口的对象)用来处理请求,实例化Service对象以及维护其生命周期的方式 ...

  2. Learning WCF:Fault Handling

    There are two types of Execptions which can be throwed from the WCF service. They are Application ex ...

  3. Learning WCF Chapter1 Generating a Service and Client Proxy

    In the previous lab,you created a service and client from scratch without leveraging the tools avail ...

  4. Learning WCF Chapter1 Hosting a Service in IIS

    How messages reach a service endpoint is a matter of protocols and hosting. IIS can host services ov ...

  5. Java 下的 JSON库性能比较:JSON.simple

    JSON已经成为当前服务器与WEB应用之间数据传输的公认标准,不过正如许多我们所习以为常的事情一样,你会觉得这是理所当然的便不再深入思考了.我们很少会去想用到的这些JSON库到底有什么不同,但事实上它 ...

  6. JSON库之性能比较:JSON.simple VS GSON VS Jackson VS JSONP

    从http://www.open-open.com/lib/view/open1434377191317.html 转载 Java中哪个JSON库的解析速度是最快的? JSON已经成为当前服务器与WE ...

  7. WCF:为 SharePoint 2010 Business Connectivity Services 构建 WCF Web 服务(第 1 部分,共 4 部分)

    转:http://msdn.microsoft.com/zh-cn/library/gg318615.aspx 摘要:通过此系列文章(共四部分)了解如何在 Microsoft SharePoint F ...

  8. Learning WCF 书中的代码示例下载地址

    Learning WCF Download Example Code 第一个压缩文件LearningWCF.zip是VS2005创建的项目,不要下载这个. 建议下载VS2008版的,以及Media

  9. simple demo of Handlebars.js & jquery.js

    simple demo of Handlebars.js & jquery.js <html> <head> <script src="jquery-1 ...

随机推荐

  1. win7、centos7 双系统安装总结

    centos7安装过程 问题:TroubleShooting选项进入图形化界面安装才成功. win7恢复引导区 问题:安装完Centos后,win7的引导区不见了 具体恢复过程:http://www. ...

  2. Linux安装配置JDK1.7

    1  在/usr/local   文件夹下新建一个文件夹software ,将JDK放到此文件夹中 并在此文件夹下解压执行命令  tar  zxvf  jdk-8u144-linux-x64.tar. ...

  3. (转)SQL SERVER 生成建表脚本

    https://www.cnblogs.com/champaign/p/3492510.html /****** Object: StoredProcedure [dbo].[GET_TableScr ...

  4. P1880 [NOI1995]石子合并-(环形区间dp)

    https://www.luogu.org/problemnew/show/P1880 解题过程:本次的题目把石子围成一个环,与排成一列的版本有些不一样,可以在后面数组后面再接上n个元素,表示连续n个 ...

  5. 【前言】Go语言开坑

    很早之前就已经听过Go语言的大名,今天终于要开坑研究Go了,来吧看看<Go语言从入门到入坟>. [Go语言学习目录] 1. Go安装 2. Go变量(Variables) 3. Go语言基 ...

  6. TZOJ 1693 Silver Cow Party(最短路+思维)

    描述 One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big ...

  7. FortiGate上架前准备

    1.收集信息 1.网络拓扑信息(了解网络拓扑信息有助于网络方案的规划) 2.环境信息(了解部署位置.部署模式.最大吞吐.最大用户数有助于对设备性能的评估) 3.客户需求,对FortiGate部署的功能 ...

  8. Python学习笔记-常用内置函数

    输出:print() 功能:输出打印 语法:print(*objects, sep=' ', end='\n', file=sys.stdout) 参数:objects----复数,表示可以一次输出多 ...

  9. [leetcode]16. 3Sum Closest最接近的三数之和

    Given an array nums of n integers and an integer target, find three integers in nums such that the s ...

  10. 校验金额、大小写字母、大写字母、合法uri、email

    /* 合法uri*/ export function validURL(url) { const reg = /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0- ...