Git Product home page Git Product logo

havit.castlewindsor.webforms's Introduction

Havit.CastleWindsor.WebForms

NuGet Package Build

Implementation of Castle Windsor DI container to ASP.NET WebForms 4.7.2. In previous versions there is no direct support due to mising extensibility point HttpRuntime.WebObjectActivator. You can find more details in news article.

Instalation to existing project

  1. Switch Target framework for your project to .NET Framework 4.7.2. If you don't have installed .NET Framework 4.7.2 developer pack. You can download it from here.

Check web.config and targetFramework in httpRuntime section. Both must be set to 4.7.2.

<system.web>
	<compilation debug="true" targetFramework="4.7.2"/>
	<httpRuntime targetFramework="4.7.2"/>
</system.web>
  1. Add nuget package Havit.CastleWindsor.WebForms. Don't forget to reinstall other nuget packges, if you changed Target framework in previous step.
  2. Install nuget packge Castle.Windsor.
  3. Add this initiaizing code to Application_Start method in Global.asax. Extension method AddWindsorContainer will initialize new container and will use it for resolving dependencies.
IWindsorContainer container = this.AddWindsorContainer();
  1. Also add using to the header of global.asax
using Havit.CastleWindsor.WebForms;

Working areas

There are many areas you can use Dependency Injection in WebForms applications now. Here is a complete list:

  • Pages and controls
    • WebForms page
    • User control
    • Custom control
  • IHttpHandler and IHttpHandlerFactory
  • IHttpModule
  • Providers
    • BuildProvider
    • ResourceProviderFactory
    • Health monitoring provider
    • Any ProviderBase based provider created by System.Web.Configuration.ProvidersHelper.InstantiateProvider. e.g. custom sessionstate provider

Known limitations

Because it is not possible to use Dependency Injection through WebOjectActivator in Web Services (*.asmx), we added a workaround for this case, so you can do dependency injection to web services via properties.

How to inject to Web Services

  1. Web Service must inherit from the abstract class Havit.CastleWindsor.WebForms.InjectableWebServiceBase
  2. Every property, you want to inject must be marked with the attribute Havit.CastleWindsor.WebForms.InjectAttribute
  3. Every property, you want to inject must have a public getter and setter

Example

We have prepared a simple example appliction with one page and one web service with Dependency Injection. See Havit.CastleWindsor.WebForms.Example appliction.

Troubleshooting

  1. If you hit the error, that a page (or an user control) cannot be created because of missing a contructor with zero arguments, check if you had switched your project to .NET FW 4.7.2 (don't forget to check compilation and httpRuntime elements in the web.config file).

havit.castlewindsor.webforms's People

Contributors

hakenr avatar jirikanda avatar vaclavek avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

havit.castlewindsor.webforms's Issues

Component ... could not be registered. There is already a component with that name.

Steps to reproduce

  • Prepare Default.aspx
  • Run the application, open Default.aspx
  • Modify Default.aspx (do not stop the running application)
  • Refresh Default.aspx

Behavior

This exception is thrown:

Component ... could not be registered. There is already a component with that name. Did you want to modify the existing component instead? If not, make sure you specify a unique name.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.