【WCF--初入江湖】01 WCF编程概述
01 WCF编程概述
SOA的优点
什么是WCF
WCF
是微软分布式应用程序开发的集大成者,它整合了.Net平台下所有的和分布式系统有关的技术,如
Enterprise Sevices
(COM+).Net Remoting(一种非常有弹性的扩展性框架)、
Web Service(ASMX)、
WSE3.0
MSMQ消息队列。
以通信(Communiation)范围而论,它可以
跨进程、跨机器、跨子网、企业网乃至于 Internet;
以宿主程序而论,可以以
ASP.NET,EXE,WPF,Windows Forms,NT Service,COM+作为宿主(Host)
WCF可以支持的协议包括TCP,
HTTP,跨进程以及自定义,
安全模式则包括SAML, Kerberos,X509,用户/密码,
自定义等多种标准与模式。
也就是说,在WCF框架下,开发基于SOA的分布式系统变得容易了,微软将所有与此相关的技术要素都包含在内,掌握了WCF,就相当于掌握了叩开SOA大门的钥匙
WCF与webService是什么关系
ASP.NET Web Service:
利用Soap协议对象在分布式环境之间的数据交互。
WSDL来发布服务接口相关的描写。
称为WSE系列:
ASP.NET Web Service的安全,功能,数据加密、解密,托管宿主等多方面的伸展,
WCF宿主
static void Main(string[] args)
{
var baseAddress = ConfigurationManager.AppSettings["baseAddress"];
ApplicationService.Initialize();
MongoDBBootstrapper.Bootstrap(); using (ServiceHost orderService = new ServiceHost(typeof(OrderService), new Uri(string.Format("{0}/OrderService.svc", baseAddress))))
using (ServiceHost productService = new ServiceHost(typeof(ProductService), new Uri(string.Format("{0}/ProductService.svc", baseAddress))))
using (ServiceHost userService = new ServiceHost(typeof(UserService), new Uri(string.Format("{0}/UserService.svc", baseAddress))))
{
orderService.Open();
productService.Open();
userService.Open(); Console.WriteLine("Byteart Retail Services started at: {0}.", DateTime.Now);
Console.ReadLine();
Console.WriteLine("Byteart Retail Services stopped at: {0}.", DateTime.Now); orderService.Close();
productService.Close();
userService.Close();
}
}
<appSettings>
<add key="baseAddress" value="http://localhost:8559"/>
</appSettings>
服务端配置:
<system.serviceModel>
<extensions>
<behaviorExtensions>
<add name="unity" type="ByteartRetail.Infrastructure.UnityExtensions.UnityBehaviorExtensionElement, ByteartRetail.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
</behaviorExtensions>
</extensions>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
<unity operationContextEnabled="true" instanceContextEnabled="true" contextChannelEnabled="true" serviceHostBaseEnabled="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
另一种配置,
namespace Keasy5.WCF.WCFHost
{
class Program
{
static void Main(string[] args)
{
using (ServiceHost serviceHostPerCall = new ServiceHost(typeof(ServicePerCall)))
using (ServiceHost serviceHostPerSession = new ServiceHost(typeof(ServicePerSession)))
using (ServiceHost serviceHostSingle = new ServiceHost(typeof(ServiceSingle)))
{
serviceHostPerCall.Open();
serviceHostPerSession.Open();
serviceHostSingle.Open(); Console.WriteLine("所有服务已经启动: {0}.", DateTime.Now);
Console.ReadLine();
Console.WriteLine("所有服务已经关闭: {0}.", DateTime.Now); serviceHostPerCall.Close();
serviceHostPerSession.Close();
serviceHostSingle.Close();
}
}
}
}
对应的配置文件:
<?xml version="1.0" encoding="utf-8" ?>
<configuration> <system.web>
<compilation debug="true" />
</system.web>
<system.serviceModel>
<services>
<service name="Keasy5.WCF.WCFService.ServicePerCall" behaviorConfiguration="textserviceBehaviors">
<host>
<baseAddresses>
<add baseAddress = "http://localhost:5833/Keasy5.WCF.WCFService/ServicePerCall/" />
</baseAddresses>
</host>
<endpoint name="ServicePerCall" address="" binding="basicHttpBinding" contract="Keasy5.WCF.WCFService.IService1"></endpoint>
</service> <service name="Keasy5.WCF.WCFService.ServicePerSession" behaviorConfiguration="textserviceBehaviors">
<host>
<baseAddresses>
<add baseAddress = "http://localhost:5833/Keasy5.WCF.WCFService/ServicePerSession/" />
</baseAddresses>
</host>
<endpoint name="" address="" binding="basicHttpBinding" contract="Keasy5.WCF.WCFService.IService1"></endpoint>
</service> <service name="Keasy5.WCF.WCFService.ServiceSingle" behaviorConfiguration="textserviceBehaviors">
<host>
<baseAddresses>
<add baseAddress = "http://localhost:5833/Keasy5.WCF.WCFService/ServiceSingle/" />
</baseAddresses>
</host>
<endpoint name="ServiceSingle" address="" binding="basicHttpBinding" contract="Keasy5.WCF.WCFService.IService1"></endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors >
<behavior name="textserviceBehaviors">
<serviceMetadata httpGetEnabled="True"/>
<serviceDebug includeExceptionDetailInFaults="True" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel> </configuration>
客户端调用
获取WCF服务的一个封装类:
ServiceProxy.cs
using System.ServiceModel; namespace ByteartRetail.Infrastructure.Communication
{
/// <summary>
/// 表示用于调用WCF服务的客户端服务代理类型。
/// </summary>
/// <typeparam name="T">需要调用的服务契约类型。</typeparam>
public sealed class ServiceProxy<T> : DisposableObject
where T : class, IApplicationServiceContract
{
#region Private Fields
private T client = null;
private static readonly object sync = new object();
#endregion #region Protected Methods
protected override void Dispose(bool disposing)
{
if (disposing)
lock (sync)
{
Close();
}
}
#endregion #region Public Properties
/// <summary>
/// 获取调用WCF服务的通道。
/// </summary>
public T Channel
{
get
{
lock (sync)
{
if (client != null)
{
var state = (client as IClientChannel).State;
if (state == CommunicationState.Closed)
client = null;
else
return client;
}
var factory = ChannelFactoryManager.Instance.GetFactory<T>();
client = factory.CreateChannel();
(client as IClientChannel).Open();
return client;
}
}
}
#endregion #region Public Methods
/// <summary>
/// 关闭并断开客户端通道(Client Channel)。
/// </summary>
/// <remarks>
/// 如果使用using语句对ServiceProxy进行了包裹,那么当程序执行点离开using的
/// 覆盖范围时,Close方法会被自动调用,此时客户端无需显式调用Close方法。反之
/// 如果没有使用using语句,那么则需要显式调用Close方法。
/// </remarks>
public void Close()
{
if (client != null)
((IClientChannel)client).Close();
}
#endregion
}
}
ChannelFactoryManager.cs
using System;
using System.Collections.Generic;
using System.ServiceModel; namespace ByteartRetail.Infrastructure.Communication
{
/// <summary>
/// 表示Channel Factory管理器。
/// </summary>
internal sealed class ChannelFactoryManager : DisposableObject
{
#region Private Fields
private static readonly Dictionary<Type, ChannelFactory> factories = new Dictionary<Type, ChannelFactory>();
private static readonly object sync = new object();
private static readonly ChannelFactoryManager instance = new ChannelFactoryManager();
#endregion #region Ctor
static ChannelFactoryManager() { }
private ChannelFactoryManager() { }
#endregion #region Public Properties
/// <summary>
/// 获取<c>ChannelFactoryManager</c>的单件(Singleton)实例。
/// </summary>
public static ChannelFactoryManager Instance
{
get { return instance; }
}
#endregion #region Protected Methods
protected override void Dispose(bool disposing)
{
if (disposing)
{
lock (sync)
{
foreach (Type type in factories.Keys)
{
ChannelFactory factory = factories[type];
try
{
factory.Close();
continue;
}
catch
{
factory.Abort();
continue;
}
}
factories.Clear();
}
}
}
#endregion #region Public Methods
/// <summary>
/// 获取与指定服务契约类型相关的Channel Factory实例。
/// </summary>
/// <typeparam name="T">服务契约的类型。</typeparam>
/// <returns>与指定服务契约类型相关的Channel Factory实例。</returns>
public ChannelFactory<T> GetFactory<T>()
where T : class, IApplicationServiceContract
{
lock (sync)
{
ChannelFactory factory = null;
if (!factories.TryGetValue(typeof(T), out factory))
{
factory = new ChannelFactory<T>(typeof(T).Name);
factory.Open();
factories.Add(typeof(T), factory);
}
return factory as ChannelFactory<T>;
}
}
#endregion
}
}
客户端调用:
using (ServiceProxy<IProductService> proxy = new ServiceProxy<IProductService>())
{
var categories = proxy.Channel.GetCategories(QuerySpec.Empty);
return View(categories);
}
客户端配置:
注意到ChannelFactoryManager类中创建工厂的方法:
public ChannelFactory<T> GetFactory<T>()
{
。。。。
factory = new ChannelFactory<T>(typeof(T).Name);
注意到ChannelFactoryManager类中创建工厂的方法,使用的是类型的名称typeof(T).Name)作为节点的配置名。
ChannelFactory<T>构造函数查看参数说明:
//
// 摘要:
// 使用指定的终结点配置名称初始化 System.ServiceModel.ChannelFactory<TChannel> 类的新实例。
//
// 参数:
// endpointConfigurationName:
// 用于终结点的配置名称。
//
// 异常:
// System.ArgumentNullException:
// endpointConfigurationName 为 null。
public ChannelFactory(string endpointConfigurationName);
故,客户端的配置EndPointd配置节点名要使用要用类型的名称,
<endpoint name="IProductService" 。。。
例如:
<endpoint name="IProductService" address="http://localhost:8558/ProductService.svc" contract="ByteartRetail.ServiceContracts.IProductService" binding="basicHttpBinding" bindingConfiguration="ServiceProxyBinding" />
客户端的配置:
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="ServiceProxyBinding" sendTimeout="00:10:00" receiveTimeout="00:10:00" closeTimeout="00:10:00" maxReceivedMessageSize="" maxBufferSize="">
<readerQuotas maxStringContentLength="" />
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint name="IProductService" address="http://localhost:8558/ProductService.svc" contract="ByteartRetail.ServiceContracts.IProductService" binding="basicHttpBinding" bindingConfiguration="ServiceProxyBinding" />
<endpoint name="IOrderService" address="http://localhost:8558/OrderService.svc" contract="ByteartRetail.ServiceContracts.IOrderService" binding="basicHttpBinding" bindingConfiguration="ServiceProxyBinding" />
<endpoint name="IUserService" address="http://localhost:8558/UserService.svc" contract="ByteartRetail.ServiceContracts.IUserService" binding="basicHttpBinding" bindingConfiguration="ServiceProxyBinding" />
<endpoint name="IPostbackService" address="http://localhost:8558/PostbackService.svc" contract="ByteartRetail.ServiceContracts.IPostbackService" binding="basicHttpBinding" bindingConfiguration="ServiceProxyBinding" />
</client>
</system.serviceModel>
【WCF--初入江湖】01 WCF编程概述的更多相关文章
- WCF 4.0 如何编程修改wcf配置,不使用web.config静态配置
How to programmatically modify WCF without web.config setting WCF 4.0 如何编程修改wcf配置,不使用web.config静态配置 ...
- WCF入门一[WCF概述]
一.什么是WCF WCF是使用托管代码建立和运行面向服务(Service Oriented)应用程序的统一框架.它使得开发者能够建立一个跨平台的.安全.可信赖.事务性的解决方案,且能与已有系统兼容协作 ...
- WCF入门三[WCF宿主]
一.概述 WCF程序必须在宿主上运行,也就是WCF服务必须寄宿在某一个windows的进程中,可以是IIS.控制台程序.窗体程序.WAS以及所有.net程序等程序进程中.在我用VS2013创建WCF服 ...
- WCF学习笔记——WCF基础
一 WCF与SOA SOA是一种通过为所有软件提供服务外观,并将这些服务的WSDL集中发布到一个地方的一种组织企业软件的方法.它通过使用明确定义的接口通过跨越边界传递消息来让多个自治的服务协同工作.S ...
- 第01章 Spring概述
第01章 Spring概述 1.Spring概述 ①Spring是一个开源框架 ②Spring为简化企业级开发而生,使用Spring,JavaBean就可以实现很多以前要靠EJB才能实现的功能.同样的 ...
- WCF服务与WCF数据服务的区别
问: Hi, I am newbie to wcf programming and a little bit confused between WCF Service and WCF Data Se ...
- Android网络编程概述
Android网络编程概述 首先,应该了解的几个问题: 1)Android平台网络相关API接口 a) java.net.*(标准Java接口) java.net.*提供与联网有关的类,包括流.数据包 ...
- [C#] 《Concurrency in C# Cookbook》读书笔记(一)- 并发编程概述
并发编程概述 前言 我们经常在耳边听说一些关于高性能.并发和并行等名词,并且很多人都对并发编程有一些误解. 误解 1:并发就是多线程? 答:多线程只不过是并发编程的其中一种形式而已.并发编程的种类很多 ...
- C# 图解教程 第二章 C#编程概述
C#编程概述 一个简单的C#程序标识符关键字Main:程序的起始点从程序输出文本注释 C#编程概述 一个简单的C#程序 标识符 标识符是一种字符串,用来命名变量.方法.参数和许多后面将要阐述的其他程序 ...
随机推荐
- JS正则表达式基础总结
定义正则: 1 var re = new RegExp(“a”); //RegExp对象.参数就是我们想要制定的规则.有一种情况必须用这种方式,下面会提到. 2 var re = /a/; // 简写 ...
- DB2&&oracle-培训内容
DB2 1 Data Sharing架构,高可用 2 DB2的对象主要有以下几类:database,storage group, Tablspace, indexspace, table,index ...
- Java--CJDP
was定义,包定义, 1. Java的接口概念 进行封装,方便的使用 2. 包定义,Java 中多种包,进行迁移使用,包的导入,例如对数据库的操作Hibernate 3. 配置文件xml和json,对 ...
- 为一个有数据没有主键id的数据表添加主键字段
ALTER TABLE `photo_feedbacks` ADD COLUMN `id` int(11) NOT NULL AUTO_INCREMENT FIRST ,ADD PRIMARY KE ...
- Nginx之负载均衡
转自:http://www.360doc.com/content/13/1114/12/7694408_329125489.shtml 注,大家可以看到,由于我们网站是发展初期,nginx只代理了后端 ...
- 封装cookie
function cookie(name,value,expires){ switch(typeof value){ case 'string': //设置 var exp=''; if(expire ...
- PHP 5.3.X 连接MS SQL Server php_mssql.dll
在网上搜索了一下PHP 5.3.X 连接SQL Server的办法,有人也遇到了这个问题 原来PHP 团队在PHP 5.3 中移除了SQL Server的驱动和库,而微软自己开发了针对PHP的SQL驱 ...
- C string 函数大全
PS:本文包含了大部分strings函数的说明,并附带举例说明.本来想自己整理一下的,发现已经有前辈整理过了,就转了过来.修改了原文一些源码的问题,主要是用char *字义字符串的问题,导致程序运行时 ...
- 修改linux命令行提示符
安装了ubuntu1304版本,发现命令行@后面的名称太长,影响视觉美观,决定修改一下.修改当前用户目录下面的.bashrc文件即可达到目的. 打开.bashrc文件,找到下面的内容:if [ &qu ...
- WPF 使用定时器
WPF 使用定时器:<ProgressBar Height="10" HorizontalAlignment="Left" Margin="28 ...