WCF是Windows Communication Foundation的缩写,是微软发展的一组数据通信的应用程序开发接口,它是.NET框架的一部分,是WinFx的三个重要开发类库之一,其它两个是WPF和WF。在本系列文章

(我现在计划的应该是三篇,一篇WCF的开发和部署,另外是在.net平台上调用它,第二篇是PHP调用,第三篇是JAVA调用)。

在本次的跨平台集成通信开发示例中,使用到的各种技术,咱且走且看,一边开发一边讲解。

1.创建项目结构

使用VS2010一个名为IntergatedCommunication的空解决方案,在其下,新建Contracts、Implemention两个类库项目,分别为契约的设计与服务的实现,而后新建ConsoleHost、Client两个控制台应用程序,分别为在控制台中实现服务托管使用,一个作为.net平台上调用WCF的实例使用,如下图

2.契约的设计

本实例我还是想让它确实可以应用在实际项目中,所以我在设计的时候,将使用复杂类型(complex type),因为这并不同于普通类型,尤其在java和php在使用复杂类型参数是,调用方法是很不一样的。

首先对Contracts、Implemention和ConsoleHost项目中添加对System.ServiceModel和System.Runtime.Serialization的引用。这两个命名空间中包含ServiceContractAttribute等WCF需要的契约特性类,和对复杂类型序列化的类DataContractSerializer。

本示例使用员工信息(员工ID、员工姓名、所属部门)查询本员工上月的工资明细(员工ID、薪水、日期),所以首先建立两个类Employee类和SalaryDetail类,在类中引用System.Runtime.Serialization命名空间,并且,在类上添加DataContractAttribute并在每个类属性上添加DataMemberAttribute:

Employee.cs

using System.Runtime.Serialization;
 
namespace Contracts
{
    [DataContract]
    public class Employee
    {
        [DataMember]
        public string Id { get; set; }
        [DataMember]
        public string Name { get; set; }
        [DataMember]
        public string Department { get; set; }
    }
}
 
 
SalaryDetail.cs
using System.Runtime.Serialization;
 
namespace Contracts
{
    [DataContract]
    public class SalaryDetail
    {
        [DataMember]
        public string Id { get; set; }
        [DataMember]
        public decimal Salary { get; set; }
        [DataMember]
        public DateTime Date { get; set; }
    }
}
 
以上所设计的是数据契约,在使用DataContract和DataMember修饰和类和属性后,可将这些类型和属性暴露在元数据中,而后设计服务契约
     定义一个借口名为IEmployeeManagement并添加一个方法签名GetSalaryOfLastMonth,并添加ServiceContractAttribute和OperationContractAttribute。
    IEmployeeManagement.cs
using System.ServiceModel;
 
namespace Contracts
{
    [ServiceContract]
    public interface IEmployeeManagement
    {
        [OperationContract]
        SalaryDetail GetSalaryOfLastMonth(Employee emp);
    }
}

3.实现服务

在Implemention中添加对Contracts项目的引用,添加EmployeeManagement类,实现IEmployeeManagement接口

EmployeeManagement.cs

using Contracts;
 
namespace Implemention
{
    public class EmployeeManagement:IEmployeeManagement
    {
        public SalaryDetail GetSalaryOfLastMonth(Employee emp)
        {
            SalaryDetail salaryDetail = new SalaryDetail();
            salaryDetail.Id = emp.Id;
            salaryDetail.Date = DateTime.Now.AddMonths(-1);
            salaryDetail.Salary = 20000;
            return salaryDetail;
        }
    }
}
 
因为这里实现的内容并不重要,所以没有具体的去实现它,知识简单的返回了一个SalaryDetail的实例,Id为传入参数的员工ID,时间为当前时间的前一个月,薪水为固定的20000。
 

4.控制台托管服务

在ConsoleHost中添加对以上两个项目的引用,这时,生成整个解决方案,然后在ConsoleHost中添加应用程序配置文件App.config。并使用WCF服务配置编辑器打开它,并配置服务托管地址和绑定类型等信息,最终配置结果为

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <behaviors>
            <serviceBehaviors>
                <behavior name="ExposeMetaDataBehavior">
                    <serviceMetadata httpGetEnabled="true" httpGetUrl="EmployeeManagement/MEX" />
                </behavior>
            </serviceBehaviors>
        </behaviors>
        <services>
            <service behaviorConfiguration="ExposeMetaDataBehavior" name="Implemention.EmployeeManagement">
                <endpoint address="EmployeeManagement"
                    binding="wsHttpBinding" bindingConfiguration="" contract="Contracts.IEmployeeManagement" />
              <host>
                <baseAddresses>
                  <add baseAddress="http://localhost:9876/"/>
                </baseAddresses>
              </host>
            </service>
        </services>
    </system.serviceModel>
</configuration>

打开program.cs,在main方法中添加代码,托管服务

using System.ServiceModel;
using Implemention;
 
namespace ConsoleHost
{
    class Program
    {
        static void Main(string[] args)
        {
            ServiceHost host = new ServiceHost(typeof(Implemention.EmployeeManagement));
            try
            {
                Console.WriteLine("EmployeeManagement Service Starting");
                host.Open();
                Console.WriteLine("EmployeeManagement Service Started");
                Console.ReadKey();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                if (ex.InnerException != null)
                {
                    Console.WriteLine("\n" + ex.InnerException.Message);
                }
            }
            finally
            {
                host.Close();
            }
            Console.ReadKey();
        }
    }
}

生成解决方案,并在VS外以管理员权限启动ConsoleHost.exe文件,这样就在控制台中托管了服务

5.在.net平台中调用WCF

在Client中,添加服务引用,命名空间设置为ServiceReference

在program.cs中添加代码,调用控制台中托管的服务

