以编程方式在.net WCF服务中创建端点
本文关键字:服务 创建 端点 WCF net 编程 方式 | 更新日期: 2023-09-27 18:14:47
我有一个被设计用来与web服务一起工作的类。现在,我编写它来查询http://www.nanonull.com/TimeService/TimeService.asmx。
类将被使用VBScript的遗留应用程序使用,所以它将使用命名空间实例化。名称约定。
我在编写代码以获得绑定和端点与我的类一起工作时遇到了麻烦,因为我将无法使用配置文件。我看到的示例讨论使用SvcUtil.exe,但我不清楚如何做到这一点,如果web服务是外部的。
谁能给我指个正确的方向?这就是我目前所拥有的,并且编译器在IMyService: 上崩溃了。 var binding = new System.ServiceModel.BasicHttpBinding();
var endpoint = new EndpointAddress("http://www.nanonull.com/TimeService/TimeService.asmx");
var factory = new ChannelFactory<IMyService>(binding, endpoint);
var channel = factory.CreateChannel();
HelloWorld = channel.getCityTime("London");
Darjan是对的。建议的web服务解决方案可以工作。使用svcutil生成代理的命令行为
svcutil.exe /language:cs /out:generatedProxy.cs /config:app.config http://www.nanonull.com/TimeService/TimeService.asmx
你可以忽略app.config,但是把generatedProxy.cs添加到你的解决方案中。接下来,您应该使用TimeServiceSoapClient,看一下:
using System;
using System.ServiceModel;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
TimeServiceSoapClient client =
new TimeServiceSoapClient(
new BasicHttpBinding(),
new EndpointAddress("http://www.nanonull.com/TimeService/TimeService.asmx"));
Console.WriteLine(client.getCityTime("London"));
}
}
}
基本上就是这样!