【问题标题】:Programmatically create endpoints in .NET WCF service以编程方式在 .NET WCF 服务中创建端点
【发布时间】:2011-11-16 23:24:09
【问题描述】:

我有一个旨在使用 Web 服务的课程。目前,我已将其编写为查询http://www.nanonull.com/TimeService/TimeService.asmx

该类将由使用 VBScript 的旧版应用程序使用,因此将使用 Namespace.ClassName 约定对其进行实例化。

我在编写代码以使绑定和端点与我的类一起工作时遇到问题,因为我将无法使用配置文件。我看到的示例讨论了使用 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");

【问题讨论】:

  • “编译器崩溃”是什么意思?错误是什么?
  • 愚蠢的问题,但那是 ASMX 服务而不是 WCF 服务,对吧?
  • Darjan - 我试过了,但我不清楚要使用的类型,这让我想到:Charles - 在尝试解析 ChannelFactory 构造函数中的 Type 时它的轰炸。在 Darjan 的帖子中,我也不清楚那里有什么论据。 Jeremy 这是我试图调用的 ASMX 服务。感谢您的输入。我觉得我快到了,但错过了一些愚蠢的东西。

标签: c# .net wcf web-services


【解决方案1】:

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"));
    }
  }
}

基本上就是这样!

【讨论】:

  • 谢谢。你的代码比我用 Darjan 的链接拼凑的更简洁。
猜你喜欢
  • 1970-01-01
  • 2010-12-11
  • 1970-01-01
  • 2023-02-22
  • 2023-03-09
  • 1970-01-01
  • 2017-03-30
  • 2012-02-07
相关资源
最近更新 更多