近来学习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. C#中enum类型

    最近碰到了枚举类型,就顺便整理下. 枚举的基类Enum,可以是除 Char 外的任何整型.不做显示声明的话,默认是整形(Int32). 声明一个Enum类型: /// <summary> ...

  2. Visual Studio 2013 和 ASP.NET 预览

    VS 2013预览版在2013的TechEd大会由Brain Harry正式发布.这次发布包括了一系列的新特性:工程模板.Scaffolding 升级和Web工具.当你在VS 2013创建一个新工程的 ...

  3. 修改cdh5集群中主机节点IP或hostName

    前言 在使用cdh集群过程中,难免会因为某些不可抗拒的原因导致节点IP或hostName变动,而cm的监控界面无法完成这些事情,但是cm将集群中所有的主机的信息都存在postgresql数据库的hos ...

  4. 用原生JS读写CSS样式的方法总结

    为了日后方便查询,本人翻阅了一些资料总结了以下方法,仅限原生JS,如有不对的地方欢迎指出!只求大家看完觉得有学到点什么就OK了!   一.可以通过DOM节点对象的style对象(即CSSStyleDe ...

  5. jquery删除数组中重复元素

    首先定义如下数组: var arr=[0,2,3,5,6,9,2]; 我们可以看到数组中存在重复元素'2'; 最后通过jquery筛选应该得到[0,2,3,5,6,9]; ok,首先我们再定义一个空数 ...

  6. 我所理解的readonly和const

    最近要给学校软件小组新成员讲几次课,所以把很多以前懒得学习的和模糊不清的知识点,重新学习了一下. MSDN是这样解释的: readonly 关键字与 const 关键字不同. const 字段只能在该 ...

  7. SingalR--介绍

    什么是SignalR? ASP.NET SignalR是为简化开发开发人员将实时web内容添加到应用程序过程而提供的类库.实时web功能指的是让服务器代码可以随时主动推送内容给客户端,而不是让服务器等 ...

  8. Java NIO:NIO概述

    Java NIO:NIO概述 在上一篇博文中讲述了几种IO模型,现在我们开始进入Java NIO编程主题.NIO是Java 4里面提供的新的API,目的是用来解决传统IO的问题.本文下面分别从Java ...

  9. Web API:将FlexChart导出为图片

    如果想要将FlexChart在应用之外使用,比如使用在报表中,Web API帮助你将FlexChart导出成任何你需要的图片格式. 下面是实现的步骤: 1:创建FlexChart 2:调用Servic ...

  10. jquery基本选择器匹配多个元素

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...