【WCF】创建第一个WCF应用程序

一、什么是WCF:

Windows Communication Foundation(WCF)是由微软发展的一组数据通信的应用程序开发接口,也可以说是一套软件开发包。WCF合并了Web服务、.net Remoting、消息队列和Enterprise Services的功能并集成在Visual
Studio中。WCF专门用于面向服务开发。

 WCF的最终目标是通过进程或不同的系统、通过本地网络或是通过Internet收发客户和服务之间的消息。并为服务提供直接的支持。托管、安全、事务管理、离线对立等等。

椭圆里面是若干服务器,会通过internet传输,服务都会提供一个对外的接口。

二、WebService和WCf的区别

(1)Web Service:严格来说是行业标准,也就是Web Service 规范,也称作WS-*规范,既不是框架,也不是技术。

(2)WCF:WCF 是一个分布式应用的开发框架,属于特定的技术,或者平台。既不是标准也不是规范。

(3)WCF 是一套框架,用来创建各种服务。其中包括创建 Web服务(采用 basicHttpBinding绑定的服务就是一个Web 服务)。

三、新建一个项目

有两个默认的,一个是接口,一个是类文件,在接口上面有【ServiceContract】,在上面定义非常的灵活。便于以后的重构。【PerationContract】操作契约。【DataContact】数据契约。下面的类继承接口。直接将两个文件删除即可。

(1)首先创建一个类库

(2)添加引用,找到ServiceModel,点击添加

(3)将自动生成的类文件删除,然后添加一个接口,命名为IHelloService代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel;

namespace HelloService
{
    /// <summary>
    ///  //定义服务契约
    /// </summary>
    [ServiceContract]
    public interface IHelloService
    {
        //添加操作契约,如果不添加的话访问这个服务访问不到sayHello方法
        /// <summary>
        /// 服务操作
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        [OperationContract]
        string SayHello(string name);

    }
}

(4)添加实现接口的类HelloService,定义SayHello方法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel;

namespace HelloService
{
    public class HelloService:IHelloService

    {
        /// <summary>
        /// 打招呼
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public string SayHello(string name)
        {
            return name + ":你好!";
        }
    }
}

(5)创建宿主程序。地址+绑定+契约=终结点。选择iis作为宿主,必须是http,作为通信协议绑定,如果使用自定义的必须根据绑定协议。 创建一个控制台应用程序,命名为HelloServiceHost

(6)添加ServiceModel引用,和HelloService引用。并在Program类文件中写入

(7)编写Program类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel;
using System.ServiceModel.Channels;

namespace HelloServiceHost
{
    class Program
    {
        static void Main(string[] args)
        {
            using (MyHelloHost Host=new MyHelloHost())
            {
                Host.open();
                Console.Read();
            }
        }
    }

    public class MyHelloHost : IDisposable
    {
        /// <summary>
        /// 定义一个服务对象
        /// </summary>
        private ServiceHost _myhost;

        public ServiceHost Myhost
        {
            get { return _myhost; }

        }

        /// <summary>
        /// 定义一个基地址
        /// </summary>
        public const string BaseAddress = "net.pipe://localhost";

        /// <summary>
        /// 可选地址
        /// </summary>
        public const string HelloServiceAddress = "Hello";

        //3、服务契约实现类型
        public static readonly Type ServiceType = typeof(HelloService.HelloService);//必须引用HelloService
        //服务契约接口
        public static readonly Type ContractType = typeof(HelloService.IHelloService);

        /// <summary>
        ///4、定义一个服务绑定
        /// </summary>
        public static readonly Binding hellobinding = new NetNamedPipeBinding();

        /// <summary>
        /// 5、构造服务对象
        /// </summary>
        protected void CreateHelloServiceHost()
        {
            //创建服务对象
            _myhost = new ServiceHost(ServiceType, new Uri[] { new Uri(BaseAddress) });

            //6、添加终结点
            _myhost.AddServiceEndpoint(ContractType,hellobinding,HelloServiceAddress);
        }

        /// <summary>
        /// 7、打开服务方法
        /// </summary>
        public void open()
        {
            Console.WriteLine("开始启动服务···");
            _myhost.Open();
            Console.WriteLine("服务已经启动···");
        }

        /// <summary>
        /// 8、创建构造方法
        /// </summary>
        public MyHelloHost()
        {
            CreateHelloServiceHost();
        }

        /// <summary>
        /// 关闭服务后,销毁服务宿主实例对象
        /// </summary>
        public void Dispose()
        {
            if (_myhost!=null)
            {
                (_myhost as IDisposable).Dispose();
            }
        }
    }

}

(8)运行结果

创建客户端:

(1)继续创建控制台应用程序命名为HelloService(记得添加引用),编写代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel;
using HelloService;
using System.ServiceModel.Channels;

namespace HelloClient
{
    class Program
    {
        static void Main(string[] args)
        {
            using (HelloProxy proxy = new HelloProxy())
            {
                //利用代理调用服务
                Console.WriteLine(proxy.Say("帅哥"));
                Console.Read();
            }
        }

        //硬编码定义服务契约
        [ServiceContract]
        interface IService
        {
            //服务操作
            [OperationContract]
            String Say(String name);

        }

