Monday, July 21, 2008

CAS & RefusedSet of permissions

I granted assemblies with a particular Strong Name "Full Trust" both in "My Computer" and "Intranet" zone.  When I run them out of VS.Net IDE they run with full set of permissions.  But, the moment I specify one or more permissions as "requisites" on my assembly, I get SecurityException (s) for the permissions I didn't mention on my assembly.

  1. I used .Net Framework 2.0 Configuration Wizard to grant all assemblies (in MyComputer zone as I'm debugging in my IDE)  that has a particular Strong Name.
  2. Now, I go ahead and specify a single pre-requisite for the assembly:
    • [assembly: SqlClientPermission(SecurityAction.RequestMinimum, Unrestricted = true)]
  3. I run the application
  4. A tell-tale security warning appears signifying the assembly is no longer running under "Full Trust" CAS-1
    • Warning: Indicates assembly is no longer running under "Full Trust"
  5. I specify UI permissions now:
    • [assembly: UIPermission(SecurityAction.RequestMinimum, Unrestricted=true)


    • Fig 2: UIPermission granted.  No warning messages
  6. The warning disappears
  7. As soon as I move to the next step in the wizard [where it requires a ConfigurationPermission], it throws a SecurityException CAS-3
    • Fig 3: Exception due to ConfigurationPermission denied
  8. But when I remove all CAS permission request declarations, the code appears to run with "Full Trust"


    • Fig 4: All access granted [including ConfigurationPermission]  

This leads me to wonder if the permissions I do not specify on the assembly become part of the permissions refused!

Wednesday, July 16, 2008

Castle Windsor RC3 Woes Continued...


Now, I'm asking WindsorContainer to pickup from the default configuration file as it supposed to:

   45         public static IDbCreator GetDBCreator()
   46         {
   47             WindsorContainer container = new WindsorContainer(); //WindsorConfigFile);
   48             IDbCreator creator = container.Resolve<IDbCreator>(UserPreferences.Instance.LocalDataStoreChosen);
   49             return creator;
   50         }
With this result:
Of course, AppDomain.CurrentDomain.SetupInformation.ConfigurationFile is pointing to a valid *.dll.config file and is having all keys:
      <component id="DbTypeSql"
            type="ThinkFarAhead.WordReporter.Data.SqlDbCreator, ThinkFarAhead.WordReporter.Data">

Castle Windsor RC3 Detests MSTest?: DeserializeElement cannot process element configSections


I'm exasperated getting Castle Windsor to work with MSTest.  For once, it's not the other way around :)  Here's the partial code tries to resolve a database provider using Windsor.  The App.config in my MSTest project is copied to a TestResults folder with a *.dll.config extension [I didn't know .Net started allowing .dll.config files.  Well may not be, but that's a topic for another post!). 

Here's my app.config [ThinkFarAhead.WordReporter.Data.UnitTest.dll.config]:

    1 <?xml version="1.0"?>
    2 <configuration>
    3   <configSections>
    4     <section name="castle"
    5         type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler, Castle.Windsor" />
    7   </configSections>
    9   <castle>
   10     <components>
   11       <component id="DbTypeSql"
   12             service="ThinkFarAhead.WordReporter.Data.IDbCreator"
   13             type="ThinkFarAhead.WordReporter.Data.SqlDbCreator, ThinkFarAhead.WordReporter.Data">
   14       </component>
   15       <component id="DbTypeOracle"
   16             service="ThinkFarAhead.WordReporter.Data.IDbCreator"
   17             type="ThinkFarAhead.WordReporter.Data.OracleDbCreator, ThinkFarAhead.WordReporter.Data">
   18       </component>
   19       <component id="DbTypeOledb"
   20             service="ThinkFarAhead.WordReporter.Data.IDbCreator"
   21             type="ThinkFarAhead.WordReporter.Data.OleDbDbCreator, ThinkFarAhead.WordReporter.Data">
   22       </component>
   23       <component id="DbTypeOdbc"
   24             service="ThinkFarAhead.WordReporter.Data.IDbCreator"
   25             type="ThinkFarAhead.WordReporter.Data.OdbcDbCreator, ThinkFarAhead.WordReporter.Data">
   26       </component>
   27     </components>
   28   </castle>
   29 </configuration>
Here's the code that tries to inject dependencies:

   37         private static string WindsorConfigFile;
   38         static DbCreatorFactory()
   39         {
   40             WindsorConfigFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile; //  set // "Windsor.config"; //string.Format(@"{0}\Windsor.Config", Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase));
   41         }
   43         //Removed statically typed enum [that cannot be extended at runtime]
   44         //Here at least other clients can pass anything
   45         public static IDbCreator GetDBCreator()
   46         {
   47             WindsorContainer container = new WindsorContainer(WindsorConfigFile);
   48             IDbCreator creator = container.Resolve<IDbCreator>(UserPreferences.Instance.LocalDataStoreChosen);
   49             return creator;
   50         }

Here's my MSTest code:

   69         [TestMethod]
   70         public void CheckObjectCreation()
   71         {
   72             string[] databaseTypes = new string[] { "DbTypeSql", "DbType.Oracle" }; //, "DbType.Oledb", "DbType.Odbc"};
   73             Type [] dbCreators = new Type [] { typeof(SqlDbCreator), typeof(OracleDbCreator)}; //, typeof(OracleDbCreator), typeof(OleDbDbCreator)
   74             for (int i = 0; i < databaseTypes.Length; i++)
   75             {
   76                 UserPreferences.Instance.LocalDataStoreChosen = databaseTypes[i];
   77                 if(!(DbCreatorFactory.GetDBCreator() == dbCreators[i]))
   78                 {
   79                     Assert.Fail(string.Format("Type {0} instantiated incorrectly for key {1}", dbCreators[i].ToString(), databaseTypes[i]));
   80                 }
   81             }
   82         }

It fails with:



Technorati Tags: ,

Sunday, July 13, 2008

Tip: ScriptSharp and $(ScriptPath)

If you're using Script# "Class Library in a Web Site" template, you need to make sure you're Script# Projects are located inside the "bin\Scripts" of the Web site. Yes, you heard me right. If you want a reusable class library or simply want to use the scripts generated on multiple Web projects on the same solution, use either "Class Library" or "Atlas Class Library". I thought I had found a bug when I couldn't find the script files. Here's the complete analysis for the brave: 1) Select Script# "Class Library in a Web Site" template

2) Type the following it the default "Class1.cs" created
    1 // Class1.cs
    2 //
    4 using System;
    5 using System.DHTML;
    6 using ScriptFX;
    7 using ScriptFX.UI;
    9 namespace ThinkFarAhead.WordReporter.TestBed.ScriptsForWeb
   10 {
   12     public class Class1
   13     {
   14         public void Greet()
   15         {
   16             Script.Alert("Hi!");
   17         }
   19         public void Main()
   20         {
   21             new Class1().Greet();
   22         }
   23     }
   24 }
