I've been working a lot with SharePoint lately and as with any project, we add it to our Cruise Control.NET server. The first challenge was that we had to install SharePoint on the CC.NET server. It was not really a challenge, but more of a time-consuming task.
The second challenge was setting up automated unit tests. In our case we use WCF services that receive an SPToken (actually, the string representation of the SPToken). This token expires every now and then, and we had to come up with a way to get a token in order to connect to a WCF service. The solution was creating a method in a service that creates a token and sends it back. It's really not the best practice, and it's not something you want to deploy to production, but it works fine for a continuous integration environment. These are the steps we followed:
1) Create a new service that is used only in the CC.NET sandbox
2) Create a method in that service that returns a token. The method should run with elevated privileges and return a token of a hardcoded user account (preferably an administrator account)
3) Reference this service from the project that contains the unit tests (we use XUnit)
4) Instantiate a service client from the test case (something like this)
XUnitServiceClient client = new XUnitServiceClient("BasicHttpBinding_IXUnitService");
client.Endpoint.Address = new System.ServiceModel.EndpointAddress("http://mypc/_vti_bin/XUnitService.svc/soap");
string result = client.GetToken();
5) Then just call the method you're trying to test and pass the token.
Showing posts with label SharePoint. Show all posts
Showing posts with label SharePoint. Show all posts
Monday, July 30, 2012
Monday, July 23, 2012
SharePoint and WCF
I recently upgraded an ASMX project to WCF. It took me a couple of days (investigating, implementing and testing) to figure out, and although I'm sure my solution can still be polished, here are the steps so far to make it work:
1) Copy your .svc file to ISAPI
2) Make sure the markup of the .svc files is as follows
<%@ ServiceHost Language="C#" Debug="true" Service="MyApp.Service.TestService, MyApp.Service , Version=1.0.0.0, Culture=neutral, PublicKeyToken=3333333333" %>
3) We are accessing the WCF services using jQuery, so we had to enable the webHttpBinding. Security needs to be enabled like this:
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Ntlm" />
</security>
4) Create an endpoint behavior that includes
<webHttp />
5) Allow Anonymous Authentication on your IIS site.
6) Make sure you use the DataContract and DataMember directives where appropriate. You might get hard-to-debug errors when a type cannot be serialized correctly.
Along the way I learned a lot about Ntlm. At first I was getting a lot of "Unauthorized Access" responses, and I thought maybe using Ntlm was the problem. The real problem was that my WCF configuration was incorrect. Also, Ntlm is a protocol that uses a handshake, so you'll notice the same request three times at the beginning of the communication. Any subsequent request should only appear once.
1) Copy your .svc file to ISAPI
2) Make sure the markup of the .svc files is as follows
<%@ ServiceHost Language="C#" Debug="true" Service="MyApp.Service.TestService, MyApp.Service , Version=1.0.0.0, Culture=neutral, PublicKeyToken=3333333333" %>
3) We are accessing the WCF services using jQuery, so we had to enable the webHttpBinding. Security needs to be enabled like this:
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Ntlm" />
</security>
4) Create an endpoint behavior that includes
<webHttp />
5) Allow Anonymous Authentication on your IIS site.
6) Make sure you use the DataContract and DataMember directives where appropriate. You might get hard-to-debug errors when a type cannot be serialized correctly.
Along the way I learned a lot about Ntlm. At first I was getting a lot of "Unauthorized Access" responses, and I thought maybe using Ntlm was the problem. The real problem was that my WCF configuration was incorrect. Also, Ntlm is a protocol that uses a handshake, so you'll notice the same request three times at the beginning of the communication. Any subsequent request should only appear once.
Subscribe to:
Posts (Atom)