WCF架构入门-用VS2008构建WCF
2011-05-25 15:23
337 查看
根据微软官方的解释,WCF(之前的版本名为“Indigo”)是使用托管代码建立和运行面向服务(Service Oriented)应用程序的统一框架。它使得开发者能够建立一个跨平台的安全、可信赖、事务性的解决方案,且能与已有系统兼容协作。WCF是微软分布式应用程序开发的集大成者,它整合了.Net平台下所有的和分布式系统有关的技术,例如.Net Remoting、ASMX、WSE和MSMQ。以通信(Communiation)范围而论,它可以跨进程、跨机器、跨子网、企业网乃至于 Internet;以宿主程序而论,可以以ASP.NET,EXE,WPF,Windows Forms,NT Service,COM+作为宿主(Host)。WCF可以支持的协议包括TCP,HTTP,跨进程以及自定义,安全模式则包括SAML, Kerberos,X509,用户/密码,自定义等多种标准与模式。也就是说,在WCF框架下,开发基于SOA的分布式系统变得容易了,微软将所有与此相关的技术要素都包含在内,掌握了WCF,就相当于掌握了叩开SOA大门的钥匙。
本文就是要通过一个简单的例子,介绍WCF架构的搭建方法和步骤。本文使用的开发工具是VS2008。
WCF架构包括三大部分:服务、宿主进程和客户端。其中服务和宿主进程属于服务端。
一、服务(Service)
服务主要包括契约和服务的实现。
1.1 契约
WCF的所有服务必须公开契约。契约是与平台无关的,描述服务功能的标准方式。主要有服务契约、数据契约、消息契约和错误契约。
本文着重介绍一下服务契约的定义。我们所提供的服务是一个加法计算(Add)。首先我们定义契约。
view plaincopy to clipboardprint?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
namespace manual_WCF
{
[<font color="#339900">ServiceContract</font>]
interface IMyProcess
{
[<font color="#339900">OperationContract</font>]
int Add(int a, int b);
}
}
我们可以将[ServiceContract]属性应用到接口或者类上,但是通常我们将其应用于接口上,而不是类上。
另外,即使接口应用了[ServiceContract]属性,并不意味它的所有成员都是契约的一部分。我们必须使用[OperationContract]属性显式地标注哪些方法需要暴露为WCF契约的一部分。
1.2 服务的实现
服务实现,其实就是对契约(接口)的实现。
view plaincopy to clipboardprint?
<font color="#000000"><pre class="csharp" name="code"><strong>using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace manual_WCF
{
class MyProcess:IMyProcess
{
public int Add(int a, int b)
{
Console.WriteLine("Received Add({0},{1}) Returnning:{2}",a,b,a+b);
return a + b;
}
}
}</strong></pre>
</font>
二、宿主进程(Host)
WCF服务不可能凭空存在。每个WCF服务必须托管(Hosting)在Windows进程中,该进程就称为宿主进程(Host Process)。宿主可以由IIS提供,也可以有WindowsForm程序或者Console提供,也可以由Windows服务提供。
view plaincopy to clipboardprint?
<font color="#000000"><strong>using System;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Net;
namespace manual_WCF
{
class Program
{
static void Main(string[] args)
{
Uri baseAddress = new Uri("http://localhost:8001/");
ServiceHost Host = new ServiceHost(typeof(MyProcess), baseAddress);
Host.AddServiceEndpoint(typeof(IMyProcess),new BasicHttpBinding(),"manualWCF");
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
Host.Description.Behaviors.Add(smb);
Host.Open();
Console.WriteLine("The manual WCF is running at " + baseAddress.ToString()+"manualWCF");
Console.WriteLine("Press <ENTER> to terminate");
Console.ReadLine();
Host.Close();
}
}
}</strong></font>
view plaincopy to clipboardprint?
<font color="#000000"> </font><font color="#3366ff"><strong>三、客户端(Client)</strong></font>
若要调用WCF服务的操作,客户端需要首先导入服务契约到客户端本地描述(Native Representation)中。如果客户端使用WCF,调用操作的常见做法是使用代理。代理是一个CLR类,它公开了一个单独的CLR接口用以表示服务的契约。代理完全封装了服务的每一个方面:服务的位置、实现技术、运行时平台以及通信传输。
3.1 生成代理
使用命令行工具SvcUtil.exe 生成代理
svcutil.exe /language:c# http://localhost:8001
运行以上命令会生成两个文件MyProcess.cs和output.config。将这两个文件导入到客户端工程中(将output.config改名为app.config)。
3.2 调用服务操作
此时,就可以像使用本地方法一样使用WCF服务中的操作了
view plaincopy to clipboardprint?
<font color="#000000">using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Client_manual
{
class Program
{
static void Main(string[] args)
{
MyProcessClient client = new MyProcessClient();
Console.WriteLine(client.Add(3,5));
Console.ReadLine();
}
}
}</font>
本文就是要通过一个简单的例子,介绍WCF架构的搭建方法和步骤。本文使用的开发工具是VS2008。
WCF架构包括三大部分:服务、宿主进程和客户端。其中服务和宿主进程属于服务端。
一、服务(Service)
服务主要包括契约和服务的实现。
1.1 契约
WCF的所有服务必须公开契约。契约是与平台无关的,描述服务功能的标准方式。主要有服务契约、数据契约、消息契约和错误契约。
本文着重介绍一下服务契约的定义。我们所提供的服务是一个加法计算(Add)。首先我们定义契约。
view plaincopy to clipboardprint?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
namespace manual_WCF
{
[<font color="#339900">ServiceContract</font>]
interface IMyProcess
{
[<font color="#339900">OperationContract</font>]
int Add(int a, int b);
}
}
我们可以将[ServiceContract]属性应用到接口或者类上,但是通常我们将其应用于接口上,而不是类上。
另外,即使接口应用了[ServiceContract]属性,并不意味它的所有成员都是契约的一部分。我们必须使用[OperationContract]属性显式地标注哪些方法需要暴露为WCF契约的一部分。
1.2 服务的实现
服务实现,其实就是对契约(接口)的实现。
view plaincopy to clipboardprint?
<font color="#000000"><pre class="csharp" name="code"><strong>using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace manual_WCF
{
class MyProcess:IMyProcess
{
public int Add(int a, int b)
{
Console.WriteLine("Received Add({0},{1}) Returnning:{2}",a,b,a+b);
return a + b;
}
}
}</strong></pre>
</font>
二、宿主进程(Host)
WCF服务不可能凭空存在。每个WCF服务必须托管(Hosting)在Windows进程中,该进程就称为宿主进程(Host Process)。宿主可以由IIS提供,也可以有WindowsForm程序或者Console提供,也可以由Windows服务提供。
view plaincopy to clipboardprint?
<font color="#000000"><strong>using System;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Net;
namespace manual_WCF
{
class Program
{
static void Main(string[] args)
{
Uri baseAddress = new Uri("http://localhost:8001/");
ServiceHost Host = new ServiceHost(typeof(MyProcess), baseAddress);
Host.AddServiceEndpoint(typeof(IMyProcess),new BasicHttpBinding(),"manualWCF");
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
Host.Description.Behaviors.Add(smb);
Host.Open();
Console.WriteLine("The manual WCF is running at " + baseAddress.ToString()+"manualWCF");
Console.WriteLine("Press <ENTER> to terminate");
Console.ReadLine();
Host.Close();
}
}
}</strong></font>
view plaincopy to clipboardprint?
<font color="#000000"> </font><font color="#3366ff"><strong>三、客户端(Client)</strong></font>
若要调用WCF服务的操作,客户端需要首先导入服务契约到客户端本地描述(Native Representation)中。如果客户端使用WCF,调用操作的常见做法是使用代理。代理是一个CLR类,它公开了一个单独的CLR接口用以表示服务的契约。代理完全封装了服务的每一个方面:服务的位置、实现技术、运行时平台以及通信传输。
3.1 生成代理
使用命令行工具SvcUtil.exe 生成代理
svcutil.exe /language:c# http://localhost:8001
运行以上命令会生成两个文件MyProcess.cs和output.config。将这两个文件导入到客户端工程中(将output.config改名为app.config)。
3.2 调用服务操作
此时,就可以像使用本地方法一样使用WCF服务中的操作了
view plaincopy to clipboardprint?
<font color="#000000">using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Client_manual
{
class Program
{
static void Main(string[] args)
{
MyProcessClient client = new MyProcessClient();
Console.WriteLine(client.Add(3,5));
Console.ReadLine();
}
}
}</font>
相关文章推荐
- WCF架构入门-用VS2008构建WCF
- WCF架构入门-用VS2008构建WCF(转)
- 通过Ruby on Rails和docker构建微服务架构之入门教程
- WCF随录(2)—构建WCF的服务架构
- [Android入门]从零构建Android app之天气——5.设计网络请求底层架构
- WCF入门(四)---WCF架构
- Spring Cloud构建微服务架构:消息驱动的微服务(入门)【Dalston版】
- Spring Cloud构建微服务架构:分布式服务跟踪(入门)
- 如何在Visual Studio 2017中使用C# 7+语法 构建NetCore应用框架之实战篇(二):BitAdminCore框架定位及架构 构建NetCore应用框架之实战篇系列 构建NetCore应用框架之实战篇(一):什么是框架,如何设计一个框架 NetCore入门篇:(十二)在IIS中部署Net Core程序
- 【WCF系列】之二:构建WCF的服务架构
- Spring Cloud构建微服务架构:分布式服务跟踪(入门)
- SpringCloud构建微服务入门架构(二)服务注册与发现Eureka集群搭建
- 6 本软件架构书,构建你的架构知识体系
- 一步步构建大型网站架构
- 构建基于分布式SOA架构的统一身份认证体系
- SpringBoot入门——使用Swagger构建Restful API文档
- WCF 4.0 进阶系列 – 第十五章 构建REST服务(第三部分)
- HongHu云架构common-service构建流程
- 一步步构建大型网站架构
- 介绍Spring Cloud构建微服务架构