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. CAD得到所有实体方法(网页版)

    主要用到函数说明: IMxDrawSelectionSet::AllSelect 得到当前空间的所有实体.详细说明如下: 参数 说明 [in,defaultvalue(NULL)] IMxDrawRe ...

  2. mongodb--命令练习

    windows10安装下载mongodb # 官网或镜像地址下载mongodb exe 二进制安装包 # 安装时选用custorm 去除勾选compass可视化工具 # 安装完成创建data文件,修改 ...

  3. 洛谷——P4071 [SDOI2016]排列计数(错排+组合数学)

    P4071 [SDOI2016]排列计数 求有多少种长度为 n 的序列 A,满足以下条件: 1 ~ n 这 n 个数在序列中各出现了一次 若第 i 个数 A[i] 的值为 i,则称 i 是稳定的.序列 ...

  4. poj2352 Stars【树状数组】

    Astronomers often examine star maps where stars are represented by points on a plane and each star h ...

  5. Oracle的shutdown命令

    oracle的shutdown命令用来关闭当前实例,有4个可选参数:normal.transactional.immediate和abort.不带参数时默认是normal.这几个参数的差异体现在以下几 ...

  6. 51nod 1241 特殊的排序

    [题解] 设满足前后两个元素之差为1的最长上升子序列LIS的长度为m,那么本题的答案即为n-m. 证明: 1,n-m次移动一定可以让序列递增.设LIS的第一个数为i,最后一个数为j,我们按照i-1到1 ...

  7. 洛谷 3398 仓鼠找sugar 【模板】判断树上两链有交

    [题解] 题意就是判断树上两条链是否有交.口诀是“判有交,此链有彼祖”.即其中一条链的端点的Lca在另一条链上. 我们设两条链的端点的Lca中深度较大的为L2,对L2与另一条链的两个端点分别求Lca, ...

  8. [K/3Cloud] 表单python脚本使用QueryService的做法

    听说有些朋友想在表单里做自定义的界面数据处理,一般来说写个表单插件会很容易解决这类问题.但是鉴于C#插件开发的不便性和实施搭建开发环境的麻烦,在现场做C#开发可能会不太方便(没部署开发环境之类的问题) ...

  9. android保存bitmap到sdcard

    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { //判断sdcard是否存在和是否具有读写 ...

  10. M - 小希的迷宫 并查集

    上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardon来走.但是她设计迷宫的思路不一样,首先她认为所有的通道都应该是双向连通的,就是说如果有一个通道连通了 ...