您的位置:首页 > 其它

那些年,我们一起学WCF--(1)wcf初识

2012-07-23 17:24 459 查看
最近,想把有关WCF的内容做一个全面的整理,想写一个系列的文章出来,供大家参考,以前也写过相关WCF的博客,但是都是零零碎碎的。这次从头对WCF做一个全面的整理,希望大家给予支持和帮助!

如果时间允许的话,本人会做一个同步视频教程,供大家交流。如果大家有好的视频录制软件,请提供提供,最好录制完后,视频压缩后容量特别小,方便上传到视频空间.

咱们言归正传,说下WCF吧!

本节重点

1. WCF简介

2. WCF入门实例

1. WCF简介

了解WEBSERVICE的同学,知道WEBSERVICE通过HTTP协议进行传输,创建webservice后,我们只需要在公网中给出一个URL,就可以在客户端远程调用。WCF其实也是这样,不过WCF的功能更强大,可以通过HTTP,.net tcp/ip,msmq,等多种方式进行传输.

从功能的角度来看,WCF完全可以看作是ASMX,.NetRemoting,EnterpriseService,WSE,MSMQ等技术的并集。但是,WCF在统一性,兼容性,安全性,兼容性方面都做了大大提高和整合。

System.ServiceModel命名空间包含生成(WCF)服务和客户端应用程序所需要的类型。它是微软封装好的一个组件,是WCF的核心DLL,在进行WCF开发的时候,客户端和服务器端都要使用该DLL.它包含生成服务和客户端应用程序所需的类、枚举和接口,这些类、枚举和接口可用于生成大范围的分布式应用程序。有了此DLL,使程序开发和设计更加方便快捷.

WCF通过绑定,地址,契约就可以确定一个服务,然后可以在客户端进行调用.

2.WCF入门实例

这个实例演示了在客户端输入内容,从服务器端返回,该实例使用的HTTP协议,客户端和服务端都采用的是WINFORM程序.

服务器端启动后,在客户端输入内容,就可以从服务器端返回消息。先看下效果图吧

服务器端:



客户端:



下面我们看下代码

服务器端APP.CONFIG

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<service name="WindowsServer.WelCome">
<endpoint address="http://localhost:8003/WindowsServer.WelCome" contract="WindowsServer.IWelCome" binding="wsHttpBinding" >
</endpoint>
</service>
</services>
</system.serviceModel>
</configuration>


服务器端契约

[ServiceContract]
interface IWelCome
{
[OperationContract]
string WelComeTip(string name);

}


服务器端契约实现

public  class WelCome:IWelCome
{
#region IWelCome 成员

public string WelComeTip(string name)
{
return name+ ":欢迎你来到WCF学堂!";
}

#endregion
}

服务器端启动代码

private void Form1_Load(object sender, EventArgs e)
{
this.Text = "服务器端启动....";
label1.Text = "服务器端启动..........";
ServiceHost host = new ServiceHost(typeof(WelCome));
host.Open();

}


客户端配置文件

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<client>
<endpoint address="http://localhost:8003/WindowsServer.WelCome" contract="WindowsServer.IWelCome" binding="wsHttpBinding"  name="WelCome"></endpoint>
</client>
</system.serviceModel>
</configuration>


客户端代码

private void button1_Click(object sender, EventArgs e)
{
using (ChannelFactory<WindowsServer.IWelCome> channelFactory = new ChannelFactory<WindowsServer.IWelCome>("WelCome"))
{
WindowsServer.IWelCome proxy = channelFactory.CreateChannel();
string tip=proxy.WelComeTip(textBox1.Text);
MessageBox.Show(tip);

}

}


很简单吧,大家动手试下就看到效果了,本系列所有实例都使用VS2010进行开发.

DEMO下载:http://download.csdn.net/detail/zx13525079024/4446502
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: