`
terryfeng
  • 浏览: 492246 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

WCF 第一个用 Visual Studio 2010 创建的WCF服务

阅读更多

在这个例子中我们将使用VS 2010 U 创建一个WCF服务,其中会了解

[DataContract] [ServiceContract] 等特性。 

内置的 WCFSVCHost ,并使用“WCF测试客户端”来测试我们创建的服务。

注意下面的所有类、接口及方法都添加了public 的访问级别。

 

一,建立一个WCF服务库

创建一个WCF服务库项目,

2009-12-20_162938 

在解决方案中会自动为我们生成两个类文件“IService.cs”和“Service.cs”。

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

2009-12-20_171351

在弹出的“添加新项”窗口中,选择“类”,并在“名称”文本框中写入项名称“Person.cs”。

2009-12-20_171438

2009-12-20_171457

Person.cs

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

namespace WcfServiceLibrary1
{
    /
    /// <summary>
    /// DataContract数y据Y约?定¨,?Person 就í是?传?递Y的?消?息¢中D的?内ú容Y。£好?比è信?中D的?文?字?。£
    /// 为a了?保£证¤此?类à在úWCF调÷用?中D能ü够?被?序ò列D化ˉ,?我ò们?在úBook类à上?面?加ó入?[DataContract]标ê签?,?在ú每?个?需è要a序ò列D化ˉ的?成é员±变?量?上?加ó入?[DataMember]标ê签?。£
    /// 这a两?个?标ê签?在ú使1用?的?进?候ò需è要a导?入?using System.Runtime.Serialization命ü名?空?间?。£
    /// </summary>
    [DataContract]
    public class Person
    {
        //数y据Y成é员±
        [DataMember]
        public string Id;

        [DataMember]
        public string Name;

        [DataMember]
        public int Age;
    }
}


    /// DataContract数据约定,Person 就是传递的消息中的内容。好比信中的文字。
    /// 为了保证此类在WCF调用中能够被序列化,我们在Book类上面加入[DataContract]标签,在每个需要序列化的成员变量上加入[DataMember]标签。
    /// 这两个标签在使用的进候需要导入using System.Runtime.Serialization命名空间。
由于注释又出现乱码,估计是字符版本的问题,下面是用黑体复制

 

创建服务接口,声明对外发布的类和方法。

IPersonService.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.ServiceModel;

namespace WcfServiceLibrary1
{
    /// <summary>
    /// ServiceContract:服务约定,代表我们所能操作的接口集合,提供功能点。
    /// 在IPersonService接口上面,我们定义了[ServiceContract]标签,此标签代表此接口及实现此接口的类都是对外发布的Service类,
    /// 在每个需要对外发布的方法上都加上[OperationContract]标签,以使外部可以访问到此方法。
    /// [ServiceContract]和[OperationContract]这两个标签需要导入using System.ServiceModel命名空间。
    /// </summary>
    [ServiceContract]
    public interface IPersonService
    {
        /// <summary>
        /// OperationContract 操作约定,定义每个操作的接口点方法。
        /// </summary>
        /// <param name="person">要添加的人员实体</param>
        [OperationContract]
        void AddPerson(Person person);

        [OperationContract]
        List<Person> GetAllPersons();

        [OperationContract]
        void RemovePerson(string id);
    }
}

 

 

实现我们上面声明的服务接口,实现对Book的添加、删除和检索的具体功能。

PersonService.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.ServiceModel;

namespace WcfServiceLibrary1
{
    /// <summary>
    /// 此类是对IBookService接口的具体实现,在此类的上面我们声明了[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]标签,
    /// 此标签代表这个类采用SingleTone(单类模式)来生成对象。
    /// 使用[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]接口需要导入using System.ServiceModel;命名空间。
    /// </summary>
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
    public class PersonService : IPersonService
    {
        List<Person> _Persons = new List<Person>();
        public void AddPerson(Person person)
        {
            person.Id = Guid.NewGuid().ToString();
            _Persons.Add(person);
        }

        public List<Person> GetAllPersons()
        {
            return _Persons;
        }

        public void RemovePerson(string id)
        {
            //拉姆达语句,谓词 p.Id == id
            Person person = _Persons.Find(p => p.Id == id);

            _Persons.Remove(person);
        }
    }
}

Ctrl + Shift + B 编译一下

二,配置服务运行

到目前为至,我们建立好了WCF服务,那我们如何让WCFSVCHost(WCF服务主机)理解我们编写的服务类,并能够运行我们编写的服务呢。这需要我们在App.Config里面注册一下我们的WCF服务。

VS为我们提供了可视化的操作界面。
在Services项目中右击“App.Config”配置文件,在弹出的右键菜单中选择“编辑WCF配置”。

2009-12-20_172922

打开之后如下图

2009-12-20_173525

在此界面中暴露两个对外的终结点(外部可以访问到的类或接口),其中下面一个是元数据终结点,用来向外提供服务信息的终结点。

而另一个(即上面的终结点),是向外公布我们编写的[ServiceContract]的类,但我们可以看到它的Contract还是我们在第一步中删掉的WcfServiceLibrary1.IService1这个终结点。

不仅如此,在右侧上面的黑字的服务中还依旧是我们在第一步中删除的WcfServiceLibrary1.Service1服务。这说明虽然在第一步中我们删除了那两个自动生成的类文件,但配置文件中仍没有删除这两个类文件的配置信息。

下面我们把它们改变一下。

单击左侧的“服务”-“WcfServiceLibrary1.Service1”在右侧的Name,弹出“服务类型浏览器”对话框,在此类型中我们找到此WCF服务项目编译出来的WcfServiceLibrary1.dll文件,双击它就可以出现此服务中的对外公布的服务,点击选中它单击确定。

2009-12-20_174640

这样我们就可以把对外公司的服务改变为我们刚编写的服务了。
然后,我们展开左侧“服务”->“WcfServiceLibrary1.PersonService”->“终结点”,单击第一个“空名称”,从右边的“终结点属性”中的Contract中我们可以看到,这里的Contract仍然用的是WcfServiceLibrary1.IService1。

2009-12-20_175045

我们按照上面的做法,找到此WCF服务项目编译出来的WcfServiceLibrary1.dll,双击它找到里面对应的ServiceContract点击确定就可以了。

2009-12-20_175127

在高级目录树中,为服务行为配置命名,名字你自己决定。

2009-12-20_182903

 

在服务中选中,刚才的行为配置。

2009-12-20_183005

 

重点一定要记着保存,点击菜单“文件”-“保存”就可以把我们对App.Config的修改保存回配置文件了。

App.Config

 

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

  <system.web>
    <compilation debug="true" />
  </system.web>
  <system.serviceModel>
    <services>
      <service behaviorConfiguration="Services.ServiceBehavior" name="WcfServiceLibrary1.PersonService">
        <endpoint address="" binding="wsHttpBinding" contract="WcfServiceLibrary1.IPersonService">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8732/Design_Time_Addresses/WcfServiceLibrary1/IPersonService/" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="Services.ServiceBehavior">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

</configuration>

三,测试WCF

在Visual Studio 中为我们提供了测试WCF的工具,按F5启动WCF会出现两个东西
    一个是在右下角的托盘图标中会出现WCFSVCHost(WCF服务主机),它为我们在开发时候提供了一个运行WCF的服务器,用来为测试客户端提供WCF服务。

2009-12-20_175711

另一个是“WCF测试客户端”

“测试客户端”从WcfSVCHost中取得WCF服务的元数据,解析为左侧的“服务结构树”,从这里面我们可以看到此WCF服务为我们提供了一个服务契约“IPersonService”,此服务契约中对外提供了三个可调用的方法。

点击AddPerson()  方法 输入参数 点击 Invoke 调用

2009-12-20_182116

点击GetAllPersons() ,在Response 中我们看到了返回的结果。

2009-12-20_182132

 

源代码下载:WcfServiceLibrary1.rar

分享到:
评论
1 楼 studentsky 2011-11-20  
好文章,图文并茂!

相关推荐

    Visual Studio 2008开发新特性系列课程(7):使用WCF,WF,Cardspace创建互联的应用程序

    Visual Studio 2008开发新特性系列课程(7):使用WCF,WF,Cardspace创建互联的应用程序

    Visual Studio 2010 C# 从精通到入门 全部29个章节源码

    学完本书源码之后,会对C#有一个全面、透彻的理解,并能用它来构建Windows Presentation Foundation(WPF)应用程序,访问Microsoft SQL Server 数据库,开发ASP.NET Web 应用程序以及创建和使用Windows ...

    professional visual studio 2008

    Professional Visual Studio 2008Microsoft Visual Studio 2008 is the latest version in the ongoing evolution of the Integrated Development Environment (IDE), and this resource examines the diverse ...

    RiaServices(WCF RIA Services V1.0 for Silverlight 4 and Visual Studio 2010)

    WCF RIA Services simplifies the traditional n-tier application pattern by bringing together the ASP.NET and Silverlight platforms. RIA Services provides a pattern to write application logic that runs ...

    Visual Studio 2012 Cookbook

    - Get to grips with the practicality of the new changes with .NET 4.5, such as improving communications with WCF and Visual Studio 2012 - Detect duplicate code and develop Portable Class Libraries ...

    VisualStudio连接mysql数据库说明文档

    VisualStudio连接mysql数据库说明文档

    Visual Studio和C#的基本运用.pptx

    Visual Studio和C#的入门介绍PPT .NET框架是大家熟知的微软的开发平台,于2002年Visual Studio.NET 2002中,正式发布1.0版本。 .NET框架主要用于创建在Windows平台上运行的应用程序。它提供了必要编译时和运行时...

    DotNet平台与Visual Studio.Net开发工具新增功能介绍

    DotNet平台技术发展飞快,我花了一晚上时间,把DotNet平台与Visual Studio.Net开发工具新增功能整理了一下。梳理一下新技术脉络。共享给大家,共同交流学习新技术。 目录 1. .NET框架介绍 1.1.1. .NET Framework...

    Visual Basic 2010 .NET 4高级编程(第6版)

    第Ⅰ部分 语言结构和环境 第1章 Visual Studio 2010 第2章 对象和Visual Basic 第3章 定制对象 第4章 公共语言运行库 第5章 用Visual Basic进行声明式编程 第6章 异常处理和调试 第7章 测试驱动的开发第Ⅱ部分 业务...

    Microsoft Visual Studio 2015 Unleashed(SAMS,3ed,2015)

    Microsoft Visual Studio 2015 empowers you to write next-generation applications for any modern environment: mobile, web, cloud, universal Windows 10/8.x, database, and beyond. This end-to-end deep ...

    Microsoft Visual studio 2008 UNLEASHED

    The release of Visual Studio 2005 and Visual Studio Team Systems marked a major revision to the .NET development experience. It brought us code snippets, custom project templates, refactoring, data ...

    Visual Basic 2010 .NET 4高级编程(第6版)PART2

    第Ⅰ部分 语言结构和环境 第1章 Visual Studio 2010 第2章 对象和Visual Basic 第3章 定制对象 第4章 公共语言运行库 第5章 用Visual Basic进行声明式编程 第6章 异常处理和调试 第7章 测试驱动的开发第Ⅱ部分 业务...

    Visual Basic 2010 .NET 4高级编程(第6版) PART1

    第Ⅰ部分 语言结构和环境 第1章 Visual Studio 2010 第2章 对象和Visual Basic 第3章 定制对象 第4章 公共语言运行库 第5章 用Visual Basic进行声明式编程 第6章 异常处理和调试 第7章 测试驱动的开发第Ⅱ部分 业务...

    Visual.Basic.2010.&.NET4.高级编程(第6版)-文字版.pdf

    第1章 visual studio 2010 3 1.1 visual studio 2010:从express到ultimate的各种版本 4 1.2 visual basic的关键字和语法 7 1.2.1 控制台应用程序 10 1.2.2 从项目模板上创建项目 11 1.2.3 solution ...

    WCF 4高级编程.pdf

    《wcf 4高级编程》由微软mvp团队编写,可使读者对wcf 4中各种组件是如何相互支撑、进而提供一个完整的企业级分布式应用程序开发框架,有一个清晰而全面的理解。《wcf 4高级编程》详细介绍了wcf 4的各种技术,并通过3...

    [超全]C# 2010(C# 4.0)VS2010最新学习资料

    [超全]C# 2010(C# 4.0)VS2010最新学习资料 Visual Studio 2010 下载 最新版Visual Studio 2010 MSDN原版(内置Key/序列号) 全系列下载 MSDN Visual Studio 2010 全系列 RTM 英文原版光盘镜像下载 Visual Assist X ...

Global site tag (gtag.js) - Google Analytics