WCF学习之旅(一)---Hello World.

看了一些关于WCF的资料,从实例入手。记录自己学习WCF的过程。

从最简单的Hello World入门。

下图是我的solution及项目。

项目名称

功能

WCF.Client.Consoles

WCF客户端程序

WCF.Contracts

契约。接口程序

DataContract,ServiceContract,MessageContract

WCF.Host.Consoles

宿主程序

WCF.Proxys

客户端调用的代理类用SvcUtil.exe工具生成

WCF.Service.Web

发布service的站点。

WCF.ServiceLib

实现Contract的Library

Contract

IHello.cs

Code
    [ServiceContract]
    public interface IHello
    {
        [OperationContract]
        void SayHello(string name);
    }

实现类

Hello.cs

Code
    public class Hello:IHello
    {
        #region IHello Members

public void SayHello(string name)
        {
            Console.WriteLine(string.Format("Hello {0}", name));
        }

#endregion
    }

Service

Hello.svc

<%@ ServiceHost Language="C#" Debug="true" Service="Fastyou.WCF.ServiceLib.Hello" %>

Web.config

Code
<system.serviceModel>
    <services>
      <service behaviorConfiguration="Fastyou.WCF.ServiceLib.HelloBehavior"
        name="Fastyou.WCF.ServiceLib.Hello">
        <endpoint address="" binding="basicHttpBinding" contract="Fastyou.WCF.Contracts.IHello">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="Fastyou.WCF.ServiceLib.HelloBehavior">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

Host程序

Code
        static void Main(string[] args)
        {
            try
            {
                ServiceHost helloHost = new ServiceHost(typeof(Hello));
                helloHost.Opened += delegate
                {
                    Console.WriteLine("服务已经启动!");
                    
                };
                helloHost.Open();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.Read();
        } 

App.config

Code
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <services>
            <service name="Fastyou.WCF.ServiceLib.Hello" behaviorConfiguration="helloBehavior">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8089/Hello"/>
          </baseAddresses>
        </host>
        <endpoint address="Hello" binding="basicHttpBinding" contract="Fastyou.WCF.Contracts.IHello">
                    <identity>
                        <dns value="localhost"/>
                    </identity>
                </endpoint>
        <endpoint binding="mexHttpBinding" contract="IMetadataExchange" address="mex" />
            </service>
        </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="helloBehavior">
          <serviceMetadata httpGetEnabled="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

由于我的hello.svc是放在hello文件夹。所以这里的address 会有一个hello. baseAddress已经有一个Hello.

HelloProxys.cs

Code

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
    [System.ServiceModel.ServiceContractAttribute(ConfigurationName = "IHello")]
    public interface IHello
    {

[System.ServiceModel.OperationContractAttribute(Action = "http://tempuri.org/IHello/SayHello", ReplyAction = "http://tempuri.org/IHello/SayHelloResponse")]
        void SayHello(string name);
    }

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
    public interface IHelloChannel : IHello, System.ServiceModel.IClientChannel
    {
    }

[System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
    public partial class HelloClient : System.ServiceModel.ClientBase<IHello>, IHello
    {

public HelloClient()
        {
        }

public HelloClient(string endpointConfigurationName) :
            base(endpointConfigurationName)
        {
        }

public HelloClient(string endpointConfigurationName, string remoteAddress) :
            base(endpointConfigurationName, remoteAddress)
        {
        }

public HelloClient(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) :
            base(endpointConfigurationName, remoteAddress)
        {
        }

public HelloClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) :
            base(binding, remoteAddress)
        {
        }

public void SayHello(string name)
        {
            base.Channel.SayHello(name);
        }
    }

Client的program.cs

Code
static void Main(string[] args)
        {
            HelloWorld();
        }
        static void HelloWorld()
        {
            try
            {
                Console.WriteLine("-----------------------request begin-----------------------");
                #region code for client;
                //BasicHttpBinding bind = new BasicHttpBinding();
                //EndpointAddress address = new EndpointAddress("http://localhost:8089/hello/hello");

//HelloClient ws = new HelloClient(bind, address);
                //ws.SayHello("I'm fastyou. \r\n");
                //ws.Close();
                #endregion

#region configuration for client

HelloClient hc = new HelloClient("BasicHttpBinding_IHello");
                hc.SayHello("I'm fastyou. \r\n");
                hc.Close();

#endregion

Console.WriteLine("-------------------------request end------------------------------------");
                Console.WriteLine("\r\n\r\n\r\n");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

Console.Read();
        }

App.config

Code
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBinding_IHello" closeTimeout="00:01:00"
            openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
            allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
            maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
            messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
            useDefaultWebProxy="true">
          <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
              maxBytesPerRead="4096" maxNameTableCharCount="16384" />
          <security mode="None">
            <transport clientCredentialType="None" proxyCredentialType="None"
                realm="" />
            <message clientCredentialType="UserName" algorithmSuite="Default" />
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://localhost:8089/Hello/Hello"
          binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IHello"
          contract="IHello" name="BasicHttpBinding_IHello" />
    </client>
  </system.serviceModel>
</configuration>

编译通过及配置好IIS后先运行host再运行Client.这时可以看到host程序中会出现hello,I’m XXX.

代码下载

转载于:https://www.cnblogs.com/laihua/archive/2008/11/21/1338729.html

WCF学习之旅(一)---Hello World.相关推荐

  1. WCF学习之旅—WCF服务的WAS寄宿(十二)

    上接    WCF学习之旅-WCF服务部署到IIS7.5(九) WCF学习之旅-WCF服务部署到应用程序(十) WCF学习之旅-WCF服务的Windows 服务程序寄宿(十一) 八.WAS宿主 IIS ...

  2. WCF学习之旅—基于Fault Contract 的异常处理(十八)

       WCF学习之旅-WCF中传统的异常处理(十六) WCF学习之旅-基于ServiceDebug的异常处理(十七) 三.基于Fault Contract 的异常处理 第二个示例是通过定制Servic ...

  3. WCF学习之旅—第三个示例之二(二十八)

    上接WCF学习之旅-第三个示例之一(二十七) 五.在项目BookMgr.Model创建实体类数据 第一步,安装Entity Framework 1)  使用NuGet下载最新版的Entity Fram ...

  4. WCF学习之旅—实现支持REST客户端应用(二十四)

    WCF学习之旅-实现REST服务(二十二) WCF学习之旅-实现支持REST服务端应用(二十三) 在上二篇文章中简单介绍了一下RestFul与WCF支持RestFul所提供的方法,及创建一个支持RES ...

  5. WCF学习之旅—第三个示例之四(三十)

           上接WCF学习之旅-第三个示例之一(二十七)               WCF学习之旅-第三个示例之二(二十八)              WCF学习之旅-第三个示例之三(二十九)   ...

  6. WCF学习之旅—WCF第二个示例(七)

    三.创建客户端应用程序 若要创建客户端应用程序,你将另外添加一个项目,添加对该项目的服务引用,配置数据源,并创建一个用户界面以显示服务中的数据. 在第一个步骤中,你将 Windows 窗体项目添加到解 ...

  7. WCF学习之旅—请求与答复模式和单向模式(十九)

    一.概述 WCF在通信过程中有三种模式:请求与答复.单向.双工通信.以下我们一一介绍. 二.请求与答复模式 客户端发送请求,然后一直等待服务端的响应(异步调用除外),期间处于假死状态,直到服务端有了答 ...

  8. WCF学习之旅—WCF寄宿前的准备(八)

    一.WCF服务应用程序与WCF服务库 我们在平时开发的过程中常用的项目类型有"WCF 服务应用程序"和"WCF服务库". WCF服务应用程序,是一个可以执行的程 ...

  9. WCF学习之旅—WCF概述(四)

    一.WCF概述 1) 什么是WCF? Windows Communication Foundation (WCF) 是用于构建面向服务的应用程序的框架.借助 WCF,可以将数据作为异步消息从一个服务终 ...

