Geeks With Blogs

My Visual Identity
This is a Flickr badge showing photos in a set called Personal Favorites. Make your own badge here.


maina donaldson a pragmatist's blog

Adding references to WCF services is trivially easy with Visual Studio 2008, but if you're still working with 2005 there are a few things that need to be considered when referencing WCF services instead of using Add Web Reference

The following step-by-step is meant to help particularly those who have not developed WCF services but just need to consume them.  It applies to both, VS 2005 and VS 2008.


Prerequisite installs for Visual Studio 2005

If you are in a library, console, or Windows forms project, you should be seeing an Add Service Reference item in your project's context menu. If you don't see this, you are missing some prerequisites. Download and install the following packages:

  1. .NET Framework 3.0 Redistributable
    These are the libraries that contain the WCF namespaces, such as System.ServiceModel and System.Runtime.Serialization. This package also contains WF and WPF runtime libraries.
  2. Visual Studio 2005 "Orcas" Extensions
    The WCF and WPF (Windows Presentation Foundation) extensions that enable IDE integration into Visual Studio 2005.
  3. .NET Framework 3.0 SP1

You should not have to install the Windows SDK (it's a huge download) to enable IDE integration, even if the Extensions install complains that it's not present. Ignore this message if you are only interested in consuming WCF services.


VS 2005 web site projects

In Visual Studio 2005, you will not see an Add Service Reference command in web site projects, even after installing above prerequisites.  Apparently this was an oversight and didn't quite make it into the Extensions CTP release, because VS 2008 has this option.  To work around this you have two options:

  • Create a class library project, add it to your solution, and create your service references there. You will need to transfer generated app.config sections to your web.config (see below). 
  • Generate your proxy classes and configurations using svcutil.exe utility, and include the generated files in the web site project.


Adding the Service Reference

  1. Test the service from your web browser. Navigate to the .svc file. If everything checks out, copy the URL from the Address bar.
  2. Right-click on the project in the Solution Explorer window and choose Add Service Reference. In VS 2005, you'll see the following dialog.
    Add Service Reference Dialog
  3. Enter (paste) the URL to your service's .svc file. (.svc?wsdl works as well)
  4. Give the service a short but descriptive name. This will become a part of your proxy class' namespace (more about that later).

In VS 2008, you see more a bit more information about the service, plus you get a lot more control over generated data types through the Advanced options dialog:

This will generate several files. What files are generated differs between VS 2005 and VS 2008, and between non-web and web projects in VS 2008. Essentially you will come out of this operation with a proxy class and some configuration values.


The client proxy class

When you expand the tree under the service reference, you'll find a .cs file (VS 2005) that contains your proxy class. Note that it adopted the root namespace of the project it was generated in. I mention this because this is an important difference from how .asmx references were created, and also from how svcutil.exe creates your proxy.

proxy namespace

To instantiate the proxy class, use the following naming pattern:

ProjectRootNamespace.ServiceReferenceName.ServiceNameClient myServiceClient =
               new ProjectRootNamespace.ServiceReferenceName.ServiceNameClient("EndpointConfigurationName");

Notice the class name ends in Client.  Where do you get the EndpointConfigurationName from?  Glad you asked. Read on...


Configuration considerations

Besides generating a proxy class, Visual Studio's also writes a section into your .config file when you Add a Service Reference. This section is bounded by

<system.serviceModel>  </system.serviceModel>

tags. If you generated the proxy in a class library project, you will have to copy this section to the executing assembly project's .config file. 


One of the nice advantages of using a service reference over a web reference is that you have complete control over the channel stack, security, protocols, etc. through configuration. The generated configuration mimics the service's configuration on the server. The things to understand are:

  • Endpoints, in the <client> node, consist of an Address, a Binding and a Contract.  A service can expose multiple endpoints, but since you are pointing your reference tool at a specific endpoint, and not the service directly, you will only ever get one from the tool. The endpoint has a name, which you need when instantiating a the client proxy (so EndpointConfigurationName above would be replaced with MyWCFServiceBasicHttp in this example).
  • The Binding defined in the <bindings> node defines the communication patterns. It is referenced by the endpoint with the bindingConfiguration attribute.
  • The Contract defined by a fully qualified interface name. Notice that the namespace is NOT the namespace defined in the service, but the one your proxy class resides in. This may be confusing especially if you also developed the service.  If you instantiate your proxy class with above example, you don't need to care about the interface .


Again, remember to copy these sections into the executing assembly's .config file!


Updating a service reference

If the service implementation changes without data- or service contract (method signature) changes, you don't need to do anything at the client. If the contract or address has changed, however, you need to re-generate your proxy and may need to update your configuration. This can also be done through the IDE. 

  • Right-click on the reference in Solution Explorer and choose Update Service Reference.
    This will also replace the .config section, regardless of server config changes or not. If you made config changes in your client config, be aware that you may end up with double entries with names such as MyWCFServiceBasicHttp1 (to use the example above). You should clean those up, or you will end up with a holy mess after a while.

If your service moved, or you want to test, say, the staging or production service, the procedure differs between VS2005 and 2008:

  • VS2005:  Open the  .map file under the service reference. It looks something like this:
    map file
    Edit the ServiceReferenceUri only, and save the .map file, and Update Service Reference as before.  You will see the EndPoint Address change automatically as the service is contacted, a new proxy is generated and the .config file is updated.
  • VS2008:  Right-click the reference in Solution Explorer and choose Configure Service Reference.  You will see the Service Reference Settings dialog (see above). Change the Address value, click OK, and everything that needs to happen happens.

Remember again, to copy the config section into your executing assembly if you are operating in a class library.


Using svcutil.exe

You can do all the above and more using the command line tool, svcutil.exe.  In VS2005, this may be your best option if you want to establish service references in a web site project.

Note that the tool resides in different locations for the different VS versions:

  • VS2005: C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\
  • VS2008: C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin\

Open a command prompt, navigate to your project directory, and use this simple command line to generate a proxy and config:

"C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\svcutil.exe" http://localhost:4068/MyWCFService/Service.svc /out:MyWCFService.cs

This is the basic command line, you may want to investigate into other options for lots more control. For more information see

Configurations will be written into an output.config file. CAUTION: If you use the /config switch, do not directly reference your application configuration file, as it will be overwritten. If you had other config sections, you have just lost them. The VS IDE is much smarter about diffing the .config files first.

Notice that the namespace of the generated proxy class is based on the service's namespace if you don't specify it explicitly through the commandline.



Include the proxy class in your project (in a web project, in your app_code folder), and copy the config sections into the executing assembly config ... done!

Posted on Monday, February 4, 2008 7:12 PM BizTalk and SOA | Back to top

Comments on this post: Consuming a WCF service

# re: Consuming a WCF service
Requesting Gravatar...
I was adding a WCF service by using Add Service Reference. Unfortunately the signature of OperationContract is changed if the contract has an out parameter.

Here are two examples:

int ReturnWithInt(int para1, out string errorMessage)

The generated client proxy code signature becomes:

int ReturnWithInt(out string errorMessage,int para1)

it always move the out parameter to be the first parameter.

second example:

void ReturnWithVoid(int para1, out string errorMessage)

The generated client proxy code signature becomes:
string ReturnWithVoid(int para1)
basically it changes the out parameter to be the return value.

Do you know if there is way to ask it not doing that and keep the original signature?

thanks a lot


Left by Xiali Zheng on Feb 27, 2008 11:17 AM

# re: Consuming a WCF service
Requesting Gravatar...
Thanks - this post is really helpful!

Please change the section on Prerequisite Installs:

the VS2005 WCF extensions must be installed BEFORE .NET 3.0 SP1, otherwise the extension install will fail.
Left by Liatris on Mar 06, 2008 10:40 AM

# re: Consuming a WCF service
Requesting Gravatar...
Thanks so much for this feedback, Liatris! I've made this adjustment.
Left by maina on Mar 06, 2008 10:55 AM

# re: Consuming a WCF service
Requesting Gravatar...
Thanks so much it's very helpful!
Left by Jucv on Mar 18, 2008 10:54 AM

# re: Consuming a WCF service
Requesting Gravatar...
We have problems with refresh Service Reference. Whenever there are datacontract changes, we click refresh, it will generate another set of xsd/dicso and wsdl. fro an example, it first creates webServices.xsd,

After refresh it, it reomves above and creates another sets, such as webservices4.xsd
webservices6.xsd etc.

Are there any way to avoid it?

Left by Lisa on Apr 09, 2008 1:43 PM

# re: Consuming a WCF service
Requesting Gravatar...
Thank you. It helped me a lot and saved lot of time.

Thanks again................
Left by Hari on Apr 28, 2008 11:33 AM

# re: Consuming a WCF service
Requesting Gravatar...
Dear co developer

I work with vs2008 .Net3.0 C#
I work with WCF services, everything works great but there is one problem
The initialization of the WCF service takes too long. here you see an example how i initialize my wcf service

TestService testServiceClient = new TestService();

for comments or suggestions you may always reply to my email :

thx in advance


Left by G on Jun 19, 2008 7:12 AM

# re: Consuming a WCF service
Requesting Gravatar...
hi :)

