WCF原理性的东西,暂时还没有深入研究,只是在公司的项目中使用到了,会调用,然后再多做了一些了解,现在将它抽出来了一个小实例,写了一个WCF的demo。

  我写的这个WCF.Demo主要包括数据契约和服务契约,UI客户端层和Host宿主层,基于http和net.tcp两种协议通信。

  不多说,直接贴一张层次图片先,最后提供源码下载。

  

  数据契约层(DataContracts)代码:

  

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;namespace WCF.DataContracts
{[DataContract()]public class CustomerData{[DataMember()]public string Name { set; get; }[DataMember()]public string Sex { set; get; }}
}

服务契约接口层(WCF.ServiceContracts)

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;namespace WCF.ServiceContracts
{[ServiceContract()]public interface ICustomerService{[OperationContract()]string ShowInfo();}
}

服务契约实现层(WCF.Services)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WCF.DataContracts;
using WCF.ServiceContracts;namespace WCF.Services
{public class CustomerService : ICustomerService{public string ShowInfo(){CustomerData data = new CustomerData(){Name = "admin",Sex = "男"};return "姓名是:" + data.Name + ",性别是:" + data.Sex;}}
}

宿主层(两个文件)

Program

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;
using WCF.Services;namespace WCF.Hosting
{class Program{static void Main(string[] args){ServiceHost hostForHello = new ServiceHost(typeof(CustomerService));hostForHello.Open();Console.WriteLine("WCF服务启动成功!");Console.ReadLine();}}
}

app.config

<?xml version="1.0"?><configuration><system.serviceModel><services><service name="WCF.Services.CustomerService" behaviorConfiguration="mex"><host><baseAddresses><add baseAddress="http://192.168.1.100:64566/CustomerService"/><add baseAddress="net.tcp://192.168.1.100:64567/CustomerService"/></baseAddresses></host><endpoint binding="wsDualHttpBinding" contract="WCF.ServiceContracts.ICustomerService" address="mex" /> <endpoint address="net.tcp://192.168.1.100:64567/CustomerService" binding="netTcpBinding"bindingConfiguration="tcpWindowsSecurity" name="helloEndPoint"contract="WCF.ServiceContracts.ICustomerService"/><endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" /></service></services><bindings><netTcpBinding><binding name="tcpWindowsSecurity"></binding></netTcpBinding></bindings><behaviors><serviceBehaviors><behavior name="mex"><serviceMetadata  /></behavior></serviceBehaviors></behaviors></system.serviceModel><startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>

客户端调用层(WCF.Client)

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WCF.ServiceContracts;namespace WCF.Client
{public partial class WebForm1 : System.Web.UI.Page{protected void Page_Load(object sender, EventArgs e){using (ChannelFactory<ICustomerService> channelFactory = new ChannelFactory<ICustomerService>("helloEndPoint")){ICustomerService helloService = channelFactory.CreateChannel();using (helloService as IDisposable){TextBox1.Text = helloService.ShowInfo();}}}}
}

客户端配置文件

<?xml version="1.0" encoding="utf-8"?><!--有关如何配置 ASP.NET 应用程序的详细信息,请访问http://go.microsoft.com/fwlink/?LinkId=169433--><configuration><system.serviceModel><bindings><netTcpBinding><binding name="tcpWindowsSecurity"></binding></netTcpBinding></bindings><client><endpoint name="helloEndPoint" address="net.tcp://192.168.1.100:64567/CustomerService"binding="netTcpBinding" bindingConfiguration="tcpWindowsSecurity"contract="WCF.ServiceContracts.ICustomerService" /></client></system.serviceModel><system.web><compilation debug="true" targetFramework="4.5" /><httpRuntime targetFramework="4.5" /></system.web></configuration>

源码下载

转载于:https://www.cnblogs.com/renzaijianghu/p/3493264.html