namespace Client
{
    class Program
    {
        static void Main(string[] args)
        {
            ServiceReference.EmployeeManagementClient client = new ServiceReference.EmployeeManagementClient();
            ServiceReference.Employee emp = new ServiceReference.Employee() { Id = "dev001", Name = "James White", Department = "Development" };
            ServiceReference.SalaryDetail salaryDetail = client.GetSalaryOfLastMonth(emp);
            Console.WriteLine("Employee number {0}'s salary of {1} month is ¥{2}", salaryDetail.Id, salaryDetail.Date.Month, salaryDetail.Salary);
            Console.ReadKey();
        }
    }
}

在这里,我们已经简单的实现了WCF服务的实现和.net本平台调用WCF,这一篇不是最重要的,下一篇是使用IIS托管WCF并使用PHP调用WCF。

使用WCF进行跨平台开发之一(WCF的实现、控制台托管与.net平台的调用)的更多相关文章

  1. 浅谈移动应用的跨平台开发工具(Xamarin和React Native)

    谈移动应用的跨平台开发不能不提HTML5,PhoneGap和Sencha等平台一直致力于使用HTML5技术来开发跨平台的移动应用,现在看来这个方向基本算是失败的,基于HTML5的移动应用在用户体验上与 ...

  2. WCF服务端开发和客户端引用小结

    1.服务端开发 1.1 WCF服务创建方式 创建一个WCF服务,总是会创建一个服务接口和一个服务接口实现.通常根据服务宿主的不同,有两种创建方式. (1)创建WCF应用程序 通过创建WCF服务应用程序 ...

  3. 老徐FrankXuLei 受邀为花旗银行讲授《微软WCF服务分布式开发与SOA架构设计课程》

    老徐FrankXuLei 受邀为花旗银行上海研发中心讲授<微软WCF服务分布式开发与SOA架构设计课程> 受邀为花旗银行上海研发中心讲授<微软WCF服务分布式开发与SOA架构设计课程 ...

  4. 四、利用EnterpriseFrameWork快速开发基于WCF为中间件的三层结构系统

    回<[开源]EnterpriseFrameWork框架系列文章索引> EnterpriseFrameWork框架实例源代码下载: 实例下载 本章内容与上一张<利用Enterprise ...

  5. WCF学习系列二---【WCF Interview Questions – Part 2 翻译系列】

    http://www.topwcftutorials.net/2012/09/wcf-faqs-part2.html WCF Interview Questions – Part 2 This WCF ...

  6. WCF学习系列一【WCF Interview Questions-Part 1 翻译系列】

    http://www.topwcftutorials.net/2012/08/wcf-faqs-part1.html WCF Interview Questions – Part 1 This WCF ...

  7. WCF学习系列三--【WCF Interview Questions – Part 3 翻译系列】

    http://www.topwcftutorials.net/2012/10/wcf-faqs-part3.html WCF Interview Questions – Part 3 This WCF ...

  8. WCF学习系列四--【WCF Interview Questions – Part 4 翻译系列】

    WCF Interview Questions – Part 4   This WCF service tutorial is part-4 in series of WCF Interview Qu ...

  9. WCF学习笔记(1)——Hello WCF

    1.什么是WCF Windows Communication Foundation(WCF)是一个面向服务(SOA)的通讯框架,作为.NET Framework 3.0的重要组成部分于2006年正式发 ...

随机推荐

  1. js数组的处理

    //重写Array中的indexOf方法,获取数组中指定值的元素的索引 Array.prototype.indexOf = function (val) { for (var i = 0; i < ...

  2. 14mysql事务、数据库连接池、Tomcat

    14mysql事务.数据库连接池.Tomcat-2018/07/26 1.mysql事务 事务指逻辑上的一组操作,组成这组操作的各个单元,要么全部成功,要么全部不成功. MySQL手动控制事务 开启事 ...

  3. type、object、class之间的关系

    class Foo: pass print(type(int)) # <class 'type'> print(type(str)) # <class 'type'> prin ...

  4. OS X中crt中文乱码

    SecureCRT中显示乱码的话,可以去设置为UTF-8编码: Session Options->Terminal->Appearance->Character Encoding,设 ...

  5. orcad中注意的事情

    1.地的标识不能放到已经分配了网络的线上. 在用orcad画原理图的时候,把电源放到网络的时候需要特别的注意,如果,将电源地直接放到线上,而这根线又已经被分配了网络标号,那这个地会随已经分配了的网络号 ...

  6. IIS301重定向:将不带www的域名跳转到带www上

    首先你的域名有这两条解析记录 进入服务器IIS,添加2个站点,如下图 第一个正常绑定你的域名:www.baidu.com 第二个绑定不带www的域名:baidu.com 然后点开ncgd-no-www ...

  7. 学渣乱搞系列之Tarjan模板合集

    学渣乱搞系列之Tarjan模板合集 by 狂徒归来 一.求强连通子图 #include <iostream> #include <cstdio> #include <cs ...

  8. 所有在Linux系统下 arp -d $ip 命令只能清除一个IP地址的对应MAC地址缓存,可以使用组合命令操作。

    https://blog.csdn.net/u011641885/article/details/48175239 https://blog.csdn.net/zj0910/article/detai ...

  9. 线程间的通信----wait/notify机制

    wait/notify机制 实现多个线程之间的通信可以使用wait.notify.notifyAll三个方法.这三个方法都是Object类的方法.wait():导致当前线程等待,直到另一个线程调用此对 ...

  10. jd-eclipse插件的安装

    一,资源 jd-eclipse-site-1.0.0-RC2.zip    百度网盘链接:https://pan.baidu.com/s/1GTFFY_1jg4k9vjZNE4JliQ       提 ...