3) Compile and search for the *.js files created. Hey, at least I was searching for it forever! Here's my line of thinking went: If Script# is using MSBuild, the code for calling ssc.exe should be in the project.
   49   <Import Project="$(ProgramFiles)\nStuff\ScriptSharp\v1.0\nStuff.ScriptSharp.targets" />
Well, it does import some MSBuild targets. Let's see what is it:
   32     <ScriptCompilerTask
   33       Sources="@(Compile)"
   34       Resources="@(EmbeddedResource)"
   35       References="@(ReferencePath)"
   36       Defines="$(DefineConstants)"
   37       OutputPath="$(OutputPath)"
   38       ScriptPath="$(ScriptPath)"
   39       LocaleSubFolders="$(LocaleSubFolders)"
   40       ScriptName="$(ScriptName)"
   41       Template="$(TemplateFile)"
   42       CopyReferences="$(CopyReferences)"
   43       CSharpAssembly="@(IntermediateAssembly)"
   44       DocumentationFile="$(DocumentationFile)"
   45       SuppressDocumentation="$(SuppressDocumentation)">
   46       <Output TaskParameter="DebugScriptFile" ItemName="DebugScriptFile" />
   47       <Output TaskParameter="ReleaseScriptFile" ItemName="ReleaseScriptFile" />
   48     </ScriptCompilerTask>
Hm... it seems to be putting the scripts at $(ScriptPath). Let's see what's the path during compilation:
   49   <!-- Added by Vyas to know where Script# is keeping my files-->
   50   <Warning Text="Script Folder: $(ScriptPath)"/>
The project template is showing a value for $(ScriptPath) and I'm sure the compile-time warning added would confirm it
   22     <ScriptPath>..\..\..\App_Scripts\</ScriptPath>
Here's the compiler output:

------ Build started: Project: ThinkFarAhead.WordReporter.Web.Scripts, Configuration: Debug Any CPU ------

C:\Windows\Microsoft.NET\Framework\v3.5\Csc.exe /noconfig /nowarn:0028,1591,1701,1702 /nostdlib+ /errorreport:prompt /warn:4 /doc:..\ThinkFarAhead.WordReporter.Web.Scripts.xml /define:DEBUG /reference:"C:\Program Files (x86)\nStuff\ScriptSharp\v1.0\Framework\sscorlib.dll" /reference:"C:\Program Files (x86)\nStuff\ScriptSharp\v1.0\Framework\ssfx.Core.dll" /reference:"C:\Program Files (x86)\nStuff\ScriptSharp\v1.0\Framework\ssfx.UI.Forms.dll" /debug- /optimize+ /out:obj\Debug\ThinkFarAhead.WordReporter.Web.Scripts.dll /target:library Properties\AssemblyInfo.cs ClientCapabilityDetecter.cs

Compile complete -- 0 errors, 0 warnings

C:\Program Files (x86)\nStuff\ScriptSharp\v1.0\nStuff.ScriptSharp.targets : warning : Script Folder: ..\..\App_Scripts\

Done building project "ThinkFarAhead.WordReporter.Web.Scripts.csproj".

