首页 > 代码库 > (转)将wcf 以webservice的方式调用
(转)将wcf 以webservice的方式调用
将wcf 以webservice的方式调用
问题: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):
namespace Microsoft.ServiceModel.Samples
{
// NOTE: If you change the interface name "IService1" here, you must also update the reference to "IService1" in Web.config.
[ServiceContract, XmlSerializerFormat]
public interface ICalculator
{
[OperationContract]
double Add(double n1, double n2);
[OperationContract]
double Subtract(double n1, double n2);
[OperationContract]
double Multiply(double n1, double n2);
[OperationContract]
double Divide(double n1, double n2);
}
public class CalculatorService : ICalculator
{
public double Add(double n1, double n2)
{
return n1 + n2;
}
public double Subtract(double n1, double n2)
{
return n1 - n2;
}
public double Multiply(double n1, double n2)
{
return n1 * n2;
}
public double Divide(double n1, double n2)
{
return n1 / n2;
}
}
}
配置文件:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<service
name="Microsoft.ServiceModel.Samples.CalculatorService"
behaviorConfiguration="CalculatorServiceBehavior">
<endpoint address=""
binding="basicHttpBinding"
contract="Microsoft.ServiceModel.Samples.ICalculator" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="CalculatorServiceBehavior">
<serviceMetadata httpGetEnabled="True"/>
<serviceDebug includeExceptionDetailInFaults="False" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</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),添加刚刚生成的代理类。调用:
调用代码:
class Client
{
static void Main()
{
// Create a client to the CalculatorService
using (CalculatorService client = new CalculatorService())
{
// 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);
}
Console.WriteLine();
Console.WriteLine("Press <ENTER> to terminate client.");
Console.ReadLine();
}
}
配置文件:
<?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
(完)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。