        /// <summary>
        /// 客户端代理类;ClientBase 是用于创建可调用服务的客户端对象,是一种泛型,里面要包括服务端的接口;IService 接口要实现客户端创建的硬编码定义服务企业的接口。
        /// </summary>
        class HelloProxy : ClientBase<IHelloService>, IService
        {
            /// <summary>
            /// 硬编码定义绑定
            /// </summary>
            public static readonly Binding HelloBinding = new NetNamedPipeBinding();
            //硬编码定义地址
            public static readonly EndpointAddress HelloAddress = new EndpointAddress(new Uri("net.pipe://localhost/Hello"));

            /// <summary>
            /// 构造方法
            /// </summary>
            public HelloProxy() : base(HelloBinding, HelloAddress) { }

            //调用服务端方法
            public String Say(String name)
            {
                //使用Channel属性对服务进行调用,获取
                return Channel.SayHello(name);

            }
        }
    }
}

(2)打开HelloServiceHost的exe文件。在bin文件中。然后运行HelloClien中exe文件,结果:

文件格式:

小结:

刚开始接触WCF,对里面的东西理解的还不是很深刻,但是通过这个例子让我对WCF有了宏观的把控。

时间: 2024-08-11 01:35:59

【WCF】创建第一个WCF应用程序的相关文章

跟我一起学WCF(4)——第一个WCF程序

一.引言 前面几篇文章分享了.NET 平台下其他几种分布式技术,然而前面几种分布式技术专注于某一特定的领域,并且具有不同编程接口,这使得开发人员需要掌握多个API的使用.基于这样的原因,微软在.NET 3.0时实现了WCF.WCF是.NET平台下各种分布式技术的集成,它将前面介绍的几种分布式技术完全整合在一起,并提供了一套统一的编程接口(API).对于,开发人员来来说只需要掌握WCF一套的API,就可以实现之前分布式技术所实现的所有功能. 二.WCF详细介绍 WCF(Windows Commun

ubuntu下创建第一个rails应用程序

一.创建一个新的应用程序 在控制台输入 > rails new  demo create create README.rdoc create Rakefile create config.ru create .gitignore create Gemfile create app ........... Your bundle is complete! Use `bundle show [gemname]` to see where a bundled gem is installed. run

Xamarin.Android使用教程之创建第一个Android应用程序

<Xamarin Platform 试用版下载地址> 在本文中,我们将使用Xamarin创建第一个Android应用程序. 安装完Xamarin之后,在Visual Studio中点击File-> New Project,你应该可以在可用模板中看见一个Android选项. 继续并选择"Blank App (Android)",然后单击确定. Xamarin继续运行,然后创建你的第一个"Hello World"应用程序. 如果你只运行该应用程序,请确

创建第一个MVC应用程序

整个国庆期假,Insus.NET没有出门,在家静心修炼MVC.这意味着Insus.NET将来的日子里会以MVC为学习,开发,应用作为重点,不过现在才开始踏出第一步...... 路慢慢...... 下载了并安装Visual Studio Ultimate 2013 RC 和 MS SQL Server 2014.使用最新版本,可以了解和学习到最新技术. 去微软官方网站,学习MVC相关的教程:http://www.asp.net/mvc 去听微软官方网站推荐的MVC视频,虽然是英语授课,看操作即可.

WCF入门教程2——创建第一个WCF程序

本节目标 定义服务契约 创建宿主程序 创建客户端程序访问服务 定义服务契约 ServiceContract特性:该特性可被用来作用于子类或者借口之上,并允许重复声明. OperationContract:只有定义了该特性的方法才会被放入服务之中. 1.新建服务程序 新建项目--类库,这里我们先不直接新建一个WCF服务,而是新建一个类库,命名为HelloService 添加引用 删除Class1.cs,然后新建一个接口IHelloService.cs: using System; using Sy

IMX6开发板创建第一个Android应用程序helloworld

运行行 AndroidStudio 程序.如下图,选择创建一个新的 androidstudio 工程(基于 迅为-i.mx6开发板)应用名称改为“helloworld”,项目保存路径修改为自己的保存路径.连续点击下一步,直到如下界面,选择“Basic Activity”模板.继续点击下一步直到出现如下界面.点击“Finish”按钮,完成创建工程. 部分视频观看地址( 更多视频教程可在B站上搜索‘迅为电子’ ) iTOP-4412精英版开发板硬件连接 https://www.bilibili.co

在eclipse中创建第一个java应用程序,并在控制台输出“hello world”。

package com.fs.test; public class HelloWorld { public void aMethod() { } public static void main(String[] args) { System.out.print("Hello world"); } } 原文地址:https://www.cnblogs.com/ooo888ooo/p/11042700.html

第一个 WCF项目 与 Ajax

WCF(Windows Communication Foundation)是作为.Net framework 3.0发布的,所以只有2008及其以上的版本才可以创建wcf应用程序 WCF是对现有分布式通信技术的整合,其中包括Com/DCom..Net Remoting.Web服务及其WSE(web服务的升级版本).MSMQ. 现在决定 学习WCF ,所以在博客园里参考 http://www.cnblogs.com/jiagoushi/archive/2013/03/15/2962351.html

使用Visual Studio 2008创建你的第一个Windows Mobile程序介绍

使用Visual Studio 2008创建你的第一个Windows Mobile程序介绍 Windows MobileMobileWindowsMicrosoftWinForm 介绍 Microsoft Visual Studio 2008 专业版或者更高版本提供了一个Windows Mobile程序开发环境,允许你使用本地代码(C / C++)或托管代码(C# / Visual Basic.NET)为Windows Mobile设备创建程序. 这篇文章将带你正确的安装Visual Studi