Friday, 27 July 2012

WCF Binding Configuration for Maximum Data Size

Below is the WCF binding configuration to allow maximum data size. Please bear in mind not to put these settings on public services as these settings will make your application vulnerable to security attacks.
<bindings>
  <wsHttpBinding>
    <binding name="myBindingName" maxReceivedMessageSize="2147483647" 
        closeTimeout="00:10:00" openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00" >
      <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" 
          maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
      . . .
    </binding>
  </wsHttpBinding>
</bindings>
maxReceivedMessageSize, maxDepth, maxStringContentLength, maxArrayLength, maxBytesPerRead, maxNameTableCharCount attributes accept integer values so we can put the maximum integer value which is 2147483647.

According to MSDN documentation, increasing this value alone is not enough in ASP.NET compatible mode. The maxRequestLength attribute value of httpRuntime needs to be increased as well. Its default value is 4096 KB. The maximum value for .NET Framework 2.0 or above is 2GB (2097151).
<system.web>
  <httpRuntime maxRequestLength="2097151" />
  . . .
</system.web>

Note that I also increase the timeout attributes to 10 minutes.


For more information:
webHttpBinding on MSDN
readerQuotas on MSDN

Tuesday, 24 July 2012

How to Use Assert.IsAssignableFrom in xUnit

Assert.IsAssignableFrom<{type}>({object}) method means whether {type} is assignable from the type of {object} which also means whether {object} has a type similar to or is derived from {type}. While Assert.IsType method is used to test an object against a particular type, Assert.IsAssignableFrom offers more flexibility.

Let us look at some examples of this. Say that we have these classes:
public class BaseClass{}
public class TestClass : BaseClass{}
public class DerivedClass : TestClass{}

Then when we write these unit tests below. The results are commented out on each test.
public class Test
{
    private TestClass testClass;

    public Test()
    {
        testClass = new TestClass();
    }

    [Fact]
    public void test1() //Pass
    {
        Assert.IsType<TestClass>(testClass);
    }

    [Fact]
    public void test2() //Pass
    {
        Assert.IsAssignableFrom<TestClass>(testClass);
    }

    [Fact]
    public void test3() //Pass
    {
        var result = Assert.IsAssignableFrom<BaseClass>(testClass);
        //Assert.IsType<BaseClass>(result); //failed
        Assert.IsType<TestClass>(result);
    }

    [Fact]
    public void test4() //Pass
    {
        var result = Assert.IsAssignableFrom<Object>(testClass);
        //Assert.IsType<Object>(result); //failed
        //Assert.IsType<BaseClass>(result); //failed
        Assert.IsType<TestClass>(result);
    }

    [Fact]
    public void test5() //Failed
    {
        Assert.IsAssignableFrom<DerivedClass>(testClass);
    }
}
Note that when using implicit type 'var' to hold the result of Assert.IsAssignableFrom method, the new variable's type is still the same as the tested object type (line 26 & 34).