Monday 10 December 2012

How To Create a WCF web service in a SharePoint site and project.

 

 

1 create a new sharepoint project

clip_image001

2. deploy as a farm solution

3. Right Click on the project, and click on add

then in the add list at the bottom is “add a mapped sharepoint folder”

Map ISAP for authenticated anon svc for anonymous services (anonsvc is underneath ISAPI) (I map ISAPI and I’ll use windows authentication)

clip_image003

4. After adding it then Add a folder to ISAPI (name it you wish)

5. Add a new xml file to that folder, then rename it to svc then add another new item, it is an application configuration file called web.config

clip_image004

6. Create 2 new files, one for the interface and the other one for the implementation and add the System.ServiceModel.dll to the References from the GAC

I created the following interface:


using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
namespace WcfTest
{
[ServiceContract]
public interface IService
{
[OperationContract]
int Add(int x, int y);
}
}


The implementation, you have to add the following to the class: [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] like


using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel.Activation;
using System.Text;
namespace WcfTest
{
[AspNetCompatibilityRequirements(RequirementsMode =
AspNetCompatibilityRequirementsMode.Allowed)]
public class Service:IService
{
public int Add(int x, int y)
{
return x + y;
}
}
}


7. Build the project and open the Visual Studio Command Line and type:

Sn –T <pathtoyourdll>\Solutionname.dll 


In my case it is:  

E:\>SN -T e:\Temp\WcfTest\WcfTest\bin\Debug\WcfTest.dll

Microsoft (R) .NET Framework Strong Name Utility  Version 4.0.30319.1

Copyright (c) Microsoft Corporation.  All rights reserved.

Public key token is 9e39caaf61698cf3

E:\>

8. modify the SVC file created some steps above like this:

(The service is namespace.class,namespace )


<%@ ServiceHost Debug="True" Language="C#" CodeBehind="Service.cs"


Service="SharePointWCF.Service,SharePointWCF,Version=1.0.0.0,Culture=neutral,


    PublicKeyToken=9e39caaf61698cf3" %>


The green ones have to be modified based on your names and namespaces. After the services attribute the value is the fully qualified name of the service implementation and the name of the dll.

9. Open the tool from the tools menu called WCF Service Configuration Editor when it open close it down J and right click on the web.config under the ISAPI folder you have created and select the Edit WCF Configuration, if you have not opened the WCF Configuration Editor you would not have that menu here/

10. Click on create new service then browse for the dll of the project in the /bin/debug folder. Click on Next button until you reach the What is the address of your endpoint question, type an correct address, can be anything http://google.com or something like this, you will remove it anyway, but the wizard requires. When the wizard finished click on CTRL+S and close down the Wcf Configuration Editor

clip_image006

11. In your web.config correct / add the following things: (MEX cannot be exposed when NTLM authentication is used)


<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled
="true"/>
<diagnostics>
<messageLogging logMalformedMessages="true"
logMessagesAtTransportLevel="true" />
</diagnostics>
<bindings>
<basicHttpBinding>
<binding
name="BasicHttpBinding_IService">
<security
mode="TransportCredentialOnly" >

<transport clientCredentialType="Ntlm" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior
name="Service.CustomBehavior">
<serviceMetadata
httpGetEnabled="true" />
<serviceDebug />

</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="WcfTest.Service"
behaviorConfiguration="Service.CustomBehavior">
<endpoint address=""
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IService"
bindingNamespace="http://fakeService.eib.org"
contract="WcfTest.IService">
<identity>
<dns
value="localhost" />
</identity>
</endpoint>
<!--<endpoint address="mex"
binding="mexHttpBinding" contract="IMetadataExchange" />-->
</service>
</services>
</system.serviceModel>
</configuration>


DONE!





No comments:

Post a Comment