Showing posts with label WCF. Show all posts
Showing posts with label WCF. Show all posts

Saturday, May 31, 2014

Logging in WCF

Have you ever wondered how to log messages sent to a WCF service? I had an iPhone app not being able to connect to a method in a WCF service, so I did this to enable logging of requests:

  <diagnostics>
    <messageLogging
         logEntireMessage="true"
         logMalformedMessages="true"
         logMessagesAtServiceLevel="true"
         logMessagesAtTransportLevel="false"
         maxMessagesToLog="3000"
         maxSizeOfMessageToLog="2000"/>
  </diagnostics>
  </system.serviceModel>
<system.diagnostics>
  <sources>
      <source name="System.ServiceModel.MessageLogging">
        <listeners>
                 <add name="messages"
                 type="System.Diagnostics.XmlWriterTraceListener"
                 initializeData="c:\logs\messages.svclog" />
          </listeners>
      </source>
    </sources>

</system.diagnostics>

Then once you have a log file, open it with SvcTraceViewer.exe, the Service Trace Viewer tool from Microsoft.

Monday, April 14, 2014

Calling JSON WCF Services from .NET 3.5

The short answer is that you need to use the WebClient class. For the lengthy answer, check out this link:

http://www.c-sharpcorner.com/uploadfile/dhananjaycoder/wcf-rest-service-with-josn-data/

Monday, July 30, 2012

Continuous integration with SharePoint (and automated unit tests)

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.

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.

Friday, January 14, 2011

Using Json with AJAX

I came across this interesting article on how to use Json with WCF. Specifically, the "d" parameter stands out. This feature is specific to WCF/webHttpBinding, because I've consumed Json services before and this parameter was not available. For more information check out the article.

Saturday, July 24, 2010

Handling SOAP fault exceptions from a WCF service

I was expecting that using catch (Exception ex) would catch the fault (it actually does) and it would be able to retrieve the fault information (not the case). This article showed me how to do it: use catch(FaultException ex).

Actually, I added catch clauses for both the generic exception and the fault exception. Don't forget the generic exception goes after the fault exception, otherwise the fault exception will never get caught.