WCF:Windows Communication Foundation
一、特点概述:
1.统一ASMX, .NET Remoting, 与EnterpriseServices的开发模型:①为各种应用提供单一的编程模型;②基于配置驱动的协议选择,消息格式,进程分配等等
2.面向服务:①构建面向服务的系统设计② 简化实现SOA的方法
3.松耦合:①并没有限制在特定的协议,编码格式,或者主机环境上②所有的选项都可配置
4.可交互:支持Web Services的核心标准
5.已经批准和还未被批准的内容:在可扩展性方面能够快速适用新的协议和更新
6.整合性: 整合Microsoft早期的技术如:COM, Enterprise Services, MSMQ
二、原理示意图:
实现代码:
代码结构
简单的业务逻辑类:HelloIndigoService.cs
using System;
using System.ServiceModel;
namespace HelloIndigo
{
[ServiceContract(Namespace="http://www.monkeyfu.net")]
public interface IHelloIndigoService
{
[OperationContract]
string HelloIndigo(string message);
}
public class HelloIndigoService : IHelloIndigoService
{
#region IHelloIndigoService Members
public string HelloIndigo(string message)
{
return string.Format("Receivied message at{0}:{1}", DateTime.Now, message);
}
#endregion
}
}
服务端:
using System; using System.Collections.Generic; using System.Text; //WCF使用到的命名空间 using System.ServiceModel; using System.ServiceModel.Dispatcher; namespace Host { class Program { static void Main(string[] args) { using (ServiceHost host = new ServiceHost(typeof(HelloIndigo.HelloIndigoService))) { host.AddServiceEndpoint(typeof(HelloIndigo.IHelloIndigoService), new NetTcpBinding(), "net.tcp://localhost:9000/HelloIndigo"); host.Open(); Console.ReadLine(); } } } }
using System; using System.Collections.Generic; using System.Text; using System.ServiceModel; using Client.localhost; namespace Client { [ServiceContract(Namespace = "http://www.monkeyfu.net")] public interface IHelloIndigoService { [OperationContract] string HelloIndigo(string message); } class Program { static void Main(string[] args) { IHelloIndigoService proxy = ChannelFactory<IHelloIndigoService>.CreateChannel(new NetTcpBinding(), new EndpointAddress("net.tcp://localhost:9000/HelloIndigo")); string s = proxy.HelloIndigo("Hello from client..."); Console.WriteLine(s); Console.ReadLine(); } } }
<endpoint binding="mexHttpBinding" contract="IMetadataExchange" address="mex"/>