http://www.cnblogs.com/yiyisawa/archive/2008/12/16/1356191.html

问题: 假设有一个大型系统新版本使用wcf 作为服务端,生成wcf client 调用可以调用正常。 那如果当wcf 服务端出现问题或其他的原因我想再用回以前老版本的webservice或是jms server ,但客户端调用还是通过wcf client 调用。只通过更改配置来实现。

一、web service项目,添加一个普通service class .代码如下:

Code
[WebService(Namespace="http://Microsoft.ServiceModel.Samples")]
    public class CalculatorService : System.Web.Services.WebService
    {        
        [WebMethod]
        public double Add(double n1, double n2)
        {
            return n1 + n2;
        }
        [WebMethod]
        public double Subtract(double n1, double n2)
        {
            return n1 - n2;
        }
        [WebMethod]
        public double Multiply(double n1, double n2)
        {
            return n1 * n2;
        }
        [WebMethod]
        public double Divide(double n1, double n2)
        {
            return n1 / n2;
        }
    } 

webservice配置文件无需更改。运行。记录服务地址。

二、打开路径C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin:找到svcutil.exe文件。开始菜单-->run --> input cmd --->cd C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin -->回车;

输入svcutil http://localhost:8080/service/service.asmx,将会在C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin生成一个webservice的代理类。注意:此代理类是wcf client形式的。(在后面只需将这个代理类小作改动,便可用于wcf sevice.)

