WCF编程系列(一)初识WCF

Windows Communication Foundation(WCF)是微软为构建面向服务的应用程序所提供的统一编程模型。
WCF的基本概念:
  地址:定义服务的地址
  绑定:定义服务的通讯方式(传输协议、编码方案)
  契约:定义服务的具体实现
  终结点:由地址、绑定和契约共同构成一个终结点,服务器通过终结点向客户端公开服务,客户端通过终结点调用服务。

下面通过一个简单的服务示例来认识WCF(只需让本例顺利运行即可,关于代码中的各种类型及WCF的相关概念我们将在后续介绍):
1.新建项目,名称 XfrogWCFService,解决方案名称 XfrogWCFStudy001,模板选择类库,选择.NET Framework 3.0版本
2.修改Class1.cs文件名称为 IFirstService.cs
3.添加引用 System.ServiceModel
4.修改IFirstService.cs代码如下:

using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
namespace Xfrog.Study.WCF
{
    [ServiceContract]
    public interface IFirstService
    {
        [OperationContract]
        String GetData(String name);
    }
}

我们定义了一个IFirstService接口,注意在接口上申明了ServiceContract特性,即服务契约,表明该接口是一个服务。方法上声明了OperationContract特性,表示该方法是IFirstService的一个服务方法,客户端可远程调用该方法。

5.添加一个新类,文件名为FirstService.cs,代码如下:

using System;
using System.Collections.Generic;
using System.Text;
namespace Xfrog.Study.WCF
{
    public class FirstService : IFirstService
    {
        string IFirstService.GetData(String name)
        {
            return String.Format("Hello {0},Welcome To WCF!", name);
        }
    }
}
OK,到此我们的服务代码已经编写完成,下面我们必须为服务提供一个运行的宿主,通过该宿主程序来启动我们的服务。
6.在同一解决方案下新建一个项目,名称为Host,类型为控制台应用程序
7.Host项目中添加引用,引用项目XfrogWCFService,然后再添加引用:System.ServiceModel
8.修改Program.cs代码如下:

using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
using Xfrog.Study.WCF;
namespace Host
{
    class Program
    {
        static void Main(string[] args)
        {
            using (ServiceHost host = new ServiceHost(typeof(FirstService)))
            {
                host.Open();
                Console.WriteLine("服务已启动,按任意键中止...");
                Console.ReadKey(true);
                host.Close();
            }
        }
    }
}
以上,我们已经实现了服务以及为服务提供了一个运行宿主,即契约部分已经完成,下面我们为服务指定地址及绑定,本步骤可通过WCF的管理工具,或直接编写配置文件来完成。我们先采用手工编写配置文件的方式:
9.新建项,选择应用程序配置文件,文件名App.config保持不变。
10.修改app.config内容如下:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <services>
      <service name="Xfrog.Study.WCF.FirstService" behaviorConfiguration="behaviorConfiguration">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8000/"/>
          </baseAddresses>
        </host>
        <endpoint address="" binding="basicHttpBinding" contract="Xfrog.Study.WCF.IFirstService"></endpoint>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="behaviorConfiguration">
          <serviceMetadata httpGetEnabled="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>


11.设置Host项目为启动项目,启动调试。控制台上显示服务已启动后,打开浏览器输入服务地址: http://localhost:8000/  ,浏览器中会打开我们的服务页面,这表示我们的服务已经启动成功,客户端可通过该地址访问我们的服务了。
下面,我们将创建一个客户端来访问我们的服务
12.在同一解决方案下新建一个项目,名称为Client,类型为控制台应用程序
13.我们将使用微软的svcutil工具生成FirstService服务的客户端代理类,通过开始菜单/Microsoft Visual Studio 2008/Visual Studio Tools/Visual Studio 2008命令提示,启动命令环境。
14.确认FirstService服务已启动
15.切换当前路径到解决方案目录:
cd G:\Study\WCF\XfrogWCFStudy001
g:
16.输入命令:
svcutil  http://localhost:8000/?wsdl  /o:FirstServiceClient.cs
执行成功后,会在解决方案目录下生成两个文件:FirstServiceClient.cs 和output.config
17.中止Host项目的调试,回到Client项目,选择添加 现有项 ,然后选择这两个文件,添加后,将output.config重命名为App.config
18.Client项目中添加引用,选择System.ServiceModel
19.修改program.cs代码如下:

using System;
using System.Collections.Generic;
using System.Text;
namespace Client
{
    class Program
    {
        static void Main(string[] args)
        {
            String key = "";
            while (String.Compare(key, "Q", true)!=0)
            {
                FirstServiceClient client = new FirstServiceClient();
                Console.WriteLine(client.GetData(key));
                key = Console.ReadLine();
            }
        }
    }
}
20.Host项目上单击右键,选择调试—>启动新实例,待服务启动完成后,在Client项目上单击右键,选择调试—>启动新实例。输入任意字符回车,Client将调用FirstService服务GetData方法,返回一个字符串。输入q退出Client。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
☆ 资源说明:☆ ============================================================ [Addison-Wesley Professional] WCF 3.5 本质论 (英文版) [Addison-Wesley Professional] Essential Windows Communication Foundation For .NET Framework 3.5 ============================================================ [Apress] WCF 高级程序设计 (英文版) [Apress] Pro WCF Practical Microsoft SOA Implementation ============================================================ [Apress] WCF 4 高级程序设计 第2版 (英文版) [Apress] Pro WCF 4 Practical Microsoft SOA Implementation 2nd Edition ============================================================ [Microsoft Press] WCF 技术揭秘 (微软出品) (英文版) [Microsoft Press] Inside Windows Communication Foundation ============================================================ [Microsoft Press] WCF 4 从入门到精通 (微软出品) (英文版) [Microsoft Press] Windows Communication Foundation 4 Step by Step ============================================================ [奥莱理] WCF 新手实战入门手册 (英文版) [奥莱理] Learning WCF A Hands-on Guide ============================================================ [奥莱理] WCF 服务编程 第1版 (英文版) [奥莱理] Programming WCF Services 1st Edition ============================================================ [奥莱理] WCF 服务编程 第2版 (英文版) [奥莱理] Programming WCF Services 2nd Edition ============================================================ [奥莱理] WCF 服务编程 第3版 (英文版) [奥莱理] Programming WCF Services Mastering WCF and the Azure AppFabric Service Bus 3rd Edition ============================================================ [奥莱理] 使用 .Net 3.5 技术创建 RESTful Web 服务 (英文版) [奥莱理] RESTful .NET Build and Consume RESTful Web Services with .NET 3.5 ============================================================ [Packt Publishing] WCF 4.0 开发 SOA 程序经典实例 (英文版) [Packt Publishing] Microsoft Windows Communication Foundation 4.0 Cookbook for Developing SOA Applications ============================================================ [Packt Publishing] WCF 多层服务开发 —— 使用 LINQ 技术 (英文版) [Packt Publishing] WCF Multi-tier Services Development with LINQ =======================
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值