您的位置:首页 > 其它

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

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!!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