i did everything by the book, and the same way mentioned here, but i keep getting the following error

"Could not find default endpoint element that references contract 'WCF_Service.WCF_Service_Namespace.IProductService' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element."

my client has the end point the same way mention here i have made sure that .exe.config contatins the config info..

what am i missing, please if u know any answer just reply here or to my email

Basem Sayej
Left by Basem Sayej on Aug 24, 2008 1:22 PM

# re: Consuming a WCF service
Requesting Gravatar...
hi i am also getting the same error please help me out ,

"Could not find default endpoint element that references contract 'WCF_Service.WCF_Service_Namespace.IProductService' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element."

i am giving reference to class library , instead of a web or windows application

please put ur comments to :
Left by nagendra on Oct 22, 2008 5:31 AM

# re: Consuming a WCF service
Requesting Gravatar...
Kind of curious that two people are trying to produce exactly the same service interface?

Lacking your specific configuration, it's hard to tell what's wrong. The error messages received from ServiceModel, however, are extremely descriptive. Read them carefully, and make sure that your namespaces, interfaces, ServiceReference names in config and code, match up; and that you have the Servicemodel config section in your EXECUTING assembly. I also recommend to clean up previous versions of the ServiceReference section (with different names), possibly generated by multiple runs of the Add Reference tool, and only leave the most current one in place.

