假定我们要为一家汽车租赁公司开发一个新的应用程序,用于租车预约服务。该租车预约服务会被多咱应用程序访问,包括:呼叫中心(Call Center),基于J2EE的租车预约服务以及合作伙伴的应用程序(Partner Application)

使用WCF,该解决方案的实现就容易多了。如图中所示,WCF可用于前述所有情况。因此,租车预定应用程序使用这一种技术就可以实现其所有应用程序间的通信。
WCF可使用Web服务进行通信,因此与同样支持SOAP的其他平台(例如基于J2EE的主流应用程序服务器)间的互操作性就变得简单明了。
还可以对WCF进行配置和扩展,以便与使用并非基于SOAP的消息的Web服务进行通信。
性能是大多数业务中至关重要的考虑事项。开发WCF的目标就是要使之成为Microsoft所开发的速度最快的分布式应用程序平台之一。
WCF是提供统一的,可用于建立安全、可靠的面向服务的应用的高效开发平台。
WCF具有如下的优势:
1、统一性
2、互操作性
3、安全与可信赖
4、兼容性
实例:
新建项目 选择wcf应用程序
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Runtime.Serialization;
- using System.ServiceModel;
- using System.Text;
-
- namespace WcfService1
- {
-
- [ServiceContract]
- public interface IService1
- {
-
- [OperationContract]
- string GetData(int value);
-
- [OperationContract]
- CompositeType GetDataUsingDataContract(CompositeType composite);
-
-
- [OperationContract]
- string HelloWorld();
-
- }
-
-
-
- [DataContract]
- public class CompositeType
- {
- bool boolValue = true;
- string stringValue = "Hello ";
-
- [DataMember]
- public bool BoolValue
- {
- get { return boolValue; }
- set { boolValue = value; }
- }
-
- [DataMember]
- public string StringValue
- {
- get { return stringValue; }
- set { stringValue = value; }
- }
- }
- }
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace WcfService1
{
// 注意: 如果更改此处的接口名称 "IService1",也必须更新 Web.config 中对 "IService1" 的引用。
[ServiceContract]
public interface IService1
{
[OperationContract]
string GetData(int value);
[OperationContract]
CompositeType GetDataUsingDataContract(CompositeType composite);
// 任务: 在此处添加服务操作
[OperationContract]
string HelloWorld();
}
// 使用下面示例中说明的数据约定将复合类型添加到服务操作。
[DataContract]
public class CompositeType
{
bool boolValue = true;
string stringValue = "Hello ";
[DataMember]
public bool BoolValue
{
get { return boolValue; }
set { boolValue = value; }
}
[DataMember]
public string StringValue
{
get { return stringValue; }
set { stringValue = value; }
}
}
}
实现接口:、
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Runtime.Serialization;
- using System.ServiceModel;
- using System.Text;
-
- namespace WcfService1
- {
-
- public class Service1 : IService1
- {
-
-
-
-
- #region IService1 成员
-
-
- string IService1.HelloWorld()
- {
- return "Hello WCF!";
- }
-
- #endregion
-
- #region IService1 成员
-
- public string GetData(int value)
- {
- throw new NotImplementedException();
- }
-
- public CompositeType GetDataUsingDataContract(CompositeType composite)
- {
- throw new NotImplementedException();
- }
-
- #endregion
- }
- }
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace WcfService1
{
// 注意: 如果更改此处的类名“Service1”,也必须更新 Web.config 和关联的 .svc 文件中对“Service1”的引用。
public class Service1 : IService1
{
#region IService1 成员
string IService1.HelloWorld()
{
return "Hello WCF!";
}
#endregion
#region IService1 成员
public string GetData(int value)
{
throw new NotImplementedException();
}
public CompositeType GetDataUsingDataContract(CompositeType composite)
{
throw new NotImplementedException();
}
#endregion
}
}
新建winform项目添加服务

- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
-
- namespace WindowsFormsApplication1
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
-
- private void button1_Click(object sender, EventArgs e)
- {
-
- ServiceReference1.Service1Client client = new WindowsFormsApplication1.ServiceReference1.Service1Client();
- MessageBox.Show(client.HelloWorld());
-
- }
- }
- }
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
ServiceReference1.Service1Client client = new WindowsFormsApplication1.ServiceReference1.Service1Client();
MessageBox.Show(client.HelloWorld());
}
}
}

第一天对WCF的认识就到这里。明天继续学习 。。。。。。。。。。。。。。。