近来学习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. Responsive Web CSS – 在线响应式布局创建器

    如果您已经使用了 CSS 或前端框架,创建响应式布局应该不难. 然而,如果你刚涉足这类布局,Responsive Web CSS 可以帮助你快速上手. 这是一个基于 Web 的工具,使任何人都可以通过 ...

  2. jsonp跨域问题

    JSONP是一个非官方的协议,它允许在服务器端集成Script tags返回至客户端,通过javascript callback的形式实现跨域访问(这仅仅是JSONP简单的实现形式). 同源策略限制 ...

  3. Windows Azure Virtual Machine (26) 使用高级存储(SSD)和DS系列VM

    <Windows Azure Platform 系列文章目录> Update: 2016-11-3,如果大家在使用Linux VM,使用FIO进行IOPS测试的时候,请使用以下命令: su ...

  4. 20套高品质的 Mobile & Web 界面 PSD 素材免费下载

    在这里,我们向大家呈现20个新鲜出炉的矢量的免费 PSD 素材.这些素材来自著名的设计社区——Dribbble,这个网站的用户不断发布各种精美的用户界面,图标和网站布局,以帮助激励他人.这些免费素材不 ...

  5. jquery基础知识汇总

    jquery基础知识汇总 一.简介 定义 jQuery创始人是美国John Resig,是优秀的Javascript框架: jQuery是一个轻量级.快速简洁的javaScript库.源码戳这 jQu ...

  6. 使用Qt installer framework制作安装包

    一.介绍 使用Qt库开发的应用程序,一般有两种发布方式:(1)静态编译发布.这种方式使得程序在编译的时候会将Qt核心库全部编译到一个可执行文件中.其优势是简单单一,所有的依赖库都集中在一起,其缺点也很 ...

  7. c# 指定的存储区提供程序在配置中找不到,或者无效

    <system.data> <DbProviderFactories> <remove invariant="System.Data.SQLite"/ ...

  8. 【转】Key/Value之王Memcached初探:一、掀起Memcached的盖头来

    一.Memcached是何方神圣? 在数据驱动的Web开发中,经常要重复从数据库中取出相同的数据,这种重复极大的增加了数据库负载.缓存是解决这个问题的好办法.但是ASP.NET中的HttpRuntim ...

  9. Web API Filter ActionFilterAttribute 使用

    WebApi 提供两种过滤器的类型: 1.ActionFilterAttribute 2.exceptionFilterAttribute 两个类都是抽象类,ActionFilter 主要实现执行请求 ...

  10. 微信、QQ、微博、陌陌……社交网络的底层逻辑是什么?

      两 年前的社交产品泛滥犹在眼前,场景之胜几乎到了言必谈社交的地步.时任阿里新CEO陆兆禧举全集团之力,力推新社交产品“来往”,动作之大震惊整个互联 网.如今,陆兆禧早早退场,只留下一个硬汉的孤独背 ...