生成的代理类:

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:2.0.50727.3053
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------     [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
    [System.ServiceModel.ServiceContractAttribute(Namespace = "http://Microsoft.ServiceModel.Samples", ConfigurationName = "CalculatorServiceSoap")]
    public interface CalculatorServiceSoap
    {         [System.ServiceModel.OperationContractAttribute(Action = "http://Microsoft.ServiceModel.Samples/Add", ReplyAction = "*")]
        double Add(double n1, double n2);         [System.ServiceModel.OperationContractAttribute(Action = "http://Microsoft.ServiceModel.Samples/Subtract", ReplyAction = "*")]
        double Subtract(double n1, double n2);         [System.ServiceModel.OperationContractAttribute(Action = "http://Microsoft.ServiceModel.Samples/Multiply", ReplyAction = "*")]
        double Multiply(double n1, double n2);         [System.ServiceModel.OperationContractAttribute(Action = "http://Microsoft.ServiceModel.Samples/Divide", ReplyAction = "*")]
        double Divide(double n1, double n2);
    }     [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
    public interface CalculatorServiceSoapChannel : CalculatorServiceSoap, System.ServiceModel.IClientChannel
    {
    }     [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
    public partial class CalculatorServiceSoapClient : System.ServiceModel.ClientBase<CalculatorServiceSoap>, CalculatorServiceSoap
    {         public CalculatorServiceSoapClient()
        {
        }         public CalculatorServiceSoapClient(string endpointConfigurationName)
            :
                base(endpointConfigurationName)
        {
        }         public CalculatorServiceSoapClient(string endpointConfigurationName, string remoteAddress)
            :
                base(endpointConfigurationName, remoteAddress)
        {
        }         public CalculatorServiceSoapClient(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress)
            :
                base(endpointConfigurationName, remoteAddress)
        {
        }         public CalculatorServiceSoapClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress)
            :
                base(binding, remoteAddress)
        {
        }         public double Add(double n1, double n2)
        {
            return base.Channel.Add(n1, n2);
        }         public double Subtract(double n1, double n2)
        {
            return base.Channel.Subtract(n1, n2);
        }         public double Multiply(double n1, double n2)
        {
            return base.Channel.Multiply(n1, n2);
        }         public double Divide(double n1, double n2)
        {
            return base.Channel.Divide(n1, n2);
        }
    }

三、添加Console Application,将上面生成的代理类加入项目中,并在Main方法中调用。

 Service1Clients client = new Service1Clients();

           //   Service1Client client = new Service1Client();
            // Call the Add service operation.
            double value1 = 100.00D;
            double value2 = 15.99D;
            
            
            
            
            double result = client.Add(value1, value2);
            Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result);             // Call the Subtract service operation.
            value1 = 145.00D;
            value2 = 76.54D;
            result = client.Subtract(value1, value2);
            Console.WriteLine("Subtract({0},{1}) = {2}", value1, value2, result);             // Call the Multiply service operation.
            value1 = 9.00D;
            value2 = 81.25D;
            result = client.Multiply(value1, value2);
            Console.WriteLine("Multiply({0},{1}) = {2}", value1, value2, result);             // Call the Divide service operation.
            value1 = 22.00D;
            value2 = 7.00D;
            result = client.Divide(value1, value2);
            Console.WriteLine("Divide({0},{1}) = {2}", value1, value2, result);             //Closing the client gracefully closes the connection and cleans up resources
            client.Close();             Console.WriteLine();
            Console.WriteLine("Press <ENTER> to terminate client.");
            Console.ReadLine();

添加配置文件:App.config.此配置文件在二步生成代理类的时候会有Out.config同时产生。config里面的内容拷过来即可。

<system.serviceModel>
    
     <bindings>
      <basicHttpBinding>
        <binding name="CalculatorServiceSoap" 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:8080/service/service.asmx"
         binding="basicHttpBinding"
         contract="IService1" name="IService1" />
    </client>
  </system.serviceModel>

将webservice运行起来,(也可host到iis 里去。)debug console application.即可看到结果。

回家吃饭了。

细节和要注意的地方在第二节中写出来。

项目下载地址:http://files.cnblogs.com/yiyisawa/wcfclienttowebservice.rar

(转)wcf client与webservice通信(-)只修改配置文件而改变服务端的更多相关文章

  1. Android BLE与终端通信(三)——客户端与服务端通信过程以及实现数据通信

    Android BLE与终端通信(三)--客户端与服务端通信过程以及实现数据通信 前面的终究只是小知识点,上不了台面,也只能算是起到一个科普的作用,而同步到实际的开发上去,今天就来延续前两篇实现蓝牙主 ...

  2. c++ 网络编程(二) linux 下多进程socket通信 多个客户端与单个服务端交互代码实现回声服务器

    原文作者:aircraft 原文链接:https://www.cnblogs.com/DOMLX/p/9612820.html 锲子-- 预备知识优雅的关闭套接字连接: 基于TCP的半关闭 TCP中的 ...

  3. webservice快速入门-使用wsimport生成ws服务端(二)

    上个例子演示的是在当前项目下发布的Webservice Server,而实际应用中和Client是分离的,本文介绍两种客户端开发方式: 1.导出WebService服务端服务接口到jar包,客户端引入 ...

  4. socket 通信 入门3 android 客户端 C# 服务端

    这是一个android端操控服务器的例子  就是发送简单指令到服务器  然后服务器响应什么的... 当然这里是未完成的  只是简单展示一下大致思路 首先连接建立起来后  服务端给客户端一条信息  告诉 ...

  5. 【修改端口号】linux下修改apache,nginx服务端口号

    一.linux下修改apache端口号 yum安装后,apache配置文件: /etc/httpd/conf/httpd.conf 找到apache目录下的 httpd.conf, 使用vi 打开,找 ...

  6. iredmail邮件服务器之修改默认的web服务端口号

    安装iredmail之后,由于需要在路由器上做端口映射以便在外网访问webmail,因此端口不能和WEB服务的端口好冲突,所以需要修改邮件服务器的httpd服务的端口. 一.apache/httpd的 ...

  7. linux下修改apache,nginx服务端口号

    一.linux下修改apache端口号 yum安装后,apache配置文件: /etc/httpd/conf/httpd.conf 找到apache目录下的 httpd.conf, 使用vi 打开,找 ...

  8. HttpClient由Client客户端上传File文件流至Server服务端

    客户端方法 public static void main(String[] args) { File file=new File("E:\\lucene\\rev\\全年平台受理量.doc ...

  9. 我的WCF之旅(3):在WCF中实现双工通信

    双工(Duplex)模式的消息交换方式体现在消息交换过程中,参与的双方均可以向对方发送消息.基于双工MEP消息交换可以看成是多个基本模式下(比如请求-回复模式和单项模式)消息交换的组合.双工MEP又具 ...

随机推荐

  1. Querylayer(查询图层) - 浅谈

    Querylayer(查询图层)是通过 SQL 查询定义的图层或独立表.通过 Querylayer 可将空间信息和非空间信息都存储在DBMS 中,从而使这些信息可以轻松地整合到 ArcMap 中的各 ...

  2. Map Resource Definition 中type为 'ArcGIS Server Local'.

    在使用 MapResourceManager, Map进行 地图显示 时, 在 Map Resource Definition 的 type 为 ArcGIS Server Local时概述. 1,打 ...

  3. 什么是 Terminal

    从用户的角度来看,Terminal 是键盘和显示器的组合,也称为 TTY(电传打字机的缩写).键盘输入字符,显示器显示字符. 从进程的角度来看,终端是字符设备,可以通过 read.write.ioct ...

  4. 学习在创建好的工程里面添加CoreData

    在学习CoreData中, 在建工程后, 没有添加, 于是就参考网络文章进行更改. 这几天在学习做一个ios的小项目,项目中需要对数据进行基本的增删改查操作.于是就想用一把CoreData.但在创建项 ...

  5. $().text() 和 $().html()

    1:性能 stackflow:http://stackoverflow.com/questions/1910794/what-is-the-difference-between-jquery-text ...

  6. 后台构建 html 字符串传到前台字符串转码(html)处理

    知识在于总结,那就记下了吧! 例如后台 html 字符串是 var htmlStr="后台html字符串": 转码 var html格式代码=decodeHtml(htmlStr) ...

  7. 02.Lua的数据类型

    简单认识Lua 百度了一下(偷哈懒就不自己写了) Lua 是一个小巧的脚本语言.是巴西里约热内卢天主教大学(Pontifical Catholic University of Rio de Janei ...

  8. PHPExcel用法有感

    今日项目需求导出excel.我用最简单的header头方式导出了,但是需求部门退回了,说不满足他们的需求,需要我按照他们的模板来导出. 然后想到了PHPExcel,没用过,走了不少弯路. 1.去官网下 ...

  9. PHP打开PDO_MySQL扩展的配置方法

    PHP中的PDO其实是一个很好用的扩展,在一些PHPCMS系统中,开发者大多都有用到,那么如何开启PDO和PDO_MySQL扩展呢?方法同样很简单: 打开php.ini配置文件,找到extension ...

  10. Python新手学习基础之初识python——与众不同1

    Python是什么? 首先我们先简单介绍下python这门语言,Python是一种解释性的脚本语言,它不需要像C/C++那样先编译再执行,也不像JS那样可以在浏览器上直接执行.它为我们提供的基础代码库 ...