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 ...
随机推荐
- ---mingw Linux交叉编译给Window的工具
https://arrayfire.com/cross-compile-to-windows-from-linux/
- java学习--java源文件
java源文件以“java”为扩展名.源文件的基本组成部分是类(class) 一个源文件中最多只能有一个public类.其他类(如抽象类,default类即省去修饰符的类,接口)的个数不限. 如果源文 ...
- 一个free异常引发的异常
有同事反馈说自己的线程不工作,查看堆栈发现其打印如下: # # # # # # # # , info= # <signal handler called> # # # # # # # , ...
- 使用Powershell实现计算机名称及IP地址修改
我的第一篇博客分享,写这个代码的用途是在公司Ghost完系统之后去修改本地计算机名称及IP 地址,用Powershell实现. 1. 代码第一部分,检查Powershell是否已管理员权限执行,如果不 ...
- MySQL中的sort_buffer_size参数大小的设置问题
看到sort_buffer_size这个参数(connect级别的参数,MySQL5.7,默认值是1048576字节,也就是1MB)的默认值这么小,想着是不是可以调大一点,反正内存动不动几十个GB的, ...
- http://www.36dsj.com/archives/46131
https://docs.growingio.com/Developer%20Document.html https://youdata.163.com/dash/39706/editor?pid=7 ...
- ubuntu系统中安装eclipse
具体可以看这篇博文 .https://www.cnblogs.com/sanduo1314/articles/5137090.html 然后再/usr/share/applications中找到ecl ...
- javaMail实现收发邮件(三)
JavaMail API中定义了一个java.mail.Transport类,它专门用于执行邮件发送任务,这个类的实例对象封装了某种邮件发送协议的底层实施细节,应用程序调用这个类中的方法就可以把Mes ...
- Java17-java语法基础——泛型
Java18-java语法基础——泛型 一.泛型概念和作用 1.泛型概念: 泛型是JavaSE1.5的新特性,泛型的本质是参数化类型,也就是说,所操作的数据类型被指定为一个参数.这种参数类型可以用在类 ...
- Linux-echo、cat命令详解(14)
echo:显示一段文字 比如: echo hello,串口上就显示hello echo hello > /dev/tty1, LCD上便显示hello字段 cat:查看一个文件的内容 比如: c ...