以编程方式在.NET WCF服务中创建端点

E. *_*uez 8 .net c# wcf web-services

我有一个专门用于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");
Run Code Online (Sandbox Code Playgroud)

nbu*_*lba 9

Darjan是对的.Web服务的建议解决方案有效.使用svcutil生成代理的命令行是

svcutil.exe /language:cs /out:generatedProxy.cs /config:app.config http://www.nanonull.com/TimeService/TimeService.asmx
Run Code Online (Sandbox Code Playgroud)

您可以忽略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"));
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

基本上就是这样!