WCF分分钟入门
近来学习wcf,总结了一下入门的经验,小白的入门篇,也方便以后复习,省的去查质料。
第一步:创建wcf程序,程序初始化有一个接口和一个实现类写个简单的返回方法就可以了;
第二步:创建一个宿主,也就是服务,写好打开服务的代码和配置文件;
第三步:创建一个客户端服务,运行宿主,打开服务后在客户端添加服务引用;
下面的代码是建立在配置文件的基础上,下面也给出了配置的内容。
具体流程如下:
WCF程序代码
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.Text; namespace CommunicationsService { // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的类名“Service1”。 public class Service1 : IService1 { public string GetData(string value) { return string.Format("You entered: {0}", value); } } }
using CommunicationsService; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.ServiceModel; using System.ServiceModel.Description; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace ServerUI { public partial class forServer : Form { public forServer() { InitializeComponent(); } ServiceHost host = null; private void forServer_Load(object sender, EventArgs e) { host = new ServiceHost(typeof(Service1)); host.Opened += delegate//打开服务时触发事件 { rtbMessage.Text = "Service已经启动服务!"; }; host.Open();//打开服务 } private void forServer_FormClosing(object sender, FormClosingEventArgs e) { host.Close(); } } }
<system.serviceModel> <services> <service name="CommunicationsService.Service1"> <endpoint address="http://172.16.140.207:8080/Service1" binding="wsHttpBinding" bindingConfiguration="" contract="CommunicationsService.IService1"> <headers> <sn xmlns="http://www.artech.com/"> {DDA095DA-93CA-49EF-BE01-EF01-EF5B471779FD0} </sn> </headers> </endpoint> <host> <baseAddresses> <add baseAddress="http://172.16.140.207:8080/" /> </baseAddresses> </host> </service> </services> <behaviors> <serviceBehaviors> <behavior name=""> <!--服务请求地址配置--> <serviceMetadata httpGetEnabled="true" httpGetUrl="http://172.16.140.207:8080/IService1/metadata"/> <serviceDebug includeExceptionDetailInFaults="false" /> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel>
客户端代码
private void forClient_Load(object sender, EventArgs e) { ChannelFactory<IService1> channelFactory = new ChannelFactory<IService1>("ClientPoints"); IService1 proxy = channelFactory.CreateChannel(); rtbMessage.Text = proxy.GetData("hello"); }
<system.serviceModel> <bindings> <wsHttpBinding> <binding name="WSHttpBinding_IService1" /> </wsHttpBinding> </bindings> <client> <endpoint address="http://172.16.140.207:8080/Service1" binding="wsHttpBinding" bindingConfiguration="" contract="CommunicationsService.IService1" name="ClientPoints" kind="" endpointConfiguration=""> <identity> <dns value="localhost" /> <certificateReference storeName="My" storeLocation="LocalMachine" x509FindType="FindBySubjectDistinguishedName" /> </identity> </endpoint> <endpoint address="http://172.16.140.207:8080/Service1" binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IService1" contract="ServiceReference1.IService1" name="WSHttpBinding_IService1"> <identity> <userPrincipalName value="MyPC\LiuZhen" /> </identity> </endpoint> </client> </system.serviceModel>
希望每天的自己都比昨天的自己强。
WCF分分钟入门的更多相关文章
- C#面向服务编程技术WCF从入门到实战演练
一.WCF课程介绍 1.1.Web Service会被WCF取代吗? 对于这个问题阿笨的回答是:两者在功能特性上却是有新旧之分,但是对于特定的系统,适合自己的就是最好的.不能哪一个技术框架和行业标准作 ...
- WCF 程序入门
WCF是微软公司推出的符合SOA思想的分布式应用程序技术框架和编程模型,是建立在消息通信这一概念基础上运行的一个运行时服务系统. WCF编程模型的目标是实现以下两个实体之间的通信:WCF服务端和WCF ...
- WCF 快速入门
定义服务契约 构建HelloWCF应用的第一步是创建服务契约.契约式是表示消息应用外形的主要方式.对于外形,是指服务暴露的操作,使用的消息 schema和每个操作实现的消息交换模式(MEP).总之,契 ...
- C# WCF服务入门
之前在公司用的服务端是wcf写的,但是没有深入研究,最近找工作,面试的时候好多人看到这个总提问,这里做个复习 就用微软官方上的例子,搭一个简单的wcf服务,分6步 1 定义服务协定也就是契约,其实就是 ...
- WCF的入门教程dome(一)
一.概述 Windows Communication Foundation(WCF)是由微软发展的一组数据通信的应用程序开发接口,可以翻译为Windows通讯接口,它是.NET框架的一部分.由 .NE ...
- Python 基础:分分钟入门
Python和Pythonic Python是一门计算机语言(这不是废话么),简单易学,上手容易,深入有一定困难.为了逼格,还是给你们堆一些名词吧:动态语言.解释型.网络爬虫.数据处理.机器学习.We ...
- WCF宿主实践入门
本篇属于WCF实践入门,由于博主本人水平有限,没有理论上的介绍,仅仅从其几种不同的宿主方式分别介绍WCF的使用. WCF有多种宿主方式:1.自托管宿主,2.windows service宿主,3.II ...
- WCF学习笔记1--发布使用配置文件的服务
关于WCF的入门网上资料很多,可以参考蒋金楠老师的博客http://www.cnblogs.com/artech/archive/2007/02/26/656901.html,我是从这篇博客开始学习的 ...
- [老老实实学WCF] 第一篇 Hello WCF
老老实实学WCF 第一篇 Hello WCF WCF(Windows Communication Foundation)是微软公司推出的面向服务技术的集大成者,涵盖继承了其之前发布的所有的分布式应用 ...
随机推荐
- LINQ的Expression与delegate表达式
Linq的delegate表达式,Insus.NET觉得它封装得好,让开发时简化了很多代码,而且容易阅读与检索. 比如,我们需要计算优惠给客户金额,打85%折,可以这样写: using System; ...
- Java动态程序设计:反射介绍
使用运行的类的信息使你的程序设计更加灵活 反射授予了你的代码访问装载进JVM内的Java类的内部信息的权限,并且允许你编写在程序执行期间与所选择的类的一同工作的代码,而不是在源代码中.这种机制使得反射 ...
- TCP 与 UDP
TCP Transmission Control Protocol,传输控制协议,传输层通信协议. 采用“带重传的肯定确认”(Positive Acknowledge with Retransmiss ...
- 【译】About the Java Technology
About the Java Technology Java technology is both a programming language and a platform. The Java Pr ...
- MyEclipse 不能将WAR包导出的解决方法
不能导出WAR包的原因是破解没有完全导致的. 解决办法: 找到MyEclipse安装目录下MyEclipse\Common\plugins文件夹中的com.genuitec.eclipse.expor ...
- Scalaz(35)- Free :运算-Trampoline,say NO to StackOverflowError
在前面几次讨论中我们介绍了Free是个产生Monad的最基本结构.它的原理是把一段程序(AST)一连串的运算指令(ADT)转化成数据结构存放在内存里,这个过程是个独立的功能描述过程.然后另一个独立运算 ...
- phpcms 移植【添加相关文章】功能
添加相关文章功能相当有用,移植一个过来基本上可以实现比较复杂的页面内包含分类功能,做二次开发时可以省下不少力气. 用例:如果一个产品,属于一个厂家,而这个厂家是动态添加的,既不是一个分类,而是一个厂家 ...
- opencart 添加新模型
1,修改模板页,复制category_list.tpl,category_form.tpl,修改成新的名称. category_list.tpl是列出页面,category_form.tpl是添加和修 ...
- loadrunner录制回放常见问题及解决办法
1.录制错误 1)录制本机 WebTours 或录制本地网站无法打开 当 使 用 Vista 以 后 的 操 作 系 统 时 , 会 出 现 该 问 题 . 这 是 由 于 在 本 地Wi ...
- IO流(二)__BufferedReader和BufferedWriter
BufferedReader和BufferedWriter 字符流的缓冲区:缓冲区的而出现提高了对数据的读写效率对应类:BufferedWriter BufferedReader缓冲区要结合流才可以 ...