Left by maina on Oct 22, 2008 9:01 AM

# re: Consuming a WCF service
Requesting Gravatar...
I just want to reply to main, why the last two posts are the same,
this is because this is an example in john sharp's WCF Step by Step book :)
Left by Basem Sayej on Nov 26, 2008 12:37 AM

# re: Consuming a WCF service
Requesting Gravatar...
@Endpoint Error-Last to Last Post.

If you have done it with svcutil.exe , then the Service Class file and output.config file will be generated in the svc folder.

I suggest that, better open ur web.config, delete the service model section and copy it from ouput.config file and then run your application, it might work.

Secondly, just refresh your browser, delete the cookies and all and then run the application.
Left by Himanshu Raj on Oct 03, 2009 6:12 AM

# Consuming a WCF service with Silverlight
Requesting Gravatar...
I am trying to use a wcf service in silverlight, the generation and addition of the service reffernce worked well,
however after I created the client proxy instance, I discovered that all the methods that I created in the service can not be accessed directly, meaning: If I had a method named somemethod I now see in intelisence a method called somemethodAsync and an event called somemethodCompleted.
Anyone knows why this happens and can I change it?

Thanks in advance
Left by Paul on Jan 03, 2010 12:14 PM

# re: Consuming a WCF service
Requesting Gravatar...

I also got same error which was posted by nagendra,and i found a solution to that error.

For client Application you need to add Service Referrence and add application config file and replace the <system.servicemodel> of app.config file with <system.servicemodel> of output.config which was generated by SVCUTIL.EXE.

I Think it will be helpful for you.
Left by Prasad Muthya on Jun 28, 2010 6:34 AM

# Consuming a WCF service
Requesting Gravatar...
I migrate my project vss2005 to vss2008.
then i added the web service reference(consume wcf ) in vss 2008 project.
Service added in project but i cant able to add namespace in web page.

please let me know if there is any solution.
Left by Santhosh on Jul 19, 2010 7:52 PM

# re: Consuming a WCF service
Requesting Gravatar...
Hi , I am getting following error ..Please suggest..

Unable to cast object of type 'ABC.ClassInput' to type 'ABC.ClassInput'.

Left by Rahul Sharma on Mar 31, 2011 7:17 AM

# re: Consuming a WCF service
Requesting Gravatar...
I consumed WCF service Using svcutil.exe
in your blog. Can you tell me how can i maintain the same later if the WCF gets changed?

Do i need to do the same process again? or is there any other way round?
Left by Fardin on Aug 10, 2011 11:19 PM

# re: Consuming a WCF service
Requesting Gravatar...
good article
Left by praveen on Aug 26, 2011 4:04 AM

# re: Consuming a WCF service
Requesting Gravatar...
Its really helpful to me..
Thank you very much
Left by leela on Sep 22, 2011 6:22 AM

# re: Consuming a WCF service
Requesting Gravatar...
THANK YOU!! This was great! Just what I needed!
Left by puzzle on Feb 06, 2012 9:03 PM

# re: Consuming a WCF service
Requesting Gravatar...
I host My WCF Service in IIS in Web Server

While I am Accessing it, It shows error XML Parser Error, While It is Working Correctly on Local Machine.
Left by Ankit on Jun 05, 2012 12:57 AM

# re: Consuming a WCF service
Requesting Gravatar...
Congratulations Admin! Thank you so much for taking the time to share this exciting information.
Read More
Left by on Mar 19, 2013 1:32 PM

Your comment:
 (will show your gravatar)

Copyright © Maina Donaldson | Powered by: