Learning WCF:A Simple Demo
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:
- Address
- Binding
- 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.
Learning WCF:A Simple Demo的更多相关文章
- Learning WCF:Life Cycle of Service instance
示例代码下载地址:WCFDemo1Day 概述 客户端向WCF服务发出请求后,服务端会实例化一个Service对象(实现了契约接口的对象)用来处理请求,实例化Service对象以及维护其生命周期的方式 ...
- Learning WCF:Fault Handling
There are two types of Execptions which can be throwed from the WCF service. They are Application ex ...
- 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 ...
- 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 ...
- Java 下的 JSON库性能比较:JSON.simple
JSON已经成为当前服务器与WEB应用之间数据传输的公认标准,不过正如许多我们所习以为常的事情一样,你会觉得这是理所当然的便不再深入思考了.我们很少会去想用到的这些JSON库到底有什么不同,但事实上它 ...
- JSON库之性能比较:JSON.simple VS GSON VS Jackson VS JSONP
从http://www.open-open.com/lib/view/open1434377191317.html 转载 Java中哪个JSON库的解析速度是最快的? JSON已经成为当前服务器与WE ...
- WCF:为 SharePoint 2010 Business Connectivity Services 构建 WCF Web 服务(第 1 部分,共 4 部分)
转:http://msdn.microsoft.com/zh-cn/library/gg318615.aspx 摘要:通过此系列文章(共四部分)了解如何在 Microsoft SharePoint F ...
- Learning WCF 书中的代码示例下载地址
Learning WCF Download Example Code 第一个压缩文件LearningWCF.zip是VS2005创建的项目,不要下载这个. 建议下载VS2008版的,以及Media
- simple demo of Handlebars.js & jquery.js
simple demo of Handlebars.js & jquery.js <html> <head> <script src="jquery-1 ...
随机推荐
- Linux操作系统监控分析
性能分析点:1. 负载机2.网络传输3.硬件4.应用程序线程池5.数据库连接池6.代码业务逻辑7.数据库sql执行时间8.jvm(GC) 操作系统硬件:cpu:计算.逻辑处理:CPU的颗粒数越多,CP ...
- leetcode84
public class Solution { public int LargestRectangleArea(int[] hist) { // The main function to find t ...
- Sybase采用load table加载文本数据中的部分字段数据
LOAD TABLE语句实现从数据文件中装载部分列数据的方法.转载自:https://www.cnblogs.com/lizm166/p/8116475.html(有修改) LOAD TABLE:从数 ...
- UNITY 打包时提示sdk tools 或 sdk build tools版本低时可以直接点update 按钮进行更新
如题.如果不更新,而选择 : use newest version installed ,打包到结尾时可能会报错,莫名其妙的java错误 而且,SDK一旦被更新后,其所在目录的 SDK MANAGER ...
- 反射与特性与Tool编写
大多数程序都是用来处理数据的,他们读,写,操作和显示数据,图形也是一种数据. 程序员为某种目的创建和使用一些类型,因此,在设计时必须理解所使用类型的特性. 有关程序及其类型的数据被称为元数据,他们保存 ...
- c#mvc实现登录
本篇介绍MVC实现登录的方式,如下: 1.通过MVC Form 表单请求实现登录 2.通过AJAX GET 请求MVC Controller 实现登录 3.通过AJAX POST 请求MVC Cont ...
- 小强学渲染之OpenGL的GPU管线
GPU渲染流水线,是硬件真正体现渲染概念的操作过程,也是最终将图元画到2D屏幕上的阶段.GPU管线涵盖了渲染流程的 几何阶段 和 光栅化阶段,但对开发者而言,只有对顶点和片段着色器有可编程控制权,其他 ...
- POJ-1797.HeavyTransportation(最长路中的最小权值)
本题思路:最短路变形,改变松弛方式即可,dist存的是源结点到当前结点的最长路的最小权值. 参考代码: #include <cstdio> #include <cstring> ...
- Linux的php-fpm优化心得-php-fpm进程占用内存大和不释放内存问题(转)
原文地址:https://wzfou.com/php-fpm/ 最近发现博客的内存老是隔三差五地被“吃掉”了,登录到后台后偶尔会出卡顿的情况,一开始怀疑是Swap不够导致的,于是给VPS主机增加了几个 ...
- git回滚远程仓库
关于远程仓库回滚 首先,必须要明白的一件事,任何普通用户不能擅自做有关远程仓库回退的操作,如果你擅自回滚了远程仓库,会对项目团队其他人造成不可预知的影响.如果需要回退版本,先联系项目的仓库管理员,在团 ...