Learning WCF:A Simple Demo
2024-10-18 21:25:15
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.
最新文章
- 0032 Java学习笔记-类加载机制-初步
- Reportng报告替代testng
- C#构造方法重载
- NodeJS无所不能:细数10个令人惊讶的NodeJS开源项目
- python之range(), xrange()
- CoreAnimation6-基于定时器的动画和性能调优
- 想追赶.Net的脚步?Java面前障碍重重
- Java课程设计——学生信息系统(团队)
- 百度地图api 区级以下行政区划
- HDU1005 找规律 or 循环点 or 矩阵快速幂
- verdi\debussy的使用技巧
- 2019年1月6日 没有nainai吃 习题1
- tomcat启动出现Preparing launch delegate,一直卡在100%
- SORT AGAIN(hdu2523)
- shell学习笔记汇总
- windows命令行大全
- 字典树(前缀树)-Java实现
- ny55 懒省事的小明
- HDU 6035 Colorful Tree(dfs)
- Python的hasattr() getattr() setattr() 函数使用方法(简介)
热门文章
- Idea中类上有叉的解决方法
- python大法好——模块(内置模块未完)
- layer.js 注册登录切换的问题
- [C语言]进阶|图形库
- mysql修改EST时区,mysql时间修改
- Python设计模式 - UML - 类图(Class Diagram)
- nlp算法工程师养成记 目标要求
- io.netty.resolver.dns.DnsNameResolverContext
- [leetcode]5. Longest Palindromic Substring最长回文子串
- Linux系统minicom命令详解