Sunday, December 29, 2013

Func vs Action vs Predicate in C#

Often users find certain aspects of functional programming in C# confusing and this is one of them.

To simply put Action<T> and Predicate<T> are special cases of Func<T,TResult>. And all of these are delegate types provided by C# so that you do not need to define your own delegate type.

Action<T>
When you need a delegate that takes in a parameter (say an int) and returns nothing (void) then you have,

Action<int> which is equivalent to Func<int,void>

(Note that Func<int,void> is not something valid and wont work, this is just to draw a parallel between Action and Func. )

Predicate<T>
When you need a delegate that takes in a parameter (say an int ) and returns a bool then you have,

Predicate<int> which is equivalent to Func<int,bool>

Typically Linq uses Func and List<T> uses Action and Predicate.

Tuesday, December 17, 2013

An unsecured or incorrectly fault error WCF

I encountered this error while executing the following snippet,

factory.CreateChannel().Issue(rst)       // Here the rst is a RequestSecurityToken object and 
                                                          //  factory is a WSTrustChannelFactory object

It seems that this was due to incorrect clock skew between my machine (client) and the server (where the WCF service was running). After correcting the time of my machine the error went away.

Friday, November 15, 2013

IIS error 405 method not allowed

I was developing some services using Asp.net Web API and it was working fine on the visual studio development server, but as soon as I hosted it on IIS (7.5) I was getting this error. The HTTP verb for the service  was PUT. GET and POST requests were working fine. The error was occurring due to WebDAV which does not allow the  PUT verb. So a simple solution is to remove the WebDAV module from your web project.

1. Go to your web.config and add the following under modules in system.webserver,

 <remove name="WebDAVModule"/>

2. After adding this my web.config looked like this (This is only a part of it),

<system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <modules runAllManagedModulesForAllRequests="true">
      <remove name="WebDAVModule"/>
    </modules>