• (转)那天有个小孩教我WCF[一][1/3]


    原文地址:http://www.cnblogs.com/AaronYang/p/2950931.html

    既然是小孩系列,当然要有一点基础才能快速掌握,归纳,总结的一个系列,哈哈

    前言:

    第一篇嘛,不细讲,步步教你创建一个简单SOA案例,对WCF有个基本的认识,我不会细讲概念

    1.1 SOA简介

         SOA(Service-Oriented Architecture,面向服务架构),既是一种编程模式,也是软件开发的一种架构方法。

    根据这种架构方法,应用程序就会由“具有一定行为(服务)的功能”单元组成的。<如果你写过WebService,你就会更好理解了,如果你以前用三层形式写,那么原来的数据访问层,你只能供你本程序中的程序调用,假如你用了这种架构,例如,根据一个ISBN查询到对应书的价格,本来是一个方法,如果你用这个架构,这个方法(行为)就会变成一种服务,这个服务绑定到一个url地址上,然后你根据这个url加个特殊的参数(例如ISBN)就可以返回一个json或者xml,然后你就可以调用了,那么例如你的andriod平板,ipad,只要可以通过url就可以获得你想要的数据,就可以达到跨平台的效果,跟WebService很像,但是WCF完全不会是那么简单的>

     

    1.2 基本开始

    前言:

         我们准备完成一个简单的新闻发布系统

         内部编辑发布新闻有更多的功能,但是外部我只提供查看功能,添加自己的新的新闻,但是新闻类型是 好友提供

    可能由于我们的新闻系统比较好,我们提供一些接口,让别人自己做自己的客户端,也能实现发布新闻,但是我们的源代码不可能给人家的,所以我们就可以用WCF去做了,再比如,我们想做个iphone应用,别人下载我们的应用也可以查看我们发布的新闻,我们不可能在苹果手机上去访问数据库,我们通过服务去更新数据,中间可以利用WCF实现跨平台,现在我们开始做吧

    Contract是契约的意思,最常见的,务必背诵 

    1.打开Visual Studio 2010,新建一个空白的解决方案

    image

    命名NewsSystem,点击确定

    然后右击解决方案,添加4个解决方案文件夹

    Clients,Hosts,Services,Interfaces

    image

    image

    2.右击Interfaces,添加2个类库

    NewsInterface

    NewsExternalInterface

    image

    添加WCF中最重要的两个程序集引用

    imageimage

    同样的操作对NewsExternalInterface类库也操作下

    3. 删除默认添加的两个Class1.cs文件,分别对应向两个类库添加INewsInterface,INewsExternalInterface

    image

    4.向两个接口,都添加下面命名空间引用

    using System.Runtime.Serialization;
    using System.ServiceModel;

    5.interface前面加上 public关键字

    例如:

    imageimage

    6.实体类,我们就不分开写了,直接写在对应的接口里面吧,这里只是演示WCF的使用,数据库操作,直接使用linq to sql了,企业里大部分用EF code first,关于这个以后有时间再讲。为了不与数据库的表明冲突,后面加一个Dto了

    主要提前领略一下 操作契约,数据契约,枚举的使用

    我们先操作NewsInterface类库

    6.1新建一个枚举,NewsTypeEnum,演示枚举的基本写法

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Runtime.Serialization;
     
    namespace NewsInterface
    {
        [DataContract]
        public enum NewsTypeEnum
        {
            [EnumMember]
            Sports,
            [EnumMember]
            IT,
            [EnumMember]
            Country,
            [EnumMember]
            Funny
        }
    }

    NewsTypeEnum是一个公开的枚举类型,注意,enum前必须加上DataContract特性,枚举类型每个常量都必须使用EnumMember特性来表示

    6.2 添加一个NewsDto实体类, 演示实体的基本写法

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Runtime.Serialization;
    using System.ServiceModel;
     
    namespace NewsInterface
    {
        [DataContract]
        public class NewsDto
        {
            /// <summary>
            /// 新闻标题
            /// </summary>
            [DataMember]
            public string NewsTitle { get; set; }
     
            /// <summary>
            /// 新闻内容
            /// </summary>
            [DataMember]
            public string Content { get; set; }
     
            /// <summary>
            /// 新闻类型,这里顺便演示下枚举
            /// </summary>
            [DataMember]
            public NewsTypeEnum NewsType { get; set; }
     
            /// <summary>
            /// 发布时间
            /// </summary>
            [DataMember]
            public DateTime publishTime { get; set; }
     
            /// <summary>
            /// 最后更新时间
            /// </summary>
            [DataMember]
            public DateTime LastUpdateTime { get; set; }
     
            /// <summary>
            /// 作者
            /// </summary>
            [DataMember]
            public string Author { get; set; }
     
            /// <summary>
            /// 最后修改作者
            /// </summary>
            [DataMember]
            public string LastAuthor { get; set; }
     
            /// <summary>
            /// 阅读量
            /// </summary>
            [DataMember]
            public int ReadCount { get; set; }
        }
    }

    6.3 打开INewsInterface,添加公开方法,演示 普通的接口,这里在WCF中专业名叫数据契约

    我们只演示基本的简单的更删改查,还有个显示新闻关联图片5个方法,再次声明,我不讲新闻发布的新闻逻辑,这里只是演示WCF的用法

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Runtime.Serialization;
    using System.ServiceModel;
     
    namespace NewsInterface
    {
        [ServiceContract]
        public interface INewsInterface
        {
            /// <summary>
            /// 添加一条新闻
            /// </summary>
            /// <param name="dto">新闻实体</param>
            /// <returns>添加成功后返回添加成功后的ID</returns>
            [OperationContract]
            int NewsAdd(NewsDto dto);
     
            /// <summary>
            /// 删除一条新闻
            /// </summary>
            /// <param name="dto">新闻实体</param>
            /// <returns>是否删除成功</returns>
            [OperationContract]
            bool NewsDelete(NewsDto dto);
     
            /// <summary>
            /// 更新一条新闻
            /// </summary>
            /// <param name="dto">新闻实体</param>
            /// <returns>是否更新成功</returns>
            [OperationContract]
            bool NewsUpdate(NewsDto dto);
     
            /// <summary>
            /// 获得所有新闻
            /// </summary>
            /// <returns>返回新闻列表</returns>
            [OperationContract]
            List<NewsDto> NewsList();
     
            /// <summary>
            /// 根据ID获得新闻的图片
            /// </summary>
            /// <param name="Id">新闻编号</param>
            /// <returns>新闻相关联的图片</returns>
            [OperationContract]
            byte[] GetNewsImage(string Id);
     
        }
    }

    7.创建服务

    右击 Services解决方案文件夹,添加一个类库,名字叫NewsServices

    将Class1.cs文件名改下

    修改名字

    7.1 先编译解决方案,然后 添加引用

             imageimage

    7.2 实现接口

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Runtime.Serialization;
    using System.ServiceModel;
    using NewsInterface;
     
    namespace NewsServices
    {
        public class NewsImpl:INewsInterface
        {
     
            public int NewsAdd(NewsDto dto)
            {
                throw new NotImplementedException();
            }
     
            public bool NewsDelete(NewsDto dto)
            {
                throw new NotImplementedException();
            }
     
            public bool NewsUpdate(NewsDto dto)
            {
                throw new NotImplementedException();
            }
     
            public List<NewsDto> NewsList()
            {
                throw new NotImplementedException();
            }
     
            public byte[] GetNewsImage(string Id)
            {
                throw new NotImplementedException();
            }
        }
    }

    8. 添加宿主Hosts文件夹下,我们以控制台演示,宿主可以是很多种形式的,具体以后讲

    image

    同时修改属性 用.NET Framework4

    image

    然后添加引用

    imageimage

    打开Program.cs填入一下基本代码

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Runtime.Serialization;
    using System.ServiceModel;
    using NewsServices;
     
    namespace NewsHosts
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("服务宿主正在打开...");
                try
                {
                    //ToDo 等待开启服务代码
     
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);                
                }
                Console.WriteLine("打开成功!");
                Console.ReadKey();
            }
        }
    }

    使用ServiceHost对象,添加Program类中

    在try…catch代码块中添加如下代码:

    image

    添加宿主服务配置,我们利用WCF工具配置,也可以手动写

    默认生成的App.config右击它,没有使用WCF工具配置,我们打开

    vs2010菜单栏中的 WCF服务配置编辑器

    image

    使用之前,请确保你的解决方案重新生成了

    image

    下面我录个屏,讲一下配置

     这里注意记住终结点这个地址,通过这个地址,我们的其他客户端,就可以获得我们想要的数据,我们可以直接把数据发在这个地址上,返回JSON或者XML,或者是.NET对象,然后你继续处理,如果JSON或者XML,那么WEB就可以使用$.ajax等jQuery的方法获得数据了,用于显示在网页上了

    设置启动项,运行

    设置主项目

    先把服务能启动成功!

     代码下载: 点我下载

  • 相关阅读:
    F# 语法概览
    Excel 帮助无法正常工作的解决方法
    autofac 组件的实例范围
    visual studio code 中隐藏从 ts 文件生成的 js 文件和 map 文件
    git vim 编辑器基本操作
    nhibernate 中 lazy="no-proxy" 时的问题
    什么是数据科学
    Elasticsearch 疑难解惑
    Hadoop MapReduce执行过程实例分析
    深入浅出JVM
  • 原文地址:https://www.cnblogs.com/fcsh820/p/3162555.html
Copyright © 2020-2023  润新知