Skip to content

Be careful using ThreadStatic in Visual Studio unit tests

Given is a ThreadStatic field which is set to empty in the static constructor, and set by a public instance method:

    public class TestClass
    {
        [ThreadStatic] 
        private static string threadStaticValue;
        static TestClass()
        {
            threadStaticValue = "";
        }
        public void SetThreadStaticValue(string value)
        {
            threadStaticValue = value;
        }
        public string GetThreadStaticValue()
        {
            return threadStaticValue;
        }
    }

Now two unit tests check whether the threadStaticValue is set correct.

    [TestClass]
    public class ThreadStaticTest
    {
        [TestMethod]
        public void WithThreadStaticValueSetTest()
        {
            Debug.WriteLine("Thread id: {0}", System.Threading.Thread.CurrentThread.ManagedThreadId);
            TestClass tc = new TestClass();
            tc.SetThreadStaticValue("foo");
            Assert.AreSame(tc.GetThreadStaticValue(), "foo");
        }
        [TestMethod]
        public void WithThreadStaticValueNotSetTest()
        {
            Debug.WriteLine("Thread id: {0}", System.Threading.Thread.CurrentThread.ManagedThreadId);
            TestClass tc = new TestClass();
            Assert.AreSame(tc.GetThreadStaticValue(), "");
        }
    }

Those tests will fail(good) or pass(bad) depending of whether the test engine will reuse the same thread.

Executed a second time, the result could be different.

Solution:

  1. Don't use ThreadStatic at all
  2. Make sure the ThreadStatic fields are initialized correctly before using them

Howto set MinWidth or MinHeight of a WinRT ScrollViewer

I use the ScrollViewer control in a windows 8 metro app in order to zoom in/out an image. The problem now is, that it is possible to zoom in the image untill you almost can't zoom out again(because the image gets so small). I've tried MinWidth and MinHeigt on the ScrollViewer and on the image, but it didn't work. After some time i found the MinZoomFactor of the ScrollViewer, and this now works. It was just not obvious to me...

  
        
            
                
            
        
    

Exception: That assembly does not allow partially trusted callers

I have .net 3.5 program where i load some assemblies from a network share via reflection. Now i've got the following exception: "That assembly does not allow partially trusted callers''(Detailed message below). The calling assembly(.exe) is local. This .exe loads the assembly from network via reflection. The assembly from the network share then has a reference on a third assembly which is located beneath the .exe. (Hard to explain, image will do better...)

The reason for this failure is the .net security. It doesn't allow the MyLib.dll to be loaded, because the calling assembly is located on a network share and is signed with a strong name.

Solution 1, caspol.exe Allow all code to be executed. This might be dangerous, but if you're desperate it could be a solution.

 "C:\Windows\Microsoft.NET\Framework\v2.0.50727\CasPol.exe" -machine -chggroup All_Code FullTrust

Solution 2, no signing If you remove the signature from MyLib2.dll, everything works.

Solution 3, set attribute AllowPartiallyTrustedCallers If you set the AllowPartiallyTrustedCallers in the AssemblyInfo.cs of the signed assembly, everything works.

using System.Security;

[assembly: AllowPartiallyTrustedCallers()]

Usefull links

Continue reading "Exception: That assembly does not allow partially trusted callers"