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. Jenkins部署报weblogic.deploy.api.tools.deployer.DeployerException: Java heap space

    Jenkins部署报weblogic.deploy.api.tools.deployer.DeployerException: Java heap space异常 解决办法: 在MAVEN_OPTS中 ...

  2. 深度学习原理与框架-Tensorflow卷积神经网络-cifar10图片分类(代码) 1.tf.nn.lrn(局部响应归一化操作) 2.random.sample(在列表中随机选值) 3.tf.one_hot(对标签进行one_hot编码)

    1.tf.nn.lrn(pool_h1, 4, bias=1.0, alpha=0.001/9.0, beta=0.75) # 局部响应归一化,使用相同位置的前后的filter进行响应归一化操作 参数 ...

  3. Linux网络编程学习(二) ----- 进程控制(第三章)

    1.进程和程序 程序是一个可执行文件,而一个进程是一个执行中的程序实例.一个进程对应于一个程序的执行,进程是动态的,程序是静态的,多个进程可以并发执行同一个程序.比如几个用户可以同时运行一个编辑程序, ...

  4. Spring再接触 注入类型

    共有三种注入类型 一种是set注入 一种是构造注入 一种是接口注入 最常用的还是set 现在看一下construct 构造注入 在userservice中加入 package com.bjsxt.se ...

  5. C语言实现的反转字符串

    这段代码大约是10年前写的了,一直收藏在自己的代码仓库里面,贴出来分享下. 网上也有很多类似的代码,学生们用的比较多,工作中用的很少,权做参考. char* ReverseString(char* s ...

  6. chart.js应用中遇到的问题

    问题一:chart.js的版本问题:打开官网https://github.com/chartjs/Chart.js/releases/tag/v2.7.3,点击Tags,选择最新版本,我这里选用的是V ...

  7. Linux 下监控用户最大进程数参数(nproc)是否到达上限的步骤:

    https://www.cnblogs.com/autopenguin/p/6184886.html 1.查看各系统用户的进程(LWP)数: 注意:默认情况下采用 ps 命令并不能显示出所有的进程.因 ...

  8. oracle函数nvl, nvl2, nullif

    nvl函数 语法: NVL(表达式1, 表达式2) select nvl(s.name, '未填写') from student s 如果表达式1的值为空, 则显示第二个值, 否则显示原来的值, nv ...

  9. 手游开发之lua的table 元表的运用

    元表在项目中的运用,其中就包括元方法这点.元方法是指__index和__newIndex,下面我总结下,更详细的例子讲解可以参考<lua程序设计 第2版>的第13章内容.长h短说,简言之有 ...

  10. mybatis-plus 自动生成代码

    public class MpGenerator { /** * <p> * MySQL 生成演示 * </p> */ public static void main(Stri ...