Wednesday, September 25, 2013

Consuming WCF service from sharepoint web part or project

Consuming WCF service from sharepoint web part or project

Make sure that web part project/ dot net project has references to the following assemblies.

Import the following namespace in the code-behind of the web part/web page.
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 use the command line utility svcutil.exe.

Follow the below steps to create service reference using svcutil.exe.

Run command prompt with administrator privilege or take visual studio command prompt

Step 3.1:
Start > run > cmd.exe

Step 3.2: Deploy the service using basic http binding /net tcp binding and get the url of the service deployed

For example my service url is :
It nettcpbindng is used it will be:

Step 3.3: Change directory to the path of svcutil.exe

>cd C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin


cd C:\Program Files\Microsoft SDKs\Windows\v7.0A\bin

Step 3.4: Run the below command to create service reference class and configuration file for the client sharepoint project ASCO.SP(Namespace is ASCO.SP)

>svcutil /config:output.config /out:MasterDataService.cs /tcv:Version35 /n:*, ASCO.SP http://dt3138aaashjuae/asco/ASCO.ServiceLayer.MasterDataService.svc

If service is using nettcpbinding the above command should be:

> svcutil /config:output.config /out:MasterDataService.cs /tcv:Version35 /n:*, ASCO.SP net.tcp://dt3138aaashjuae/ASCO.Services/ASCO.ServiceLayer.MasterDataService.svc/mex

/config – specifies the name of the client configuration file to be generated
/out – 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.

Text in the above command window are:
Setting environment for using Microsoft Visual Studio 2010 x86 tools.
C:\Program Files\Microsoft Visual Studio 10.0\VC>
C:\Program Files\Microsoft Visual Studio 10.0\VC>svcutil /config:output.config
/out:MasterDataService.cs /tcv:Version35 /n:*,ASCO.Service http://dt3138aaashjuae/asco/ASCO.ServiceLayer.MasterDataService.svc
Microsoft (R) Service Model Metadata Tool
[Microsoft (R) Windows (R) Communication Foundation, Version 4.0.30319.1]
Copyright (c) Microsoft Corporation. All rights reserved.
Attempting to download metadata from 'http://dt3138aaashjuae/asco/ASCO.ServiceLayer.MasterDataService.svc' using WS-Metadata Exchange or DISCO.
Generating files...
C:\Program Files\Microsoft Visual Studio 10.0\VC\MasterDataService.cs
C:\Program Files\Microsoft Visual Studio 10.0\VC\output.config
C:\Program Files\Microsoft Visual Studio 10.0\VC>

Now Svcutil had generated two files in the location ‘C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin’
1. output.config
2. MasterDataService.cs

Step 4
Copy the content of section ‘system.serviceModel’ from the configuration to the clients web.config section ‘system.serviceModel’. Generated configuration file with ‘nettcpbinding’ looks like as below:

maxBytesPerRead="4096"maxNameTableCharCount="16384" />
enabled="false" />
<transportclientCredentialType="Windows"protectionLevel="EncryptAndSign" />
<messageclientCredentialType="Windows" />
<dnsvalue="localhost" />

For Sharepoint site ASCO.SP the command should be:
svcutil /config:output.config /out:MasterDataService.cs /tcv:Version35 /n:*,ASCO.SP net.tcp://dt3138aaashjuae/ASCO.Services/ASCO.ServiceLayer.MasterDataService.svc/mex

For .Net Project ASCO.DotNet the command should be:
svcutil /config:output.config /out:MasterDataService.cs /tcv:Version35 /n:*,ASCO.DotNet net.tcp://dt3138aaashjuae/ASCO.Services/ASCO.ServiceLayer.MasterDataService.svc/mex

Step 5
Copy and paste the proxy class MasterDataService.cs from the path ‘C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin’ to the client project (Sharepoint/Asp.Net)which consume the wcf service

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.

For Example: Consuming service method to bind a grid view

protected void grdMaster_NeedDataSource(object sender,Telerik.Web.UI.GridNeedDataSourceEventArgs e)
int pageSize =grdMaster.PageSize;
int pageIndex =grdMaster.CurrentPageIndex;

MasterDataServiceClient service = new MasterDataServiceClient();

long? status = 0;
long? errno = 0;
string errmsg = string.Empty;

PREAMBLE_MASTER[] preambles = service.GetPreambleMasterData(null, null, ref status, ref errno, ref errmsg);

this.grdMaster.DataSource = preambles.Skip(pageIndex * pageSize).Take(pageSize);
this.grdMaster.VirtualItemCount = preambles.Count();