近来学习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分分钟入门的更多相关文章

  1. C#面向服务编程技术WCF从入门到实战演练

    一.WCF课程介绍 1.1.Web Service会被WCF取代吗? 对于这个问题阿笨的回答是:两者在功能特性上却是有新旧之分,但是对于特定的系统,适合自己的就是最好的.不能哪一个技术框架和行业标准作 ...

  2. WCF 程序入门

    WCF是微软公司推出的符合SOA思想的分布式应用程序技术框架和编程模型,是建立在消息通信这一概念基础上运行的一个运行时服务系统. WCF编程模型的目标是实现以下两个实体之间的通信:WCF服务端和WCF ...

  3. WCF 快速入门

    定义服务契约 构建HelloWCF应用的第一步是创建服务契约.契约式是表示消息应用外形的主要方式.对于外形,是指服务暴露的操作,使用的消息 schema和每个操作实现的消息交换模式(MEP).总之,契 ...

  4. C# WCF服务入门

    之前在公司用的服务端是wcf写的,但是没有深入研究,最近找工作,面试的时候好多人看到这个总提问,这里做个复习 就用微软官方上的例子,搭一个简单的wcf服务,分6步 1 定义服务协定也就是契约,其实就是 ...

  5. WCF的入门教程dome(一)

    一.概述 Windows Communication Foundation(WCF)是由微软发展的一组数据通信的应用程序开发接口,可以翻译为Windows通讯接口,它是.NET框架的一部分.由 .NE ...

  6. Python 基础:分分钟入门

    Python和Pythonic Python是一门计算机语言(这不是废话么),简单易学,上手容易,深入有一定困难.为了逼格,还是给你们堆一些名词吧:动态语言.解释型.网络爬虫.数据处理.机器学习.We ...

  7. WCF宿主实践入门

    本篇属于WCF实践入门,由于博主本人水平有限,没有理论上的介绍,仅仅从其几种不同的宿主方式分别介绍WCF的使用. WCF有多种宿主方式:1.自托管宿主,2.windows service宿主,3.II ...

  8. WCF学习笔记1--发布使用配置文件的服务

    关于WCF的入门网上资料很多,可以参考蒋金楠老师的博客http://www.cnblogs.com/artech/archive/2007/02/26/656901.html,我是从这篇博客开始学习的 ...

  9. [老老实实学WCF] 第一篇 Hello WCF

    老老实实学WCF  第一篇 Hello WCF WCF(Windows Communication Foundation)是微软公司推出的面向服务技术的集大成者,涵盖继承了其之前发布的所有的分布式应用 ...

随机推荐

  1. Android自动化测试之Monkeyrunner学习笔记(一)

    Android自动化测试之Monkeyrunner学习笔记(一) 因项目需要,开始研究Android自动化测试方法,对其中的一些工具.方法和框架做了一些简单的整理,其中包括Monkey.Monkeyr ...

  2. JS魔法堂:LINK元素深入详解

    一.前言 我们一般使用方式为 <link type="text/css" rel="stylesheet" href="text.css&quo ...

  3. 使用Nodejs+Protractor搭建测试环境

    Protractor是一个end-to-end的测试框架,从网络上得到的答案是Protractor是作为Angular JS应用程序的测试框架.它的构建基于Selenium WebDriver之上,且 ...

  4. IOS 回收键盘通用代码

    感觉IOS的键盘回收好累,所以封装了一个通用一点的方法 -(IBAction)spbResignFirstResponder:(id)sender { // NSLogObj(sender); if ...

  5. Spring MVC 对于@ModelAttribute 、@SessionAttributes 的详细处理流程

    初学 Spring MVC , 感觉对于 @ModelAttribute 和 @SessionAttributes 是如何被Spring MVC处理的,这一流程不是很清楚, 经过Google资料,有了 ...

  6. iOS版本比较方法

    之前一直把版本号转换为floatValue,但是最近在项目中又出现了float的问题,主要是 NSString表示为@"17.30",转换为floatValue 值为17.2999 ...

  7. 理清那么多个OO(面向对象)

    OOA - Object-Oriented Analysis(面向对象分析) OOT - Object-Oriented Testing (面向对象测试) OOP - Object-Oriented ...

  8. 用node-webkit(NW.js)创建桌面程序

    以往写windows桌面程序需要用MFC.C#之类的技术,那么如果你只会web开发技术呢?或者说你有一个网站,但是你想把你的网站打包成一个桌面应用程序,该如何做呢? 答案就是用node-webkit这 ...

  9. 【C#进阶系列】21 托管堆和垃圾回收

    托管堆基础 一般创建一个对象就是通过调用IL指令newobj分配内存,然后初始化内存,也就是实例构造器时做这个事. 然后在使用完对象后,摧毁资源的状态以进行清理,然后由垃圾回收器来释放内存. 托管堆除 ...

  10. java入门必备单词

    ① anchor 锚 锚点 ② administrator 管理员 ③ application 应用程序 ④ align 对齐 ⑤ attribute 属性 ⑥ access 访问 ⑦ break 暂 ...