Friday, December 12, 2014

cool button


  1. background-color#e7e7e7;
  2. box-shadow0 0 2px rgba(0,0,0,.2), inset 0 14px 16px rgba(255,255,255,.75), inset 0 0 7px #fff;
  3. padding.8em 0;
  4. displayblock;
  5. -moz-border-radius.3em;
  6. -webkit-border-radius.3em;
  7. border-radius.3em;
  8. cursorpointer;
  9. bordernone;
  10. color#646464;
  11. text-transformuppercase;
  12. text-aligncenter;
  13. vertical-alignmiddle;

Friday, December 5, 2014

Could not load file or assembly … The parameter is incorrect

Looks like a corrupted assembly being referenced.
Clear both:
  1. the \bin folder of your project
  2. the temp folder (should be C:\Users\your_username\AppData\Local\Temp\Temporary ASP.NET Files in windows 7)
and see if the error still happens


Depending on if your are running X64 you might need to clean up a couple more spots. Just cleaning up my user directory was not enough.
  1. %TEMP%\Temporary ASP.NET Files
  2. C:\Windows\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files
  3. C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files
  4. C:\Windows\Microsoft.NET\Framework64\v2.0.50727\Temporary ASP.NET Files
  5. C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files
This list will grow as if you have other versions of the framework installed.

Wednesday, December 3, 2014

install a windows service

You can install the service by adding this code (in the program file, Program.cs) to install itself when run from the commandline using specified parameters:
/// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main(string[] args)
        {
            if (System.Environment.UserInteractive)
            {

                if (args.Length > 0)
                {
                    switch (args[0])
                    {
                        case "-install":
                            {
                                ManagedInstallerClass.InstallHelper(new string[] { Assembly.GetExecutingAssembly().Location });
                                break;
                            }
                        case "-uninstall":
                            {
                                ManagedInstallerClass.InstallHelper(new string[] { "/u", Assembly.GetExecutingAssembly().Location });
                                break;
                            }
                    }
                }
            }
            else
            {
                ServiceBase[] ServicesToRun;
                ServicesToRun = new ServiceBase[] { new MyService() };
                ServiceBase.Run(ServicesToRun);
            }
        }

Tuesday, December 2, 2014

debug the windows servie

put it in onstart method of a windows service to launch a debugger
System.Diagnostics.Debugger.Launch();

Thursday, November 13, 2014

Render a mvc view as a string

Here's what I came up with, and it's working for me. I added the following method(s) to my controller base class. (You can always make these static methods somewhere else that accept a controller as a parameter I suppose)
MVC2 .ascx style
protected string RenderViewToString<T>(string viewPath, T model) {
  ViewData.Model = model;
  using (var writer = new StringWriter()) {
    var view = new WebFormView(viewPath);
    var vdd = new ViewDataDictionary<T>(model);
    var viewCxt = new ViewContext(ControllerContext, view, vdd,
                                new TempDataDictionary(), writer);
    viewCxt.View.Render(viewCxt, writer);
    return writer.ToString();
  }
}
Razor .cshtml style
public string RenderRazorViewToString(string viewName, object model)
{
  ViewData.Model = model;
  using (var sw = new StringWriter())
  {
    var viewResult = ViewEngines.Engines.FindPartialView(ControllerContext,
                                                             viewName);
    var viewContext = new ViewContext(ControllerContext, viewResult.View,
                                 ViewData, TempData, sw);
    viewResult.View.Render(viewContext, sw);
    viewResult.ViewEngine.ReleaseView(ControllerContext, viewResult.View);
    return sw.GetStringBuilder().ToString();
  }
}
Edit: added Razor code.
share|improve this answer
12 
Rendering a view to a string is always "inconsistent with the whole routing concept", as it has nothing to do with routing. I'm not sure why an answer that works got a down vote. –  Ben Lesh Nov 3 '10 at 20:15 
3 
I think you might need to remove the "static" from the Razor version's method declaration, otherwise it can't find ControllerContext et al. –  Mike Jan 14 '12 at 5:10
3 
You'll need to implement your own method of removal for those superfluous whitespaces. The best way I can think of off the top of my head is to load the string into an XmlDocument, then write it back out to a string with an XmlWriter, as per the link I left in my last comment. I really hope that helps. –  Ben Lesh Feb 16 '12 at 14:01
2 
Hmm how should I do this using a WebApi Controller, any suggestions would be appreciated –  AlexanderMay 6 '13 at 16:41
2 
Hi everyone to use it with "Static" keyword for all controllers to make it common you have to make static class and inside it you have to put this method with "this" as parameter to "ControllerContext" . You can see here stackoverflow.com/a/18978036/2318354 it . –  Dilip0165 Sep 24 '13 at 9:49 