I would have tought it should put it inside the project folder! It's dereferencing it from the project folder and put it on the root for me. Project folder: D:\C#\ , Script output folder: D:\App_Scripts. I had looked at the samples before and it's putting the scripts correctly on the App_Scripts folder. Closer scrutiny this time reveals Nikhi's put the Script Projects "inside" the Web project itself at "bin\Scripts"! No wonder "..\..\..\App_Scripts" works for him.

Oh yes, I'm kicking myself for not figuring this one out much earlier!

Tip: Script# for VS.Net 2008 & Windows Vista 64


Those who are developing on a Vista 64 know VS.Net 2008 cannot debug 64-bit assemblies.  You need to select x86 as CPU for the resultant assembly to be debuggable.  Though on Script# projects, make sure you have "Any CPU" selected.  Otherwise, it would not generate *.js files.

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">





  <NoWarn>0028, 1591</NoWarn>



<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">






  <NoWarn>0028, 1591</NoWarn>



DeskPins - Always On Top

Found a wonderful little gem of a program today: DeskPins.  Now I can make effective use of my 24" display!


Sunday, July 06, 2008

Extension Methods: Calling on an object instance

In C#, static methods could only be called through their respective type references.  The power of Extensions Methods lies in the fact they could be called on an object instance.  In fact, they could ONLY be called on an object instance.  This ability is vital for implementing Linq in C# 3.0.

    1 using System;
    2 using System.Collections.Generic;
    3 using System.Linq;
    4 using System.Text;
    6 namespace TestBed
    7 {
    9     public class TestExtensionMethods
   10     {
   11         public static void Main()
   12         {
   14             string val = "Vyas";
   16             //Static method of string class called through the class
   17             Console.WriteLine("Is Internered: {0}", string.IsInterned(val));
   19             //Static method called through an instance
   20             //Intellisense does not display it
   21             //Error: Member 'string.IsInterned(string)' cannot be 
   22             //accessed with an instance reference; 
   23             //qualify it with a type name instead
   24             //Console.WriteLine(val.IsInterned(val));
   27             //The ability for a static method to be called through an instance
   28             Console.WriteLine(val.Reverse());
   30             //Does not compile
   31             //Error: 'string' does not contain a definition for 'Reverse'
   32             //Console.WriteLine(string.Reverse(val));
   33             Console.Read();
   34         }
   36     }
   38     public static class StringUtils
   39     {
   40         public static string Reverse(this string arg)
   41         {
   42             char [] reversed;
   43             Array.Reverse(reversed = arg.ToCharArray());
   44             return new string(reversed);
   46         }
   47     }
   50 }


Saturday, July 05, 2008

My New Rig - Part II: Assembling

Apologies for posting this second issue after so long!  When quad-cores are going out of fashion and dual or quad-quad cores are in ;)

The story goes like this:

  1. I found SG975XBX2 does not have a VGA out!
    • Shelled out nearly INR 2500/- [around USD $55]
  2. The SG975XBX2 does not support Intel Core-2-Quad
  3. I waited for over 2 weeks to get a replacement [No D975XBX2].  The shopkeeper would tell one story or the other and kept me at bay [with my money securely in his pocket, of course :)] for a ASUS board.
  4. Finally, I gave in and asked for a MSI P35 Platinum Combo
    • I had to literally sit there for an hour to get this
  5. The new board MSI P35 Platinum Combo had dual RAM support (DDR2 & DDR3)
    • Aside:  When you turn on the power for the first time, it switches itself off and restarts, presumably to detect memory used.  I wonder what purpose the "toggle card" has.  We are supposed to switch direction of the card per type of RAM used.   As if the grave warnings threatening dire consequences if the type of RAM and the direction of the switch card is wrong weren't enough!
  6. Installed Vista 64 Enterprise
    • One of the perks of being a Volume Licensing Administrator for the company.  A MSDN Universal subscription
  7. The machine booted alright but the processor was overheating (about 95 degree C).  Eventually traced it to incorrectly installed Processor Fan.  Now it hovers around 35 degree C
  8. Alas, by now, my monitor, a 20" Samsung SyncMaster 206BW started displaying horizontal lines [it's still in Warranty but in Europe!].  There goes my 2000 Swedish Kroner down the drain!

Final Component List:

  • Intel Core 2 Quad Q6600
  • MSI P35 Platinum Combo
    • Integrated Sound 7.1
    • Integrated LAN Support
  • Transcend 4 GB DDR II RAM [800 MHz]
  • Nvidia GeForce 8400 GS

I took my Desktop with me when I shifted my base to US last month.  I found, to my dismay, my SMPS wouldn't support dual voltage and I shelled out $150 for an Antec S75QB.  Also bought a Samsung SyncMaster mentioned above for $550

  • Antec S75QB 750W ATX 12V
  • Samsung  SyncMaster 2493HM (24" LCD)

BUG(?): Hosting Word 2007 Document inside IE

I'm in the process of writing an Open Source Word based reporting tool. It requires me to have Word 2007 hosted inside IE. I have two problems: 1. The standard default Word 2007 SDI behavior opens a blank Word Window (without any documents) 2. The MDI behavior is stranger [Uncheck Show All Windows in the Taskbar] Here's a flash movie I made demonstrating the issue. You can see how Word 2007 is giving me grief! Oh, of course, this behavior may be by design.