Translate

Sunday, September 9, 2012

Create WCF Service and Host in SharePoint

Create WCF Service and Host in SharePoint

Creating a custom SharePoint WCF Service

There are situations where SharePoint could be acting as a data store providing your business data
to external applications or any web service or any components. These applications requesting data may need to provide information  on having a valid subscription or license before accessing the data. The out-of-the-box web services will not provide you a way to verify this information before allowing access. There may also be a scenario where you may want to strip out confidential information from the list
before passing the data to these external applications. In these scenarios, you can create
your own custom web service which could validate the credentials, or strip out the confidential
data before passing the information to these external applications.


In this example, we will create a custom SharePoint WCF service which returns us the string.This service is 
being host in _layout\_vti_bin directory in internet manager IIS.

Steps to Create a WCF Service:


In order to create a custom SharePoint WCF service,follow the following steps:

1. Open Visual Studio 2010 and create an empty SharePoint project.


2. Name the project CustomWCF. Deploy the solution as the farm solution.
3. Add a SharePoint mapped folder for ISAPI. In this folder, create a folder
named CustomWCF.
4. Add an empty text file named CustomWCF.svc to the CustomWCF folder
underneath ISAPI.
5. Add a new item to the project and select the WCF Service template in the Visual
Studio:
6. This will add three files to your project namely: CustomWCF.cs, ICustomWCF.cs,
and app.config.
7. Move the app.config file from the root folder to the ISAPI\CustomWCF folder in
the project and rename it web.config.
8. Build your project.
9. Open the Visual Studio command prompt and navigate to the bin\debug
directory of your project and execute the following command to get the public
key token of the assembly:
Sn –T CustomWCF.dll
10. Add the following line to the CustomWCF.svc file and add the public key token
obtained from the previous step:
<%@ ServiceHost Debug="true" Language="C#" Service="CustomWCF.
CustomWCF, CustomWCF, Version=1.0.0.0, Culture=neutral, PublicKeyT
oken=2832c7898c525539" CodeBehind="CustomWCF.cs" %>
11. The default DoWork method the Visual Studio added to ICustomWCF.cs returns string
12. Make the same change in the CustomWCF.cs file as well. This is the file where
the interface is implemented. Add the following line of code as shown in the

sample code
public string GetData(int value)
        {
            return string.Format("You entered: {0}", value);
        }

13. Add the AspNetComaptibility attribute to the class that is implementing the
interface. So your CustomWCF.cs
class should look as follows:
using System.ServiceModel.Activation;
namespace CustomWCF
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
[AspNetCompatibilityRequirements(RequirementsMode =
AspNetCompatibilityRequirementsMode.Required)]
    public class CustomWCF : ICustomWCF
    {
        public string GetData(int value)
        {
            return string.Format("You entered: {0}", value);
        }

        public CompositeType GetDataUsingDataContract(CompositeType composite)
        {
            if (composite == null)
            {
                throw new ArgumentNullException("composite");
            }
            if (composite.BoolValue)
            {
                composite.StringValue += "Suffix";
            }
            return composite;
        }
    }
}
14. Build and deploy the application. You should now be able to access this WCF service
from the browser by typing in the URL: http://servername/_vti_bin/
CustomWCF/CustomWCF.svc.

15. Screenshot similar to the following







No comments:

Post a Comment