WCF服务

  Windows Communication Foundation (WCF)是由微软发展的一组数据通信的应用程序开发接口,它是.NET框架的一部分,由.NET Framework 3.0开始引入,与Windows Presentation Foundation及 Windows Workflow Foundation并行为新一代Windows操作系统以及WinFX的三个重大应用程序开发类库。

  WCF由于集合了几乎由.NET Framework所提供的通信方法,因此学习曲线比较陡峭,开发人员必须要针对各个部份的内涵做深入的了解,才能够操控WCF来开发应用程序。

  • 通信双方的沟通方式,由合约来订定。
  • 通信双方所遵循的通信方法,由协定绑定来订定。
  • 通信期间的安全性,由双方约定的安全性层次来订定。

  

合约(Contract)

WCF的基本概念是以合约(Contract)来定义双方沟通的协定,合约必须要以接口的方式来呈现,而实际的服务代码必须要由这些合约接口派生并实现。合约分成了四种:

  1. 数据合约(Data Contract),订定双方沟通时的数据格式。
  2. 服务合约(Service Contract),订定服务的定义。
  3. 营运合约(Operation Contract),订定服务提供的方法。
  4. 信息合约(Message Contract),订定在通信期间改写信息内容的规范。

  

协定绑定(Binding)

由于WCF支持了HTTPTCPNamed PipeMSMQ,Peer-To-Peer TCP等协定,而HTTP又分为基本HTTP支持(BasicHttpBinding)以及WS-HTTP支持(WsHttpBinding),而TCP亦支持NetTcpBinding,NetPeerTcpBinding等通信方式,因此,双方必须要统一通信的协定,并且也要在编码以及格式上要有所一致。

一个设置通信协定绑定的示例如下:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <!-- 設定服務繫結的資訊 -->
    <services>
      <service name=" CalculatorService" >
        <endpoint address="" binding="wsHttpBinding" bindingConfiguration="Binding1"
            contract="ICalculator" />
      </service>
    </services>
    <!-- 設定通訊協定繫結的資訊 -->
    <bindings>
      <wsHttpBinding>
        <binding name="Binding1">
        </binding>
      </wsHttpBinding>
   </bindings>
  </system.serviceModel>
</configuration>

虽然WCF也可以使用SOAP做通信格式,但它和以往的ASP.NET XML Web Services不同,因此有部份技术文章中,会将ASP.NET的XML Web Services称为ASMX Service

WCF的服务可以挂载于Console Application,Windows Application,IIS(ASP.NET)Application,Windows Service以及Windows Activation Services中,但大多都会挂在Windows Service。

安全性层次

WCF实现上已经支持了传输层次安全性(Transport-level security)以及信息层次安全性(Message-level security)两种。

  • 传输层次安全性:在数据传输时期加密,例如SSL
  • 信息层次安全性:在数据处理时就加密,例如使用数字签章散列或是使用金钥加密法等。

--------------------------------------------------------------------------------------------------------------------------

以上为概念介绍,下面我们自己来创建 一个简单的demo。

首先,我们新建一个空白项目,紧接着创建一个控制台应用程序,在控制台应用程序下创建一个WCF服务命名问WCFService,如下。

  

  服务建好后,我们可以看到系统自动为我们创建了3个文件,分别为:app.config、IWCFService.cs、WCFService.cs。

  打开IWCFService,这是一个接口累,稍加修改如下:

using System.ServiceModel;
using System.Text;

namespace Host
{
    // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IWCFService”。
    [ServiceContract]  // 服務合約
    public interface IWCFService
    {
        [OperationContract]  //营运合约
        string DoWork(string msg);
    }

  在这里我们可以创建WCF合约。

  然后打开WCFService.cs文件,这个类乃是对接口的实现。

using System.ServiceModel;
using System.Text;

namespace Host
{
    // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的类名“WCFService”。
    public class WCFService : IWCFService
    {
        public string DoWork(string msg)
        {
            return string.Format("你在{0}收到信息:{1}:",DateTime.Now.ToString(),msg);
        }
    }
}

  最后需要在Program中调用:

using System.ServiceModel;
namespace Host
{
    class Program
    {
        static void Main(string[] Args)
        {
            ServiceHost host = new ServiceHost(typeof(Host.WCFService));
            host.Open();
            Console.WriteLine("WCF服务已启动!");
            Console.ReadLine();
            host.Close();

        }
    }
}

从上面贴的代码不难看出System.ServiceModel程序集是必须的。这样一个简单的WCF服务就创建了,在客户端调用就不说了,另外我们还可以用多个程序集来创建WCF。

  在这里,我们先建两个类库(Server、Contracts),Contracts不明思议,就是合约,在这个程序集中创建各种我们想要的合约,然后在Server中继承并实现合约,这里需要在Server程序集中引用Contracts程序集。 完后继续创建一个控制台应用程序,在这里实现WCF。

  对于多个WCF服务,我们则需要配置app.config,这样才能找到服务节点,如下:

 <system.serviceModel>
        <behaviors>
            <serviceBehaviors>
                <behavior name="">
                    <serviceMetadata httpGetEnabled="true" />
                    <serviceDebug includeExceptionDetailInFaults="false" />
                </behavior>
            </serviceBehaviors>
        </behaviors>
        <services>
            <service name="Host.WCFService">
                <endpoint address="" binding="wsHttpBinding" contract="Host.IWCFService">
                    <identity>
                        <dns value="localhost" />
                    </identity>
                </endpoint>
                <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
                <host>
                    <baseAddresses>
                        <add baseAddress="http://localhost:8732/Design_Time_Addresses/Host/WCFService/" />
                    </baseAddresses>
                </host>
            </service>
          <service name="Host.NewServer">
            <endpoint address="" binding="wsHttpBinding" contract="Contracts.INewServer">
              <identity>
                <dns value="localhost" />
              </identity>
            </endpoint>
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
            <host>
              <baseAddresses>
                <add baseAddress="http://localhost:8732/Design_Time_Addresses/Host/NewService/" />
              </baseAddresses>
            </host>
          </service>
        </services>
    </system.serviceModel>

  有兴趣的话可以将Asp.net作为宿主搭建一个WCF服务。

posted @ 2013-03-12 11:43  诸葛风流  阅读(317)  评论(0编辑  收藏  举报