Create and Consume WCF in Sharepoint
2010-05-07 11:08
405 查看
In this case, you needn't create a seperate Web site for the WCF services, the services will share the same web and port number with existing sharepoint site. Because it is a farm solution, once it is deployed, it will be shared in the whole farm.
Here are the steps,
1. Create a new Sharepoint Empty project "SPFirstWCF".
2. Add Reference.
a. System.ServiceModel
this one is the core assembly for WCF
b. Microsoft.SharePoint.Client.ServerRuntime
This contain the attribute [BasicHttpBindingServiceMetadataExchangeEndpointAttribute] which allows SharePoint to automatically support metadata exchange endpoints, and is the secret sauce that allows us to develop custom WCF services without having to deploy endpoint configuration to the SharePoint web.config.
This assembly is locating at %Windows%/assembly/GAC_MSIL/Microsoft.SharePoint.Client.ServerRuntime
3. Add a SharePoint Mapped Folder to the ISAPI folder, and create an empty text file with the .SVC extension. Note that it is always a good practice to create a sub-folder when deploying your custom code to the SharePoint file system. It helps keep you custom code separate from the original installed files.
4. Create WCF service Interface
5. Create WCF service class
6. The assembly will go to GAC after the deployment. You can check its public token and version number
7. Based on above information, modify the svc file
8. Deploy the solution to the sharepoint farm. you can use this link to check whether it is working
http://localhost:26292/_vti_bin/GSWCF/gswcf.svc/mex the mata data info should be returned.
9. Create a test console project and add the service reference by using that service URL.
Here is the code,
ALL done!!
Here are the steps,
1. Create a new Sharepoint Empty project "SPFirstWCF".
2. Add Reference.
a. System.ServiceModel
this one is the core assembly for WCF
b. Microsoft.SharePoint.Client.ServerRuntime
This contain the attribute [BasicHttpBindingServiceMetadataExchangeEndpointAttribute] which allows SharePoint to automatically support metadata exchange endpoints, and is the secret sauce that allows us to develop custom WCF services without having to deploy endpoint configuration to the SharePoint web.config.
This assembly is locating at %Windows%/assembly/GAC_MSIL/Microsoft.SharePoint.Client.ServerRuntime
3. Add a SharePoint Mapped Folder to the ISAPI folder, and create an empty text file with the .SVC extension. Note that it is always a good practice to create a sub-folder when deploying your custom code to the SharePoint file system. It helps keep you custom code separate from the original installed files.
4. Create WCF service Interface
namespace SPFirstWCF { [ServiceContract] public interface ISampleService { [OperationContract] string GetCurrentUrl(); } }
5. Create WCF service class
using Microsoft.SharePoint; using System.ServiceModel.Activation; using Microsoft.SharePoint.Client.Services; namespace SPFirstWCF { [BasicHttpBindingServiceMetadataExchangeEndpointAttribute] [AspNetCompatibilityRequirementsAttribute(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)] public class SampleService:ISampleService { #region ISampleService Members public string GetCurrentUrl() { return SPContext.Current.Web.Url; } #endregion } }
6. The assembly will go to GAC after the deployment. You can check its public token and version number
7. Based on above information, modify the svc file
<%@ServiceHost Language="C#" Factory="Microsoft.SharePoint.Client.Services.MultipleBaseAddressBasicHttpBindingServiceHostFactory,Microsoft.SharePoint.Client.ServerRuntime, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" Service="SPFirstWCF.SampleService, SPFirstWCF, Version=1.0.0.0, Culture=neutral, PublicKeyToken=77a6dd47bc70f02b" %>
8. Deploy the solution to the sharepoint farm. you can use this link to check whether it is working
http://localhost:26292/_vti_bin/GSWCF/gswcf.svc/mex the mata data info should be returned.
9. Create a test console project and add the service reference by using that service URL.
Here is the code,
MyServices.SampleServiceClient client = new MyServices.SampleServiceClient(); //you must provide this, or else, you don't have permission to call the service. client.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation; Console.WriteLine(client.GetCurrentUrl()); Console.Read();
ALL done!!
相关文章推荐
- How to create your own custom 404 error page and handle redirect in SharePoint 2007 (MOSS)?
- How to create your own custom 404 error page and handle redirect in SharePoint
- 记录一个:Create, Update, Delete and Copy Item Activity in SharePoint 2010 Workflow
- 转:How to create your own custom 404 error page and handle redirect in SharePoint 2007 (MOSS)?
- Create and Call HttpHandler in SharePoint
- Create and Call HttpHandler in SharePoint
- Create custom Task List and Forms in SharePoint 2010 with Visual Studio 2012
- How to:Create, Configure, Consume SharePoint 2010 Secure Store in Business Connectivity Services
- Filter the task and subtask in sharepoint 2013
- Create and Install Temporary Certificates in WCF for Message Security During Development
- How to create custom navigation menu in SharePoint with XML data source 使用XML数据源在SharePoint创建自定义导航菜单
- How to create a Gantt chart in SharePoint
- How to create and delete a SharePoint site by web application
- Adding and Deploying Solutions with PowerShell in SharePoint 2010
- SPGridView and Pagination in SharePoint SharePoint中的SPGridView和分页功能
- Hosting WCF in SharePoint 2007 (Part 1) 基本部署(转)
- Calling a WCF Service using jQuery in SharePoint (Anweshi Deverasetty)
- Web.Config - create WCF REST Service and host in Windows Azure
- how to install and configure Remote BLOB Storage (RBS) in a SharePoint 2013 farm
- SharePoint 2007 Deployment: List Instance Features(http://blogit.create.pt/blogs/andrevala/archive/2008/06/17/SharePoint-2007-De