下面先介绍一下我的开发环境吧
操作系统:windows 2003
编译器:Visual Studio 2005(3.5 framework)
WCF的三大核心是ABC
也就是A代表Address-where(对象在哪里)
B代表Binding-how(通过什么协议取得对象)
C代表Contact(契约)-what(定义的对象是什么,如何操纵)
现在用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();
}
}
}
就可以获取得到服务器的对象了
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
网络监控专题之一Centos7部署Zabbix
网络设备监控专题之部署Zabbix开源监控平台
网络监控 Zabbix Linux -
WCF中的Binding模型之一: Binding模型简介
WCF的通信是基于消息的,如果从消息交换(message exchange)
WCF Binding Channel ChannelManager ChannelListener -
WCF技术剖析之一:通过一个ASP.NET程序模拟WCF基础架构
细算起来,已经有好几个月没有真正的写过文章了。近半年以来,一直忙于我的第一本WCF专著《WCF技术剖析》的写作,一直无暇管理自己的 Blog。
WCF ASP.NET MessageEncoder MessageFormatter InstanceProvider -
艾伟_转载:WCF版的PetShop之一:PetShop简介
本系列文章导航WCF版的PetShop之一:PetShop简介WC
模块化 asp.net 服务调用 缓存 web应用 -
WCF版的PetShop之一:PetShop简介[提供源代码下载]
在《WCF技术剖析(卷1)》的最后一章,我写了一个简单基于WCF的Web应用程序,该程序模拟一
PIAB WCF Unity AOP IoC