Tuesday, December 10, 2013

MVC 4 and Ninject

Starting MVC 4 with Ninject.

In VS 2012, Go to > Tools > Library Package Manager > Manage NuGet Packages for Solution.

----
Search for and select Ninject.MVC3 then install.
----

It will create a NinjectWebCommon.cs inside App_Start

Reference your dependency resolution project / dll

Add code to instantiate implementations for your interfaces.

e.g: //IKernel kernel = new StandardKernel(
            //    new LoggingModule(typeof(App).ToString(), App.AppPath.ToString())
            //    );

            //kernel.Bind<IUserService>().To<UserService>();
            //kernel.Bind<IDialogService>().To<DialogService>();
            //kernel.Bind<IWorker>().To<AsyncWorker>();
            //kernel.Bind<IDialogService>().To<DialogService>();          
            string appPath = System.Web.Hosting.HostingEnvironment.MapPath(System.Web.HttpRuntime.AppDomainAppVirtualPath);
            var modules = new List<INinjectModule>
            {
                new LoggingModule(("Ninject"), appPath)
                //new RepositoryModule()              
            };
            kernel.Load(modules);

Monday, July 15, 2013

Self Hosting WCF

Security Rights

Check and firewall:

Window 7 Client

  • netsh http add urlacl url=http://+:8090/ClientService/AppointmentNotification user=myUser or = users

XP

  • httpcfg.exe set urlacl /u http://+:11715/service/ /a <http://+:11715/service/%20/a>  D:(A;;GX;;;BU)

Refference:
http://msdn.microsoft.com/en-us/library/ms733768.aspx

Tuesday, June 25, 2013

Sync to ancestors properties


<Canvas>

  <DockPanel
      Width="{Binding RelativeSource=
                {RelativeSource FindAncestor,
                AncestorType={x:Type Canvas}},
                Path=ActualWidth}"
      Height="{Binding RelativeSource=
                {RelativeSource FindAncestor,
                AncestorType={x:Type Canvas}},
                Path=ActualHeight}">
  </DockPanel>
</Canvas>

Window Service - Pointers and troubles encountered.

AppDomain.CurrentDomain.BaseDirectory - To have a path same as the service location.
----------------------------------------
Add debug mode in win service
     #if DEBUG #else #end
----------------------------------------
Install Manually
Right click window service and add install

  • click serviceProcessInstaller and go to properties
    1. change account property : LocalService for local and LocalSystem for server

  • click serviceInstaller and go to properties
    1. start type - automatic
    2. update service name and description to project name
Go to developer cmd: run as ADMINISTRATOR
Create batch files
  • InstallUtil ServiceName.exe
  • InstallUtil /u ServiceName
C:\Windows\Microsoft.NET\Framework\v4.0.30319\installutil.exe

Right click service and select recovery
----------------------------------------
Error encountered 1
System.ComponentModel.Win32Exception: The specified service already exists serviceInstaller.DisplayName = "A new name";

  • sc delete YourServiceName

Error encountered 2
Error 1053: The service did not respond to the start or control request in a timely fashion framework version problem 2008 r2
----------------------------------------
Error Handling
Add another thread to catch the error
catch (Exception exception)
            {
                System.Threading.ThreadPool.QueueUserWorkItem(
                    _ => { _logger.Error("", exception.Message, "", "", exception.StackTrace); });              
                    //_ => { throw new Exception("Exception on timer.", exception); });              
            }

----------------------------------------
Setup same framework

Check Windows Event, Windows Logs, System
----------------------------------------
Helpfull Links
http://stackoverflow.com/questions/14121079/windows-could-not-start-service-on-win-server-2008-r2-sp1-error-1053
http://www.coretechnologies.com/products/AlwaysUp/Apps/RunCommandPromptAsAService.html

----------------------------------------
Install Automatically manually

Add a new project

Thursday, April 18, 2013

Ninject







I was once using IOC in one of my earlier projects back in 2007. "Castle Windsor" Now it's time to explore new dependency injectors. Ninject.

I am creating an admin tool for my current project. I want to explore new architecture, patterns and a testable loosely coupled application. So first goal is to setup a dependency resolution project which will help me decoupled my concrete classes from my client. To achieve this, here is a sample of code.

This is the entry point of my application. ILogger is the interface of my service that i will be using in my application. In this example, I used IKernel to create an instance of my ILogger to implement a specific implementation which is FileLogger.


using Infrastructure.Interfaces;
using Ninject.Modules;


public partial class App : Application
{
ILogger _logger;

protected override void OnStartup(System.Windows.StartupEventArgs e)
        {
            SingleInstance.Make();
            base.OnStartup(e);

            IKernel kernel = new StandardKernel(new LoggingModule(typeof(App).ToString(),   App.AppPath.ToString()));
            DependencyResolution(kernel.Get<ILogger>());

            CreateEntryWindow();                                  
            _logger.Info("Application started!");
        }

}


private void DependencyResolution(ILogger logger)
        {
            _logger = logger;
        }




------------------------------------------------------------------------------

using BackDoors.Logger;
using Infrastructure.Interfaces;
using Ninject.Modules;



public class LoggingModule: NinjectModule
    {      
        string _classObject;
        string _appPath;

        public LoggingModule(string classObject,string appPath="")
        {          
            _classObject = classObject;
            _appPath = appPath;
        }      

        public override void Load()
        {
            Bind<ILogger>().To<FileLogger>()
                .WithConstructorArgument("classObject", _classObject)
                .WithConstructorArgument("appPath", _appPath);
        }      
    }