Contents tagged with .NET

  • Merge multiple dll ‘s into one WPF application

    Tags: .NET, WPF

    It’s pretty standard scenario to have your application reference assemblies that are not part of the .net framework.  In those instances the default behaviour is for VS to sit those dll’s along side your built .exe file.

    Of course the application won’t run without them, but it can be a pain (or at least unwanted) to have to deploy these dll’s with the application.  Especially if you want to share/move the .exe file around the place.

    ILMerge to the rescue, this is a groovy little tool from Microsoft that will “Merge” your dll’s into a single dll/exe.  However, this does not work with WPF applications and was never designed too.

    After a little digging around I found this solution, it does require a little code, but nothing heavy.  I’d actually rather have this piece of code than flakey post build commands anyway.

    Essentially you add your dll to the application in VS, set it as an Embedded Resource and put the code chunk below into the Application OnStartup event.

    protected override void OnStartup(StartupEventArgs e)
    {
    	base.OnStartup(e);
    	AppDomain.CurrentDomain.AssemblyResolve += (sender, args) => {
    
    		string resourceName = "<Your namespace>.<folder name dll is in>." + new AssemblyName(args.Name).Name + ".dll";
    
    		try {
    			using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName)) {
            	        	Byte[] assemblyData = new Byte[stream.Length];
    		                stream.Read(assemblyData, 0, assemblyData.Length);
            	        	return Assembly.Load(assemblyData);	
    			}
    	        } catch (Exception ex) {
    			throw new System.IO.FileLoadException("Cannot find dll embedded resource - " + resourceName, ex);
    	        }
    	};
    }

    Don't forget to change the reference to the assembly to "Copy Local = False", we don’t need that to copy anymore!

    Further:
    Costura is a tool/add in that will basically do the above, without writing the code.  I’m happy with the code!

    more...

  • Minify JS and CSS in Visual Studio Build Events

    Tags: .NET 4.0, .NET, ASP.net, VS 2010, SharePoint, Personal

    It’s pretty widely accepted (and common sense) that if you’re putting a site out into the wild – such a simple task as minifying CSS and JavaScript files can save a bunch of load time, especially on mobile devices.

    I’ve been playing about with this today and was quite happily using jsmin as described here until I noticed that in IE 7 and IE 8 the minified CSS didn’t work.

    The problem seems to be when using a background style, it takes out the space between the image url close bracket and the next style.

    background:url(image.png) no-repeat;
    background:url(image.png)no-repeat;

    Not a problem in IE 9 or chrome (haven’t tested others).

    So I set about researching the problem and found nothing…struggled a bit getting the search term right and clearly never mastered it as I found zip.

    Not to be put off, I was looking at the YUI compression tool from Yahoo, said to be better…but needs the java runtime and seems to complex to simply use, certainly for my simple taste!

    Then I found a YUI .net implementation on codeplex.  Excellent, its was even in NuGet.

    However…I wanted it as a build task to run in VS, I don’t really want to think about this stuff more than once.

    So I wrote a little command line wrapper for it.  Excellent.  Even got ILMerge involved to merge the YUI dll’s into my exe.  I know there are ways to use Reflection to load dll’s as embedded resources…but I didn’t fancy doing that!

    I did look at using CommandLine (also on codeplex) as I have done before, but ILMerge does not like that.  Don’t take my word for it, but it seems to be something to do with ILMerge is .net 2 and CommandLine uses some .net4 features.  It wasn’t happy…anyway, I just wrote a few lines of code to parse the command line args myself.

    Perfect!!!

    It’s not complex or very flexible, but is very suitable to my needs.

    You can pass in an array of files to minify and a target path.  That’s it.  It’ll merge the files if you pass more than one…you have no choice!

    > SCmin.exe –s C:\big.css C:\big1.css –t C:\small.css

    // This will take big.css and big1.css and put it into small.css

    Simples!

    Download the exe or full source.

      

    more...

  • Using Telerik MVC with your own custom jQuery and or other plug-ins

    Tags: ASP.net, .NET, .NET 4.0, ASP.net MVC

    If you are using MVC it might be worth checking out the telerik controls (http://demos.telerik.com/aspnet-mvc), they are free if you are doing an internal or “not for profit” application.

    If however you do choose to use them, you could come up against a little problem I had.  Using the telerik controls with your own custom jQuery.  In my case I was using the jQuery UI dialog.

    It kept throwing an error where I was setting my div to a dialog.

    Code Snippet
    1. $("#textdialog").dialog({

    The problem is when you use the telerik mvc stuff you need to call ScriptRegistrar

    Code Snippet
    1. @Html.Telerik().ScriptRegistrar()

    in order to setup the javascript for the controls.

    By default this adds a reference to jQuery and if you have already added a reference to jQuery because you are using it elsewhere, this causes a problem.

    I found the solution here

    And it was to change the above ScriptRegistrar call to this…

    Code Snippet
    1. @Html.Telerik().ScriptRegistrar().jQuery(false).DefaultGroup(g => g.Combined(true).Compress(true));

    If you come across this one on stackoverflow it wont work – in my case the HtmlEditor would render no problem, but was unusable.  Which is the same as someone else found when using the tab control – they went to the bother of re-writing the ScriptRegistrar.  Not for me that one!!

    more...

  • ASP.net MVC | MvcBuildViews | MachineToApplication Error

    Tags: ASP.net MVC, .NET, ASP.net, Visual Studio

    You may come up against this little error when working on a MVC application, I specifically find things like this crop up when you are upgrading from previous version e.g. VS 2008 to 2010 and the like.

    They are a complete pain in the bum quite frankly, you can spend hours googling around, trying this trying that, posting questions here and there etc etc…so I’m adding to the this and that which you will of course try, because it’s the sort of error you don’t care about…it’s just getting in the way of your day!

    There would seem to be some kind of issue with MvcBuildViews and this error http://goo.gl/ogo1e

    Anyway, what worked for me was to delete the debug directory from within the obj directory and it all worked…

    Hopefully this will help someone else!! Or at the very least be something else to try on the way to solving another time wasting issue!!

    UPDATE:  May have been a bit hasty in my excitement of solving this…its really only part solved as I find i have to delete the debug directory every time I want to publish/package!

    more...

  • Dynamic connection for LINQ to SQL DataContext

    Tags: .NET 3.5, LINQ, Visual Studio, .NET

    If for some reason you need to specify a specific connection string for a DataContext, you can of course pass the connection string when you initialise you DataContext object.  A common scenario could be a dev/test/stage/live connection string, but in my case its for either a live or archive database.

    I however want the connection string to be handled by the DataContext, there are probably lots of different reasons someone would want to do this…but here are mine.

    • I want the same connection string for all instances of DataContext, but I don’t know what it is yet!
    • I prefer the clean code and ease of not using a constructor parameter.
    • The refactoring of using a constructor parameter could be a nightmare.

     

    So my approach is to create a new partial class for the DataContext and handle empty constructor in there.

    First from within the LINQ to SQL designer I changed the connection property to None.  This will remove the empty constructor code from the auto generated designer.cs file.

    image

    Right click on the .dbml file, click View Code and a file and class is created for you!

    image

    You’ll see the new class created in solutions explorer and the file will open.

    We are going to be playing with constructors so you need to add the inheritance from System.Data.Linq.DataContext

    1. public partial class DataClasses1DataContext : System.Data.Linq.DataContext
    2.    {
    3.    }

    Add the empty constructor and I have added a property that will get my connection string, you will have whatever logic you need to decide and get the connection string you require.  In my case I will be hitting a database, but I have omitted that code.

    1. public partial class DataClasses1DataContext : System.Data.Linq.DataContext
    2. {
    3.    // Connection String Keys - stored in web.config
    4.    static string LiveConnectionStringKey = "LiveConnectionString";
    5.    static string ArchiveConnectionStringKey = "ArchiveConnectionString";
    6.  
    7.    protected static string ConnectionString
    8.    {
    9.       get
    10.       {
    11.          if (DoIWantToUseTheLiveConnection) {
    12.             return global::System.Configuration.ConfigurationManager.ConnectionStrings[LiveConnectionStringKey].ConnectionString;
    13.          }
    14.          else {
    15.             return global::System.Configuration.ConfigurationManager.ConnectionStrings[ArchiveConnectionStringKey].ConnectionString;
    16.          }
    17.       }
    18.    }
    19.  
    20.    public DataClasses1DataContext() :
    21.       base(ConnectionString, mappingSource)
    22.    {
    23.       OnCreated();
    24.    }
    25. }

    Now when I new up my DataContext, I can just leave the constructor empty and my partial class will decide which one i need to use.

    Nice, clean code that can be easily refractored and tested.

    Share this post :

    more...

  • EasyDB | What a complete waste of my time...

    Tags: .NET, ASP.net, SQL

    After watching a DNRTV episode with Scott Cate on EasyDB I was really impressed and could definitely see how it could solve a requirement in a project I am planning...

    Straight after listening to the show, with the advice of Scott Cate (a promotion code), I hot stepped over to http://easydb.com to sign up.  After waiting for a few days, maybe a week and still having heard nothing I tried again and was greeted with this little chestnut...

    image 

    ...and that remains to this day.

    Why bother doing a dnrtv episode then not letting me in to try it, blog about it and generally test it???? Pointless...

    Anyone else using this beta that may have the power to let me in?!?!

    more...

  • LINQ the lot

    Tags: .NET, LINQ

    LINQ goes so much further than just SQL and XML.

    Something like this is just why I love LINQ, a LINQ query on an ASP.NET ListView controls items.

       1:  Dim items = From lvi In AspNetListViewControl.Items _
       2:              Where CType(lvi.FindControl("DropDownList1"), 
    	                DropDownList).SelectedValue = someIntVar _
       3:     Select lvi)

    Getting a ListViewItem's where the selected value of a DropDownList is set to the value I want.  So simple, yet so powerful.

    The observant among you will notice that this is in VB.net, not my native C#!! More on that soon! :)

    Technorati Tags: ,

    more...