Consuming a WCF Service from SharePoint 2010 web part

In this article I’d cover the steps to be followed to consume a IIS 7.5 hosted WCF service from SharePoint 2010 visual web part.


Make sure that web part project has references to the following assemblies




 Import the following namespace in the code-behind of the web part
using System.ServiceModel;
using System.Runtime.Serialization;
The next step is to generate the client proxy for the WCF service. You can use ‘Add Service Reference’ option in Visual Studio 2008/10 or leverage 
the command line utility svcutil.exe. Personally I’d prefer the svcutil.exe tool for many reasons.

svcutil /config:output.config   /out:CreditCardService.cs /tcv:Version35 /n:*,SandboxVisualWP.VisualWebPart1 http://localhost:1414/CreditCardService/ValidationService.svc

/config – specifies the name of the client configuration file to be generated

/output – specifies the name of the client proxy file (.cs file)

/tcv – specifies the .net framework version for the client proxy

/n:* – specifies the namespace for the client proxy. This is a significant factor that needs to be considered. Please make sure that the namespace of the proxy file matches with the namespace of visual web part project.


The generated client configuration file looks like the following

<?xml version="1.0" encoding="utf-8"?>
                <binding name="WSHttpBinding_IVadlidationService" closeTimeout="00:01:00"
                    openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                    bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
                    maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                    messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    <reliableSession ordered="true" inactivityTimeout="00:10:00"
                        enabled="false" />
                    <security mode="Message">
                        <transport clientCredentialType="Windows" proxyCredentialType="None"
                            realm="" />
                        <message clientCredentialType="Windows" negotiateServiceCredential="true"
                            algorithmSuite="Default" />
            <endpoint address=""
                binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IVadlidationService"
                    <dns value="localhost" />

If the web part is of type farm-solution copy the contents of this configuration file and place it inside the System.ServiceModel section of the web.config file


My generated proxy file looks like the following. If you are using visual studio 2010 to create a proxy, it will always create a client proxy .cs file targetted to .NET Framework 4.0


// <auto-generated>

//     This code was generated by a tool.

//     Runtime Version:4.0.30319.269


//     Changes to this file may cause incorrect behavior and will be lost if

//     the code is regenerated.

// </auto-generated>


namespace SandboxVisualWP.VisualWebPart1


    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "")]


    public interface IVadlidationService



        string GetCountry(int value);
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "")]
    public interface IVadlidationServiceChannel : SandboxVisualWP.VisualWebPart1.IVadlidationService, System.ServiceModel.IClientChannel
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "")]
    public partial class VadlidationServiceClient : System.ServiceModel.ClientBase<SandboxVisualWP.VisualWebPart1.IVadlidationService>, 
        public VadlidationServiceClient()
        public VadlidationServiceClient(string endpointConfigurationName) : 
        public VadlidationServiceClient(string endpointConfigurationName, string remoteAddress) : 
                base(endpointConfigurationName, remoteAddress)
        public VadlidationServiceClient(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) : 
                base(endpointConfigurationName, remoteAddress)
        public VadlidationServiceClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) : 
                base(binding, remoteAddress)
        public string GetCountry(int value)
            return base.Channel.GetCountry(value);
6. Now add the proxy class ‘CreditCardService.cs’  to the visual web part project. 
If you try to access the client proxy on the code-behind of the Visual Web Part, the intellisense will show that class now.

For the intellisense to recognize this proxy class, the namespace of the generated proxy class has to match with the namespace of the visual web part project. If there is a mismatch in the namespace, the intellisense will not recognize this proxy class. It is always recommended to explicitly specify the namespace during the generation of client proxy using svcutil (mentioned in step 3).

Now you are all set to invoke the various service operations on the WCF service.

 Subscribe to my blog

2 thoughts on “Consuming a WCF Service from SharePoint 2010 web part

Leave a Reply