Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically create endpoints in .NET WCF service

I have a class that is designed to work with a web service. For now, I've written it to query http://www.nanonull.com/TimeService/TimeService.asmx.

The class is going to be used by a legacy app that uses VBScript, so it's going to be instantiated using Namespace.ClassName conventions.

I'm having trouble writing the code to get bindings and endpoints working with my class, because I won't be able to use a config file. The samples that I have seen discuss using SvcUtil.exe but I am unclear how to do this if the web service is external.

Can anyone point me in the right direction? This is what I have so far, and the compiler is crashing on 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");
like image 724
E. Rodriguez Avatar asked Nov 16 '11 23:11

E. Rodriguez


1 Answers

Darjan is right. The suggested solution with the web service works. The command line for proxy generation with svcutil is

svcutil.exe /language:cs /out:generatedProxy.cs /config:app.config http://www.nanonull.com/TimeService/TimeService.asmx

You can ignore app.config, however add generatedProxy.cs to your solution. Next, you should use TimeServiceSoapClient, take a look:

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

Basically that's it!

like image 171
nbulba Avatar answered Sep 20 '22 03:09

nbulba