WCF开发实战教程

WCF开发实战系列教程

 

1:创建第一个WCF服务

在这个实战中我们将使用DataContract,ServiceContract来构建WCF服务,并使用VS2008内置的“WCFSVCHost”运行我们创建的WCF服务,并使用“WCF测试客户端来测试我们创建的服务。
在此WCF服务中我们将建立一个关于Book的服务,并实现对Book的添加、删除和检索操作。

第一步:创建“WCF服务库
文件(F)”->“新建项目(P)...”打开新建项目对话框。在左侧的项目类型中选择“WCF”,然后再在右侧的模板中选择“WCF服务库
在下面的名称文本框中,填写我们要创建的WCF服务库的项目名称“Services”

《图1
点击确定,会创建出我们的WCF服务库项目,在解决方案中会自动为我们生成两个类文件“IService.cs”“Service.cs”

《图2
这两个类文件是两个WCF示例文件,对我们开发没有什么用处,现在我们删掉这两个文件。

第二步:创建Book实体类
解决方案窗口中,我们右击Services项目名,选择添加,再单击


《图3
在弹出的添加新项窗口中,选择,并在名称文本框中写入项名称“Book.cs”


《图4

第三步:为Book实体类编写代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
usingSystem.Runtime.Serialization;

namespace Services
{
    
[DataContract]
    public class Book
    {
        [DataMember]
        public string BookNO;
        [DataMember]
        public string BookName;
       [DataMember]
        public decimal BookPrice;
    }
}
为了保证此类在WCF调用中能够被序列化,我们在Book类上面加入[DataContract]标签,在每个需要序列化的成员变量上加入[DataMember]标签。这两个标签在使用的进候需要导入using System.Runtime.Serialization命名空间。
到此为至,我们创建完了需要在服务中传输的复杂的数据类型Book

第四步:创建服务接口
创建服务接口,声明对外发布的类和方法。
解决方案窗口中,我们右击Services项目名,选择添加,再单击



《图3
在弹出的添加新项窗口中,选择,并在名称文本框中写入项名称“IBookService.cs”


《图5
在此类文件中我们编写服务接口,代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
usingSystem.ServiceModel;

namespace Services
{
   
[ServiceContract]
    public interface IBookService
    {
        [OperationContract]
        void AddBooks(Bookbook);
       [OperationContract]
        List<Book>GetAllBooks();
        [OperationContract]
        void RemoveBook(stringid);
    }
}
IBookService接口上面,我们定义了[ServiceContract]标签,此标签代表此接口及实现此接口的类都是对外发布的Service类,在每个需要对外发布的方法上都加上[OperationContract]标签,以使外部可以访问到此方法。
[ServiceContract]
[OperationContract]这两个标签需要导入using System.ServiceModel命名空间。

第五步:创建实现服务接口的类
实现我们上面声明的服务接口,实现对Book的添加、删除和检索的具体功能。
解决方案窗口中,我们右击Services项目名,选择添加,再单击



《图3
在弹出的添加新项窗口中,选择,并在名称文本框中写入项名称“BookService.cs”


《图6
在此类文件中编写代码实现IBookService.cs服务接口。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
usingSystem.ServiceModel;

namespace Services
{
    
[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]
    public class BookService : IBookService
    {
        List<Book> _Books = newList<Book>();
        public void AddBooks(Bookbook)
        {
            book.BookNO= Guid.NewGuid().ToString();
           _Books.Add(book);
        }

        publicList<Book> GetAllBooks()
        {
            return_Books;
        }

        public void RemoveBook(stringid)
        {
            Book book =_Books.Find(p => p.BookNO == id);
           _Books.Remove(book);
        }
    }
}
此类是对IBookService接口的具体实现,在此类的上面我们声明了[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]标签,此标签代表这个类采用SingleTone(单类模式)来生成对象。
使用[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]接口需要导入using System.ServiceModel;命名空间。

第六步:配置WCF服务
到目前为至,我们建立好了WCF服务,那我们如何让WCFSVCHost(WCF服务主机)理解我们编写的服务类,并能够运行我们编写的服务呢。这需要我们在App.Config里面注册一下我们的WCF服务。
代码如下:
<system.serviceModel>
    <services>
      <servicebehaviorConfiguration="Services.Service1Behavior"name="Services.BookService">
        <endpoint address=""binding="wsHttpBinding"contract="Services.IBookService">
          <identity>
            <dnsvalue="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex"binding="mexHttpBinding" contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <addbaseAddress="
http://localhost:8731/Design_Time_Addresses/Services/Service1/" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behaviorname="Services.Service1Behavior">
          <!--
为避免泄漏元数据信息,
         
请在部署前将以下值设置为 false 并删除上面的元数据终结点 -->
          <serviceMetadatahttpGetEnabled="True"/>
          <!--
要接收故障异常详细信息以进行调试, 
         
请将下值设置为 true。在部署前 
           
设置为 false以避免泄漏异常信息-->
          <serviceDebugincludeExceptionDetailInFaults="False" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
</system.serviceModel>

这么多东西谁能记得下?!!!
没关系,VS2008为我们提供了可视化的操作界面。
Services项目中右击“App.Config”配置文件,在弹出的右键菜单中选择编辑WCF配置


《图7

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值