首页 > Web开发 > 详细

(转)将wcf 以webservice的方式调用

时间:2014-08-11 14:32:02      阅读:432      评论:0      收藏:0      [点我收藏+]

 

 

问题:a公司使用wcf 发布服务(.net Framework 3.0 or 3.5),b公司需要使用a公司发布的服务 ,但b公司目前阶段只使用.net Framework2.0(.net Framework 2.0不支持wcf),如果要调用a公司wcf 服务,那怎么办呢?

一、先上wcf 代码(这里懒得写了,借用microsoft公司发布的wcf  samples):

bubuko.com,布布扣
bubuko.com,布布扣namespace Microsoft.ServiceModel.Samples
bubuko.com,布布扣{
bubuko.com,布布扣    // NOTE: If you change the interface name "IService1" here, you must also update the reference to "IService1" in Web.config.
bubuko.com,布布扣    [ServiceContract, XmlSerializerFormat]
bubuko.com,布布扣    public interface ICalculator
bubuko.com,布布扣    {
bubuko.com,布布扣        [OperationContract]
bubuko.com,布布扣        double Add(double n1, double n2);
bubuko.com,布布扣        [OperationContract]
bubuko.com,布布扣        double Subtract(double n1, double n2);
bubuko.com,布布扣        [OperationContract]
bubuko.com,布布扣        double Multiply(double n1, double n2);
bubuko.com,布布扣        [OperationContract]
bubuko.com,布布扣        double Divide(double n1, double n2);
bubuko.com,布布扣    }
bubuko.com,布布扣
bubuko.com,布布扣    public class CalculatorService : ICalculator
bubuko.com,布布扣        {
bubuko.com,布布扣            public double Add(double n1, double n2)
bubuko.com,布布扣            {
bubuko.com,布布扣                return n1 + n2;
bubuko.com,布布扣            }
bubuko.com,布布扣
bubuko.com,布布扣            public double Subtract(double n1, double n2)
bubuko.com,布布扣            {
bubuko.com,布布扣                return n1 - n2;
bubuko.com,布布扣            }
bubuko.com,布布扣
bubuko.com,布布扣            public double Multiply(double n1, double n2)
bubuko.com,布布扣            {
bubuko.com,布布扣                return n1 * n2;
bubuko.com,布布扣            }
bubuko.com,布布扣
bubuko.com,布布扣            public double Divide(double n1, double n2)
bubuko.com,布布扣            {
bubuko.com,布布扣                return n1 / n2;
bubuko.com,布布扣            }
bubuko.com,布布扣        }
bubuko.com,布布扣}

 

配置文件:

bubuko.com,布布扣
bubuko.com,布布扣<?xml version="1.0" encoding="utf-8" ?>
bubuko.com,布布扣<configuration>
bubuko.com,布布扣  <system.serviceModel>
bubuko.com,布布扣    <services>
bubuko.com,布布扣      <service
bubuko.com,布布扣          name="Microsoft.ServiceModel.Samples.CalculatorService"
bubuko.com,布布扣          behaviorConfiguration="CalculatorServiceBehavior">
bubuko.com,布布扣        <endpoint address=""
bubuko.com,布布扣                  binding="basicHttpBinding"
bubuko.com,布布扣                  contract="Microsoft.ServiceModel.Samples.ICalculator" />
bubuko.com,布布扣      </service>
bubuko.com,布布扣    </services>
bubuko.com,布布扣    <behaviors>
bubuko.com,布布扣      <serviceBehaviors>
bubuko.com,布布扣        <behavior name="CalculatorServiceBehavior">
bubuko.com,布布扣          <serviceMetadata httpGetEnabled="True"/>
bubuko.com,布布扣          <serviceDebug includeExceptionDetailInFaults="False" />
bubuko.com,布布扣        </behavior>
bubuko.com,布布扣      </serviceBehaviors>
bubuko.com,布布扣    </behaviors>
bubuko.com,布布扣  </system.serviceModel>
bubuko.com,布布扣</configuration>

 

运行,记录服务地址。

二、使用wsdl工具将wcf service生成asp.net webservice方式。

start --->运行--->cmd --->cd C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin  回车。

输入wsdl 服务地址。例(wsdl http://localhost:8571/Service1.svc )便生成相应的类似asp.net webservice代理类的文件。文件地址亦在上面bin中。

新建Console application (net Framework 2.0),添加刚刚生成的代理类。调用:

调用代码:

bubuko.com,布布扣
bubuko.com,布布扣 class Client
bubuko.com,布布扣    {
bubuko.com,布布扣        static void Main()
bubuko.com,布布扣        {
bubuko.com,布布扣            // Create a client to the CalculatorService
bubuko.com,布布扣            using (CalculatorService client = new CalculatorService())
bubuko.com,布布扣            {
bubuko.com,布布扣                // Call the Add service operation.
bubuko.com,布布扣                double value1 = 100.00D;
bubuko.com,布布扣                double value2 = 15.99D;
bubuko.com,布布扣                double result = client.Add(value1, value2);
bubuko.com,布布扣                Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result);
bubuko.com,布布扣
bubuko.com,布布扣                // Call the Subtract service operation.
bubuko.com,布布扣                value1 = 145.00D;
bubuko.com,布布扣                value2 = 76.54D;
bubuko.com,布布扣                result = client.Subtract(value1, value2);
bubuko.com,布布扣                Console.WriteLine("Subtract({0},{1}) = {2}", value1, value2, result);
bubuko.com,布布扣
bubuko.com,布布扣                // Call the Multiply service operation.
bubuko.com,布布扣                value1 = 9.00D;
bubuko.com,布布扣                value2 = 81.25D;
bubuko.com,布布扣                result = client.Multiply(value1, value2);
bubuko.com,布布扣                Console.WriteLine("Multiply({0},{1}) = {2}", value1, value2, result);
bubuko.com,布布扣
bubuko.com,布布扣                // Call the Divide service operation.
bubuko.com,布布扣                value1 = 22.00D;
bubuko.com,布布扣                value2 = 7.00D;
bubuko.com,布布扣                result = client.Divide(value1, value2);
bubuko.com,布布扣                Console.WriteLine("Divide({0},{1}) = {2}", value1, value2, result);
bubuko.com,布布扣
bubuko.com,布布扣            }
bubuko.com,布布扣
bubuko.com,布布扣            Console.WriteLine();
bubuko.com,布布扣            Console.WriteLine("Press <ENTER> to terminate client.");
bubuko.com,布布扣            Console.ReadLine();
bubuko.com,布布扣        }
bubuko.com,布布扣    }

 

 配置文件:

 

 

bubuko.com,布布扣
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="CalculatorServiceAddress" value="http://localhost:8571/Service1.svc"/>
  </appSettings>
</configuration>

运行,即可成功调用。

项目完整代码。http://files.cnblogs.com/yiyisawa/wcfclienttowebservice.rar

(完)

(转)将wcf 以webservice的方式调用,布布扣,bubuko.com

(转)将wcf 以webservice的方式调用

原文:http://www.cnblogs.com/wanshutao/p/3904338.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!