|
|||||||||||||||||||||||||||||||||||||||||||||||||
|
This Month
Month Archive
Year Archive
Login
|
Thursday, July 3
by
purushotam
on Thu 03 Jul 2008 23:18 BST
Please don't be surprised that I have postings for all categories but I am really interested in WCF, cause that I love Remoting and Web Services since they are born!. Hence I try not to miss this zone on my regular postings. Here I would like to cover WCF's basics and Security.
Wednesday, June 11
by
purushotam
on Wed 11 Jun 2008 21:43 BST
In this posting, you will learn how to implement a WCF service that uses the application pool identity to connect to Microsoft Office SharePoint Server 2007.
The example in this section is created using Visual Studio 2008. In the example we will discuss in this section, we will create a WCF service that is hosted by Internet Information Server. Then, we will specify an application pool identity and use that in our impersonation scenario to run some code the end user is normally not allowed to execute. In the next procedure, you will see how to create a new virtual directory for a WCF service. In the next procedure, you will see how to create a new virtual directory for a WCF service. 1. Create a new folder at the following location: C:\Inetpub\wwwroot\TestWCFHost. 2. Open a command prompt and type inetmgr. This opens Internet Information Services (IIS) Manager. 3. Expand the [server name] (local computer) node. 4. Expand the Web Sites node. 5. Locate a SharePoint web site where you want to add a virtual directory for your custom WCF service, right-click it, and choose New äVirtual Directory. This opens the Virtual Directory Creation Wizard. 6. Click Next. 7. Enter the following alias: TestWCFHost. 8. Click Next. 9. Enter the following path: C:\Inetpub\wwwroot\TestWCFHost. 10. Click Next. 11. Accept the default Virtual Directory Access Permissions and click Next. 12. Click Finish. 13. Right-click the newly created TestWCFHost node and choose Properties. This opens the TestWCFHost Properties window. 14. Click Create. 15. Click OK. We will create a custom application pool for this virtual directory, specify an application pool identity under which the server will run, and associate them with each other. This is explained in the next procedure. 1. In Internet Information Services (IIS) Manager, right-click the Application Pools node and choose New Application Pool. This opens the Add New Application Pool dialog window. 2. Specify the following Application pool ID: WCFAppPool. 3. Click OK. 4. Right-click the newly created WCFAppPool node and choose Properties. This opens the WCFAppPool Properties window. 5. Click the Identity tab. 6. Select the Configurable radio button. 7. Specify a user name and password. 8. Click OK. 9. Right-click the previously created TestWCFHost virtual directory (located under the Web Site node) and choose Properties. This opens the TestWCFHost Properties window. 10. In the Application pool drop-down list, choose WCFAppPool. 11. Click OK. At this point, you have created the container that will hold the WCF service. Now, we will use Visual Studio 2008 to create the WCF service. This is discussed in the following procedure: 1. Start Visual Studio 2008 2. Click File New Web Site. This opens the New Web Site dialog window. 3. In the Templates section, choose WCF service. 4. Select HTTP from the Location drop-down list. 5. In the Location text box, type the URL of the virtual directory you created earlier in this section. In our case, this is http://[sharepoint web application]/TestWCFHost. As a result, several default files are created automatically. Delete the following files: Service.svc, App_Code\Service.cs, and App_Code\IService.cs. You will notice that creating a WCF service is quite similar to creating a classic .asmx web service. The following procedure discusses creating a WCF service: 1. Right-click your project and choose Add New Item. This opens the Add New Item – [URL WCF service host] dialog window. 2. In the Templates section, choose WCF Service. 3. Enter the following name: MyWCFService.svc. 4. Ensure that the selected Language drop-down list is set to Visual C#. 5. Click Add. This creates three files: MyWCFService.svc (located in the root of the project), and IMyWCFService.cs and MyWCFService.cs (both located in the App_Code folder). The MyWCFService.svc file contains of a single line of code that refers to a code-behind file. and here is the file.. <%@ ServiceHost Language=”C#” Debug=”true” Service=”KwikSilver.MyWCFService” CodeBehind=”~/App_Code/MyWCFService.cs” %> Next, we will take a closer look at the IMyWCFService class file. In WCF, all services expose contracts that explicitly define what a service does. There are four different types of contracts: • Service contracts that describe the operations that can be performed by the client. • Data contracts that describe the data that is passed to and from the service. • Fault contracts that describe errors that are raised by the service. • Message contracts that describe how a service can interact directly with a message. In this example, only service contracts are relevant. You can create a service contract by adding a .cs file that defines a new interface. Then, you need to decorate the interface with a [ServiceContract] attribute that exposes a normal CLR interface as a WCF contract. Every method within such an interface that needs to be exposed in the WCF contract needs to be decorated with the [OperationContract] attribute. code below shows the implementation for the IMyWCFService WCF service contract: using System; using System.Collections.Generic; using System.Runtime.Serialization; using System.ServiceModel; using System.Text; namespace KwikSilver { [ServiceContract] public interface IMyWCFService { [OperationContract] string DoWork(); } } This service contract needs to be implemented by a class. To demonstrate that impersonation has worked, we will use a piece of code that fails if the identity executing the code is not an administrator. The code retrieves the current SharePoint site and shows the collection of site groups of which the current user is a member: string strValue = String.Empty; RevertToSelf(); SPWeb objCurrentWeb = SPControl.GetContextWeb(Context); foreach (SPRole objRole in objCurrentWeb.CurrentUser.Roles) { strValue += “Role: “ + objRole.Name + “<br/>”; } This code fails unless you are an administrator. If we modify the code a bit so you can pass the URL of the site collection and the user name as arguments, the code looks like this: public string GetUserRoles(string strUrl, string strUserName) { string strResult = String.Empty; using (SPSite objSiteCollection = new SPSite(strUrl)) { using (SPWeb objSite = objSiteCollection.OpenWeb()) { SPUser objUser = objSite.Users[strUserName]; foreach (SPRole objRole in objUser.Roles) { strResult += objRole.Name + “, “; } } } return strResult; } and finally here is the complete code.. using System; using System.Collections.Generic; using System.Runtime.Serialization; using System.ServiceModel; using System.Text; using System.Security.Principal; using Microsoft.SharePoint; namespace KwikSilver { public class MyWCFService : IMyWCFService { public string DoWork() { // e.g. SHAREPOINT\system as the user name. return GetUserRoles(“[site collection URL]”, @”[user name] “); } public string GetUserRoles(string strUrl, string strUserName) { try { string strResult = String.Empty; using (SPSite objSite = new SPSite(strUrl)) { using (SPWeb objWeb = objSite.OpenWeb()) { SPUser objUser = objWeb.Users[strUserName]; foreach (SPRole objRole in objUser.Roles) { strResult += objRole.Name + “, “; } } } return strResult; } catch (Exception err) { return err.Message; } } } } Before you can use the WCF service, you need to adjust the web.config file to define endpoints that can be accessed by a client. This is done by adding a new <service> element to the <services> node of the web.config file. The <service> element allows you to define the service name via the name property. You can also add an <endpoint> element that defines a service contract that is used for the WCF service (service contracts were discussed earlier in this chapter) and a binding that is used by the service. Bindings define aspects about the transport mechanism, such as the protocol or the message encoding type that are used. In this example, we will use WsHttpBinding, which uses HTTP as the transport protocol. The following code fragment shows how to define a new service endpoint that uses the IWcfImpersonator service contract and uses HTTP as the transport protocol: <service name=”KwikSilver.MyWCFService”> <endpoint address=”” binding=”wsHttpBinding” contract=”KwikSilver.IMyWCFService” /> </service> You will also need to make sure that the WCF service emits metadata that is used by the client to retrieve a service description. You can do this by defining a service behavior for the service. Service behaviors affect all endpoints of a service, and you can specify which service behavior applies to a service by using the <service> element’s behaviorConfiguration attribute. This is shown in the following code fragment: <service name=”KwikSilver.MyWCFService” behaviorConfiguration=”MyServiceTypeBehaviors”> The following service behavior needs to be added to the <serviceBehaviors> section of the web.config file, and specifies that any service associated to this behavior exposes metadata: <behavior name=”MyServiceTypeBehaviors” > <serviceMetadata httpGetEnabled=”true” /> </behavior> If you open a browser and browse to the svc file (http://[sharepoint web application]/TestWCFService/MyWCFService.svc), you should see a page similar to figure.. ![]() The next procedure explains how to create a test client for this WCF service. 1. Right-click the solution and choose Add New Project. This opens the Add New Project dialog window. 2. In the Templates section, choose Windows Forms Application. 3. Enter the following Name: WCFTestClient. 4. Click OK. 5. Right-click the WCFTestClient project and choose Add Service Reference. The Add Service Reference dialog window appears. 6. In the Service URI text box, enter the URL of the MyWCFService.svc file. This is shown in figure.. ![]() 7. In the Service reference name text box, enter a friendly name that represents the service. In this case, I have specified a value of MyWCFServiceReference. 8. Click OK. 9. Open the code view window of the Windows form and add the code above to the Form load event: private void Form1_Load(object sender, EventArgs e) { MyWCFServiceReference.MyWCFServiceClient objService = å new MyWCFServiceReference.MyWCFServiceClient(); string strResult = objService.DoWork(); objService.Close(); } You have now created a test client for your WCF service. Try it out and if everything works fine, you can add a service reference to this WCF service within web parts or SharePoint pages. In this section, you learned how to create a WCF service that is hosted in IIS and runs under a custom application pool identity.WCF services can be used to execute code under this custom application pool identity that a normal end user would not be allowed to run. |
WELCOME!
Hey there!, I am Purushotam R. Tumkur, an IT Engineer by profession working on Consulting, Development arena specially under Microsoft Technologies. The motivation to bring this site upfront is the reason I want to share my views, thoughts, capabilities and personalities through the channel of the world and technology has exposed to us as "boon". I limit my self through blogging by writing professional and personal thoughts. I am sure you will appreciate me in this matter, hope you will enjoy ride.
Recent Reviews..
My favourites (opens new window)
|
|||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||

