近来学习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. Windows Azure Web Site (16) Azure Web Site HTTPS

    <Windows Azure Platform 系列文章目录> 我们在使用微软云Azure Web App的时候,会使用微软的二级域名:http://xxx.chinacloudsites ...

  2. Windows Azure Cloud Service (42) 使用Azure In-Role Cache缓存(1)Co-located Role

    <Windows Azure Platform 系列文章目录> Update 2016-01-12 https://azure.microsoft.com/zh-cn/documentat ...

  3. git gitignore文件失效处理

    这里讲的是使用 git ignore 时候的一种特殊情况   正常你在本地给项目添加了一些文件之后,一般都会自动全部跟踪,但是在这个时候你必须编辑一个ignore文件,把一些不需要跟踪到文件ignor ...

  4. JAVA - HashMap,TreeMap迭代

    1.使用for_each循环迭代 public class TestUnit { public static void main(String[] args) { HashMap hashMap=ne ...

  5. MVC 4.0 学习中遇到的bug

     1.0 _ViewStart.cshtml  _ViewStart.cshtml 里面的如果写了 <script src="/Scripts/jquery-1.8.2.js" ...

  6. 激活当前视图菜单高亮呈现 V2.0

    前一段时间,Insus.NET有分享一篇<激活当前视图菜单高亮呈现>http://www.cnblogs.com/insus/p/5287093.html 这篇只是同一控制器的菜单. 今天 ...

  7. android 开发:shape和selector和layer-list的(详细说明)

    目录(?)[+] Shape 简介 使用的方法 属性 Selector 简介 使用的方法 layer-list 简介 例子 最后   <shape>和<selector>在An ...

  8. Swift 值类型和引用类型

    Swift中的类型分为两类:一,值类型(value types),每个值类型的实例都拥有各自唯一的数据,通常它们是结构体,枚举或元组:二,引用类型(reference types),引用类型的实例共享 ...

  9. 浅谈Oracle中物理结构(数据文件等。。。)与逻辑结构(表空间等。。。。。)

    初始Oracle时很难理解其中的物理结构和逻辑结构,不明白内存中和硬盘中文件的区别和联系,我也是初学Oracle,这里就简单的谈谈我我看法. 首先,你需要明白的一点是:数据库的物理结构是由数据库的操作 ...

  10. 基于.Net Framework 4.0 Web API开发(4):ASP.NET Web APIs 基于令牌TOKEN验证的实现

    概述:  ASP.NET Web API 的好用使用过的都知道,没有复杂的配置文件,一个简单的ApiController加上需要的Action就能工作.但是在使用API的时候总会遇到跨域请求的问题, ...