作为微软技术.net 3.5的三大核心技术之一的WCF虽然没有WPF美丽的外观
2010-10-12 12:33
423 查看
作为微软技术.net 3.5的三大核心技术之一的WCF虽然没有WPF美丽的外观
但是它却是我们开发分布式程序的利器
但是目前关于WCF方面的资料相当稀少
希望我的这一系列文章可以帮助大家尽快入门
下面先介绍一下我的开发环境吧
操作系统:windows vista business版本
编译器:Visual Studio 2008(英文专业版)
WCF的三大核心是ABC
也就是A代表Address-where(对象在哪里)
B代表Binding-how(通过什么协议取得对象)
C代表Contact(契约)-what(定义的对象是什么,如何操纵)
其他的理论知识大家可以参见《Programming WCF Service》
或者今年3月份刚刚出版的《Essential Windows Commmunication Foundation》
现在用In Action的方式来手把手教大家创建第一个WCF程序
首先如下图所示创建一个空的解决方案
接下来右键点击解决方案HelloWCF选择Add->New Project并选择Console Application模板并选择名为项目名为Host(服务器端)
接下来右键点击Host项目选择Add->New Item并选择Webservice模板(文件命名为HelloWCFService)
将创建三个文件IHelloWCFService.cs,HelloWCFService.cs以及App.config文件
IHelloWCFService.cs代码如下
using System.ServiceModel;
namespace Host
{
[ServiceContract]
public interface IHelloWCFService
{
[OperationContract]
string HelloWCF(string message);
}
}
而HelloWCFService.cs代码实现如下
using System;
namespace Host
{
public class HelloWCFService : IHelloWCFService
{
public string HelloWCF(string message)
{
return string.Format("你在{0}收到信息:{1}",DateTime.Now,message);
}
}
}
App.config文件原则上可以不用改,但是address太长了
(默认的为baseAddress=http://localhost:8731/Design_Time_Addresses/Host/HelloWCFService/)
缩短为baseAddress=http://localhost:8731/HelloWCFService/
并修改Program.cs文件为
using System;
using System.ServiceModel;
namespace Host
{
class Program
{
static void Main(string[] args)
{
using(ServiceHost host=new ServiceHost(typeof(Host.HelloWCFService)))
{
host.Open();
Console.ReadLine();
host.Close();
}
}
}
}
编译并生成Host.exe文件
接下来创建客户端程序为Console Application项目Client
启动Host.exe文件
右键点击Client项目并选择Add Service Reference...
并且在Address的TextBox里面输入服务器的地址(就是咱们前面设置的baseaddress地址),并点击Go
将得到目标服务器上面的Services,如下图所示
这一步见在客户端间接借助SvcUtil.exe文件创建客户端代理(using Client.HelloWCF;)以及配置文件app.config
修改客户端的程序如下
using System;
using Client.HelloWCF;
namespace Client
{
class Program
{
static void Main(string[] args)
{
HelloWCF.HelloWCFServiceClient proxy=new HelloWCFServiceClient();
string str = proxy.HelloWCF("欢迎来到WCF村!");
Console.WriteLine(str);
Console.ReadLine();
}
}
}
就可以获取得到服务器的对象了
但是它却是我们开发分布式程序的利器
但是目前关于WCF方面的资料相当稀少
希望我的这一系列文章可以帮助大家尽快入门
下面先介绍一下我的开发环境吧
操作系统:windows vista business版本
编译器:Visual Studio 2008(英文专业版)
WCF的三大核心是ABC
也就是A代表Address-where(对象在哪里)
B代表Binding-how(通过什么协议取得对象)
C代表Contact(契约)-what(定义的对象是什么,如何操纵)
其他的理论知识大家可以参见《Programming WCF Service》
或者今年3月份刚刚出版的《Essential Windows Commmunication Foundation》
现在用In Action的方式来手把手教大家创建第一个WCF程序
首先如下图所示创建一个空的解决方案
接下来右键点击解决方案HelloWCF选择Add->New Project并选择Console Application模板并选择名为项目名为Host(服务器端)
接下来右键点击Host项目选择Add->New Item并选择Webservice模板(文件命名为HelloWCFService)
将创建三个文件IHelloWCFService.cs,HelloWCFService.cs以及App.config文件
IHelloWCFService.cs代码如下
using System.ServiceModel;
namespace Host
{
[ServiceContract]
public interface IHelloWCFService
{
[OperationContract]
string HelloWCF(string message);
}
}
而HelloWCFService.cs代码实现如下
using System;
namespace Host
{
public class HelloWCFService : IHelloWCFService
{
public string HelloWCF(string message)
{
return string.Format("你在{0}收到信息:{1}",DateTime.Now,message);
}
}
}
App.config文件原则上可以不用改,但是address太长了
(默认的为baseAddress=http://localhost:8731/Design_Time_Addresses/Host/HelloWCFService/)
缩短为baseAddress=http://localhost:8731/HelloWCFService/
并修改Program.cs文件为
using System;
using System.ServiceModel;
namespace Host
{
class Program
{
static void Main(string[] args)
{
using(ServiceHost host=new ServiceHost(typeof(Host.HelloWCFService)))
{
host.Open();
Console.ReadLine();
host.Close();
}
}
}
}
编译并生成Host.exe文件
接下来创建客户端程序为Console Application项目Client
启动Host.exe文件
右键点击Client项目并选择Add Service Reference...
并且在Address的TextBox里面输入服务器的地址(就是咱们前面设置的baseaddress地址),并点击Go
将得到目标服务器上面的Services,如下图所示
这一步见在客户端间接借助SvcUtil.exe文件创建客户端代理(using Client.HelloWCF;)以及配置文件app.config
修改客户端的程序如下
using System;
using Client.HelloWCF;
namespace Client
{
class Program
{
static void Main(string[] args)
{
HelloWCF.HelloWCFServiceClient proxy=new HelloWCFServiceClient();
string str = proxy.HelloWCF("欢迎来到WCF村!");
Console.WriteLine(str);
Console.ReadLine();
}
}
}
就可以获取得到服务器的对象了
相关文章推荐
- 书精通C# 3.0与.NET 3.5高级编程——LINQ、WCF、WPF、WF(随书附赠的评论
- ORM + .NET Remoting 完整例子程序 虽然现在都流行WCF,也没有必要抛弃已经掌握的.NET Remoting
- 微软.NET开发认证基础技术知识大局观——核心篇
- WCF作为SAO技术的核心:契约
- 几句话洞悉.NET 3.0后微软技术发展趋势
- 微软项目技术结构思路 ExtJs/JQuery + Asp.net MVC + Wcf service + Entity Framework
- 新疆微软.NET技术俱乐部首次活动大会图文报道
- 谷歌三大核心技术(二)Google MapReduce中文版
- 一起谈.NET技术,WCF使用NetTcp传输文件
- 一起谈.NET技术,NET下RabbitMQ实践 [WCF发布篇]
- 谷歌三大核心技术(一)Google File System - 系统设计
- 一起谈.NET技术,Silverlight3动态访问wcf
- 谷歌三大核心技术(二)Google MapReduce中文版
- “1美元利润”的中国制造能走多远——没有核心技术跳不出发展僵局
- 了解微软开源核心机器学习技术DMTK
- 谷歌三大核心技术(二)Google MapReduce中文版
- 新疆微软.NET技术俱乐部首次活动大会资料下载
- 2008年11月份最佳C#技术文章集,关于WPF LINQ pre-JIT WCF等最新技术实例
- 新疆微软.NET技术俱乐部第二次活动安排
- 微软项目技术结构思路 ExtJs/JQuery + Asp.net MVC + Wcf service + Entity Framework