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

 

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

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

    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.)

生成的代理类:

 

    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方法中调用。

 

            Console.ReadLine();

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

 

    </client>
  </system.serviceModel>

 

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

回家吃饭了。(转)wcf client与webservice通信(-)只修改配置文件而改变服务端

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

 

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

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-10-17
  • 2022-03-11
  • 2022-01-11
  • 2021-08-30
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-03
  • 2022-12-23
  • 2022-03-07
  • 2021-09-17
相关资源
相似解决方案