07 WCF服务架构平台

1.什么是WCF

Windows Communication Foundation( WCF),是微软通用的服务架构平台,其目的在于创建一个通用的Web Service平台,可以在各种不同的协议(TCP, UDP, HTTP)下使用,仅仅通过EndPoint的配置而不需要修改代码实现就能适应不同的工作环境,从而降低了分布式系统开发者的学习曲线,并统一开发标准。

 

2.WCF的优点

第一,开发的统一性。WCF是对于ASMX, Remoting,Enterprise Service,WSE,MSMQ,TCP开发等技术的整合。WCF是由托管代码编写,无论你是使用TCP通讯,Rmoting通讯还是Web Service ,我们都可以使用统一的模式进行开发,利用WCF来创建面向服务的应用程序。

第二,WCF能够实现多方互操作。它是使用 SOAP通信机制,这就保证了系统之间的互操作性,即使是运行不同开发语言,也可以跨进程、跨机器甚至于跨平台的通信。例如:使用J2EE的服务器(如WebSphere,WebLogic),应用程序可以在Windows操作系统进行调用,也可以运行在其他的 操作系统,如Sun Solaris,HP Unix,Linux等等。

第三,提供高效的安全与可信赖度,它可以使用不同的安全认证将WS-Security,WS-Trust和WS-SecureConversation等添加到SOAP消息中。

 

第四, WCF要求客户端保持一致的编程方式,不用考虑服务的位置。不管服务部署在本机上还是别的机器上,WCF不允许客户端直接与服务交互,即使它调用的是本地机器内存中的服务,WCF仍然使用远程编程模型的实例化方式,并使用代理。因而对于本地和远程方式而言,WCF都只需要维持相同的编程模型。这就使得开发者不会因为服务位置的改变影响客户端,同时还大大地简化了应用程序的编程模型。

 

3. WCF具体实例-服务接口

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace TerminalService
{
    [ServiceContract]
    public interface ITerminalService
    {
        /// <summary>
        /// 获取当前日夜模式标志及夜间节点wf,queue的服务器地址
        /// </summary>
        /// <returns></returns>
        [OperationContract]
        NightServerAddress GetDayNightFlagAndNightServer();
    }
}

 

4. WCF具体实例-数据合约

using System;
namespace TerminalService
{
    [DataContract]
    public class NightServerAddress
    {
        string dayNightFlag = “D";
        string wfValue = String.Empty;
        string queueValue = String.Empty;

        [DataMember]
        public string DayNightFlag
        {
            get { return dayNightFlag; }
            set { dayNightFlag = value; }
        }

        [DataMember]
        public string wf
        {
            get { return wfValue; }
            set { wfValue = value; }
        }

        [DataMember]
        public string queue
        {
            get { return queueValue; }
            set { queueValue = value; }
        }
    }
}

 

5. WCF具体实例-服务具体代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

namespace TerminalService
{
    public class TerminalService : ITerminalService
    {       
        public NightServerAddress GetDayNightFlagAndNightServer()
        {
          NightServerAddress obj = new NightServerAddress();
          obj.DayNightFlag = "D";
          obj.wf="http://22.11.143.88:8008";
          obj.queue="http://22.11.143.88:8009";
          return NightServerAddress;             
        }
    }
}

 

6. WCF具体实例-C#客户端调用方法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
namespace WebApp
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ServiceReference1.Service1Client client = new ServiceReference1.Service1Client();
            ServiceReference1.NightServerAddress result = client.GetDayNightFlagAndNightServer();
        }
     }
}

 

7. WCF具体实例-Http请求

POST http://22.11.143.88:9999/TerminalService.svc HTTP/1.1
Content-Type: text/xml; charset=utf-8
SOAPAction: "http://tempuri.org/ITerminalService/GetDayNightFlag"
Host: 22.11.143.88:9999
Content-Length: nnn
Expect: 100-continue
Connection: Keep-Alive

 <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Header>
    <Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://tempuri.org/IService1/GetDayNightFlagAndNightServer</Action>
  </s:Header>
  <s:Body>
    <GetDayNightFlagAndNightServer xmlns="http://tempuri.org/" />
  </s:Body>
</s:Envelope>

 

8. WCF具体实例-Http响应

HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Server: Microsoft-IIS/8.5
X-Powered-By: ASP.NET
Date: Mon, 08 Dec 2014 04:03:41 GMT
Content-Length: nnn

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Header />
  <s:Body>
    <GetDayNightFlagAndNightServerResponse xmlns="http://tempuri.org/">
      <GetDayNightFlagAndNightServerResult xmlns:a="http://schemas.datacontract.org/2004/07/WcfServiceDemo" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
        <a:DayNightFlag>D</a:DayNightFlag>
        <a:queue>http://22.11.143.88:8008</a:queue>
        <a:wf>http://22.11.143.88:8009</a:wf>
      </GetDayNightFlagAndNightServerResult>
    </GetDayNightFlagAndNightServerResponse>
  </s:Body>
</s:Envelope>

 

posted @ 2015-03-09 10:42  紫色物语  阅读(141)  评论(0编辑  收藏  举报