Programmatically create endpoints in .NET WCF service

13,303

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!

Share:
13,303
E. Rodriguez
Author by

E. Rodriguez

Software engineer for a major telephony/data provider. Primarily developing with Sage SalesLogix these days (both LAN and Web); specialties are primarily web development.

Updated on June 13, 2022

Comments

  • E. Rodriguez
    E. Rodriguez almost 2 years

    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");
    
  • E. Rodriguez
    E. Rodriguez over 12 years
    Thanks. Your code is more concise than what I had cobbled together with Darjan's link.