Consuming a web reference in a class library with C# and .NET 3.5

Since .NET 3.5 you can’t add a web reference to a class library project, so how can you access web services?

Actually it is much easier.

  1. Right click the Class Library project and select Add Service Reference…
  2. Add the URL of the Web Service and click Go
  3. Give it a meaningful Namespace then click OK

Visual Studio will add a Service References folder and a system.serviceModel to the app.config file.

To invoke methods of the web service, first you create an instance of the SoapClient class then just use that object:

MyService.WebService1SoapClient myClient = new MyService.WebService1SoapClient();

In my example above MyService is the Namespace I used in the Add Service Reference dialog.

That is all there is to it.

2 Replies to “Consuming a web reference in a class library with C# and .NET 3.5”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.