UI层调用WCF服务实例(源码)相关推荐

  1. java 摄像头 win7_JAVA摄像头调用方法_实例源码介绍—Win7

    JAVA摄像头调用_实例源码介绍-Win7. 本人菜鸟级人物,由于要搞个人脸识别登录(JAVACV),百度谷歌了一下午,找了网上简单的样例代码来试,可是老是调试不过,出现CaptureDeviceMa ...

  2. Silverlight实用窍门系列:22.Silverlight使用WebService调用C++,Delphi编写的DLL文件【实例源码下载】...

    在Silverlight程序(非Out of Browser模式)中是无法直接调用DLL的,但是很多的计算或者其他应用程序的调用中我们需要用到DLL的加载.比如调用DLL来识别身份证读卡器传输过来 ...

  3. axis2 webservice入门学识(JS,Java,PHP调用实例源码)

    来源:http://www.myexception.cn/web/952419.html axis2 webservice入门知识(JS,Java,PHP调用实例源码) 背景简介 最近接触到一个银行接 ...

  4. Android+上百实例源码分析以及开源分析+集合打包

    1.360新版特性界面源代码 实现了360新版特性界面的效果,主要涉及到Qt的一些事件处理与自定义控件.但源码好像是c++. 2.aidl跨进程调用 服务端onBind暴露,然后客户端bindServ ...

  5. Android上百实例源码分析以及开源分析集合打包

    感谢网友banketree的收集,压缩包的内容如下: 1.360新版特性界面源代码 实现了360新版特性界面的效果,主要涉及到Qt的一些事件处理与自定义控件.但源码好像是c++. 2.aidl跨进程调 ...

  6. sofa协议服务器,sofa-rpc 服务端源码流程走读

    sofa-rpc是阿里开源的一款高性能的rpc框架,这篇文章主要是对sofa-rpc provider启动服务流程的一个代码走读,下面是我简单绘制的一个基本的关系流程图 下面我们根据sofa-rpc代 ...

  7. java sofa rpc_sofa-rpc服务端源码的详细分析(附流程图)

    本篇文章给大家带来的内容是关于sofa-rpc服务端源码的详细分析(附流程图),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助. sofa-rpc是阿里开源的一款高性能的rpc框架,这篇 ...

  8. php jquery ajax登录,jQuery+Ajax+PHP弹出层异步登录效果(附源码下载)

    弹出层主要用于展示丰富的页面信息,还有一个更好的应用是弹出表单层丰富交互应用.常见的应用有弹出登录表单层,用户提交登录信息,后台验证登录成功后,弹出层消失,主页面局部刷新用户信息.本文我们将给大家介绍 ...

  9. Eureka服务注册源码分析

    本文来说下Eureka服务注册源码 文章目录 Eureka-Client注册服务 啥时候会注册 定时器注册 自动注册 DiscoveryClient.register() Eureka-Server接 ...

最新文章

  1. list redis 怎样做排行_学 Redis 的 7000 字小结!!!
  2. android平板值得买吗,2021年一月更新1000-2000价位最全平板选购指南
  3. Raect Router 4 的使用 (1)
  4. Python_mlab_3D作图/gif
  5. 自动网络搜索(NAS)的理解
  6. 用CentOS 6快速配置一台企业级Web代理服务器
  7. Ubuntu下备份系统的方法
  8. 《离散数学》题库大全及答案
  9. 展视互动签约新华网 创在线教育新蓝图
  10. java使用itext合并pdf文件
  11. PCM音量控制(高级篇)
  12. 通过2048学习自定义view(二) 滑动事件监听 与 事件回调
  13. Webkit内核探究【1】——Webkit简介
  14. 攻击篇--远程控制(一)生成被控端与主控端
  15. Cocos2d-x 面试题解 整理01
  16. 【Python网络蜘蛛 · 3】:post请求、模拟金山翻译(附源代码)
  17. 【大学物理学】狭义相对论
  18. 全球茶叶产量、消费量及进出口贸易分析:中国茶叶产量全球排名第一[图]
  19. MPI以太网通讯处理器在铝型材时效炉中的实际应用案例
  20. 计算机研究生就业方向之大厂销售(营销)

热门文章

  1. php 云技术,什么叫云技术?
  2. matlab3dimshow_matlab利用MATLAB嵌套函数表示3D作图
  3. mysql按升序创建索引_Mysql中的降序索引底层实现
  4. 积性函数与Dirichlet卷积 学习小记
  5. JZOJ 4919. 【NOIP2017提高组模拟12.10】神炎皇
  6. java 统计文本行数_统计文本文件的行数,单词书,字节数
  7. jenkins 下载插件 一直失败_Jenkins. 安装错误: No such plugin: cloudbees-folder
  8. 【学习笔记】求解简单递归式的一般方法
  9. php 调用变量方法名,php中引用(变量和函数名前加符号)用法
  10. Thinkphp5.0快速入门笔记(2)