Tuesday, September 30, 2014

Adstract Factory Design Pattern

First of all, I would suggest you to read about the Abstract Factory pattern, for example here. Now I will try to explain why you would use this pattern.
Normally, if you use the Factory pattern, you will create objects in a Factory. The problem arises when you have multiple implementation of a given class (or classes). Now, those multiple implementations are grouped. You will use the Abstract Factory pattern when you have a factory, but you would like to group the creating of objects per group.
Okay, above explanation might not be completely clear, so I will give you an example.
Let's say you have a class library with data agents. Data agents provide you methods to access and store different data. Of course, there are multiple ways of storing your data. For example: in a database, in XML file, over a service, . For each of these possible ways, you would like to have data agents. Now the problem is, you don't want that someone uses the DataAgentA for XML files together with DataAgentB for database (let's assume that we have entities A and B). The user should use only one storage engine.
Let me introduce you the Abstract Factory pattern.
You will make sure that users cannot directly instantiate your Data Agents, but they will have to get these data agents out of a factory. (An extra advantage is, that when you use for example a database (EF), you can do internal wiring to make sure your Data Agents use the same context, etc.) How do we accomplish this? We set the constructor of our data agents to ´internal´. Apart from that, we create different factories for each storage engine. Now, since those factories all do the same, we also have these interfaced (just like our data agents, since they all have to do the same, right!?).
Below we have our interfaces. Basically this is the factory pattern, but only now instead of about classes, we are talking about interfaces.
public interface IAgentA 
{
    // Add some methods here!
}

public interface IAgentB
{
    // Add some methods here!
}

public interface IAgentFactory
{
    IAgentA CreateAgentA();
    IAgentB CreateAgentB();
}
Now for the two agents, we have two possible implementations, one for XML and one for database storage (again: this is an example, you can have as many implementation types as you want). Those implementations would look like this (see below). Please note that I made the constructor internal! This is needed for the part that comes after this code block.
public class AgentA_Xml : IAgentA
{
    internal AgentA_Xml()
    { /* Construction here */}

    // IAgentA method implementations
}

public class AgentB_Xml : IAgentB
{
    internal AgentB_Xml()
    { /* Construction here */}

    // IAgentB method implementations
}


public class AgentA_Database : IAgentA
{
    internal AgentA_Database()
    { /* Construction here */}

    // IAgentA method implementations
}

public class AgentB_Database : IAgentB
{
    internal AgentB_Database()
    { /* Construction here */}

    // IAgentB method implementations
}
Now as the constructors are internal. This causes that you cannot instantiate those classes outside the assembly, which is generally what you do with these kind of cases. Now we have to create our factories.
public class XMLAgentFactory : IAgentFactory
{
    public IAgentA CreateAgentA()
    {
        return new AgentA_Xml();
    }

    public IAgentB CreateAgentB()
    {
        return new AgentB_Xml();
    }
}


public class DatabaseAgentFactory : IAgentFactory
{
    public IAgentA CreateAgentA()
    {
        return new AgentA_Database();
    }

    public IAgentB CreateAgentB()
    {
        return new AgentB_Database();
    }
}
Since both factories implement the IAgentFactory interface, the user can easily change of AgentFactory implementation (if he, in this case, wants to use a different storage engine) without having to change any other code he wrote (against the agents), as long as he programmed against the interfaces (obviously).
Above explanation hopefully answers your questions (1) and (2).
  1. Good example for Abstract factory pattern in C#?
  2. and what are advantages of Abstract factory pattern in c#?
Answering your question (3).
  1. How use C# generics with Abstract factory pattern?
You can still use generics, this doesn't change any bit when you use an Abstract Factory pattern. Of course, you will have to create generic factory methods (the create methods), but that shouldn't be any problem.
Answering your question (4).
  1. How does unit test with Abstract factory pattern?
Just the same as you would unit test any other class. Only one thing will be different.
Since you probably also want to test the constructor of your classes (and maybe other internal methods), you need to make the internal constructors (methods) visible to your unit test project (and you don't want to change the internal to public). This is easily done by adding the following line to your AssemblyInfo.cs file of your project (the project where your factory and classes are in):
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("My.UnitTest.Namespace")]
You can find more information (and remarks) about the InternalsVisibleTo attribute on MSDN.
I hope this kind of answers your question.