A quick and easy way to call a Web Service without the generated configuration

I recently had to call a web service without being able to use the section it adds to the application configuration file. The reason for this is that my project is a class library running in the process of a third-party payment system, whose code I neither own nor have access to, and I am not allowed to add anything to their configuration file.

My first thought was to search Stack Overflow, which I did, only to find the accepted solution was several more lines of code than my gut told me it should be. (And my gut demands that it is one line.)

My solution is quite simple:

  • Add a reference to the web service as you normally would.
  • Delete your config file (or the relevant section if you do have some other configuration that you care about).
  • Change your code that calls the generated reference’s constructor, where the reference is a class derived from System.ServiceModel.ClientBase. Use the constructor that takes two parameters: of type System.ServiceModel.Channels.Binding and System.ServiceModel.EndpointAddress (passing it a string with the URL of the endpoint it needs), as I have done below.
  • Call whatever methods your reference exposes. Since your reference is still strongly typed, this is simple.
using DOSequenceNumber.ReceiptNumberServiceReference;
using System.ServiceModel;

namespace DOSequenceNumber
    class Example
        private static string endPointAddress = "http://03rnb-i62dev01/ReceiptNumberService/ReceiptNumberService.svc";

        public static void ExampleMethod()
            IReceiptNumberService service = new ReceiptNumberServiceClient(
                new BasicHttpBinding(), new EndpointAddress(endPointAddress));

The advantage of doing this is that you hardly need change your code, compared to what it would have been if you could deploy the service reference configuration.

Just in case: Before you delete the generated configuration, maybe you should check what’s in it… You already know the URL (which you used when adding the service reference), but it might be worthwhile to make sure that the service you use is being called with basic binding, as mine is. Even if it isn’t, the code should still be just as easy. My impression is that most of the generated configuration is unnecessary.


About Jerome

I am a senior C# developer in Johannesburg, South Africa. I am also a recovering addict, who spent nearly eight years using methamphetamine. I write on my recovery blog about my lessons learned and sometimes give advice to others who have made similar mistakes, often from my viewpoint as an atheist, and I also write some C# programming articles on my programming blog.
This entry was posted in Programming and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s