最新文章

  1. 2011 ScrumGathering敏捷个人.pptx
  2. etcd 指定配置文件启动_5步完成 etcd 单机集群部署
  3. pytorch笔记:09)Attention机制
  4. tableau必知必会之通过 Tableau 计算挖掘数据真相
  5. .NET再出发!20岁生日快乐
  6. 实例24:python
  7. 叠置分析(涉及结果属性和输入图层属性的关系)
  8. linux学习查看系统资源和磁盘分区
  9. 【VS】项目属性配置(上)
  10. Android实现XML解析技术
  11. 真的有这么丝滑吗?近日国外一小哥深入研究了KMP算法……
  12. seaweedfs java api_seaweedfs代码详解
  13. 可测函数积分的进一步性质
  14. 中国oracle ace名单
  15. Python 爬虫监控女神的QQ空间新的说说,实现秒赞,并发送说说内容到你的邮箱
  16. Mac port 安装postgres92 + postgres92-server
  17. Cesium上帝视角漫游
  18. 南京理工大学计算机考研大纲,2021南京理工大学考研大纲参考书目
  19. im即时通讯开发:聊天消息的同步和存储
  20. linux及安全期中总结——20135227黄晓妍

热门文章

  1. R+NLP︱text2vec包——BOW词袋模型做监督式情感标注案例(二,情感标注)
  2. arts-week12
  3. 玩转Android Camera开发(一):Surfaceview预览Camera,基础拍照功能完整demo
  4. SQL*Plus环境下创建PLUSTRACE角色
  5. C#实现WebService服务 项目完整总结
  6. 高性能编程之IO复用之epoll
  7. Oracle控制文件操作
  8. python 中类与对象
  9. Ubuntu解压文件的方法
  10. C#程序员66个编码好习惯