Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add base code + readme + example app #1

Merged
merged 3 commits into from
Jan 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions Havit.CastleWindsor.WebForms.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.28307.271
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Havit.CastleWindsor.WebForms", "src\Havit.CastleWindsor.WebForms\Havit.CastleWindsor.WebForms.csproj", "{328EC034-9DC8-4D18-B889-BE91E0F1F768}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Havit.CastleWindsor.WebForms.Example", "src\Havit.CastleWindsor.WebForms.Example\Havit.CastleWindsor.WebForms.Example.csproj", "{656D9D65-D7FB-4027-A865-9566882519C8}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{D30F96B3-FAE3-4BB2-AB6D-20B5B29653F5}"
ProjectSection(SolutionItems) = preProject
README.md = README.md
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{328EC034-9DC8-4D18-B889-BE91E0F1F768}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{328EC034-9DC8-4D18-B889-BE91E0F1F768}.Debug|Any CPU.Build.0 = Debug|Any CPU
{328EC034-9DC8-4D18-B889-BE91E0F1F768}.Release|Any CPU.ActiveCfg = Release|Any CPU
{328EC034-9DC8-4D18-B889-BE91E0F1F768}.Release|Any CPU.Build.0 = Release|Any CPU
{656D9D65-D7FB-4027-A865-9566882519C8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{656D9D65-D7FB-4027-A865-9566882519C8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{656D9D65-D7FB-4027-A865-9566882519C8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{656D9D65-D7FB-4027-A865-9566882519C8}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {6844D2A0-A9D8-4A2D-8223-CAC76E4FFA6F}
EndGlobalSection
EndGlobal
60 changes: 59 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,59 @@
# Havit.CastleWindsor.WebForms
# Havit.CastleWindsor.WebForms

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](https://blogs.msdn.microsoft.com/dotnet/2018/04/30/announcing-the-net-framework-4-7-2/).

## Instalation to existing project

1. First you must 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](https://www.microsoft.com/net/download/thank-you/net472-developer-pack).

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

```xml
<system.web>
<compilation debug="true" targetFramework="4.7.2"/>
<httpRuntime targetFramework="4.7.2"/>
</system.web>
```

2. Add nuget package **Havit.CastleWindsor.WebForms**. *Don't forget to reinstall other nuget packges, if you changed Target framework in previous step.*
3. Install nuget packge **Castle.Windsor**.
4. 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.
```c-sharp
IWindsorContainer container = this.AddWindsorContainer();
```
5. Also add using to the header of global.asax
```c-sharp
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 injection to web services via proerties.

### How to inject to Web Services
1. Web Service must inherit from abstract class **Havit.CastleWindsor.WebForms.InjectableWebServiceBase**
2. Every property, you want to inject must be marked with 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, where is used Dependency Injection. See Havit.CastleWindsor.WebForms.Example appliction.

## Troubleshooting
1. If you hit error saying page (or user control) cannot be created because of missing contructor with zero arguments, check if you switched you project to .NET FW 4.7.2.

## Licence
MIT
10 changes: 10 additions & 0 deletions src/Havit.CastleWindsor.WebForms.Example/Default.aspx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<%@ Page Language="C#" AutoEventWireup="false" CodeBehind="Default.aspx.cs" Inherits="Havit.CastleWindsor.WebForms.Example._Default" %>

<html>
<head>
<title>Hello Havit.CastleWindsor.WebForms!</title>
</head>
<body>
<asp:Literal ID="litHello" runat="server" />
</body>
</html>
22 changes: 22 additions & 0 deletions src/Havit.CastleWindsor.WebForms.Example/Default.aspx.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Web.UI;

namespace Havit.CastleWindsor.WebForms.Example
{
public partial class _Default : Page
{
private readonly IMyDependecy myDependecy;

public _Default(IMyDependecy myDependecy)
{
this.myDependecy = myDependecy;
}

protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);

litHello.Text = myDependecy.MyMethod();
}
}
}
24 changes: 24 additions & 0 deletions src/Havit.CastleWindsor.WebForms.Example/Default.aspx.designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/Havit.CastleWindsor.WebForms.Example/Global.asax
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%@ Application Codebehind="Global.asax.cs" Inherits="Havit.CastleWindsor.WebForms.Example.Global" Language="C#" %>
15 changes: 15 additions & 0 deletions src/Havit.CastleWindsor.WebForms.Example/Global.asax.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Castle.MicroKernel.Registration;
using System;
using System.Web;

namespace Havit.CastleWindsor.WebForms.Example
{
public class Global : HttpApplication
{
public void Application_Start(object sender, EventArgs e)
{
var container = this.AddWindsorContainer();
container.Register(Component.For<IMyDependecy>().ImplementedBy<MyDependency>());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.2.0.0\build\net46\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.props" Condition="Exists('..\..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.2.0.0\build\net46\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.props')" />
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>
</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{656D9D65-D7FB-4027-A865-9566882519C8}</ProjectGuid>
<ProjectTypeGuids>{349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}</ProjectTypeGuids>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Havit.CastleWindsor.WebForms.Example</RootNamespace>
<AssemblyName>Havit.CastleWindsor.WebForms.Example</AssemblyName>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
<UseIISExpress>true</UseIISExpress>
<Use64BitIISExpress />
<IISExpressSSLPort />
<IISExpressAnonymousAuthentication />
<IISExpressWindowsAuthentication />
<IISExpressUseClassicPipelineMode />
<UseGlobalApplicationHostFile />
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Castle.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc, processorArchitecture=MSIL">
<HintPath>..\..\packages\Castle.Core.4.2.0\lib\net45\Castle.Core.dll</HintPath>
</Reference>
<Reference Include="Castle.Windsor, Version=4.0.0.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc, processorArchitecture=MSIL">
<HintPath>..\..\packages\Castle.Windsor.4.1.1\lib\net45\Castle.Windsor.dll</HintPath>
</Reference>
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.ComponentModel.DataAnnotations" />
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Core" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="System.Runtime.Remoting" />
<Reference Include="System.Web.Extensions" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Drawing" />
<Reference Include="System.Web" />
<Reference Include="System.Xml" />
<Reference Include="System.Configuration" />
<Reference Include="System.Web.Services" />
<Reference Include="System.EnterpriseServices" />
<Reference Include="System.Web.DynamicData" />
<Reference Include="System.Web.Entity" />
<Reference Include="System.Web.ApplicationServices" />
<Reference Include="Microsoft.Web.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<Private>True</Private>
<HintPath>..\..\packages\Microsoft.Web.Infrastructure.1.0.0.0\lib\net40\Microsoft.Web.Infrastructure.dll</HintPath>
</Reference>
<Reference Include="AspNet.ScriptManager.bootstrap">
<HintPath>..\..\packages\AspNet.ScriptManager.bootstrap.3.3.7\lib\net45\AspNet.ScriptManager.bootstrap.dll</HintPath>
</Reference>
<Reference Include="AspNet.ScriptManager.jQuery">
<HintPath>..\..\packages\AspNet.ScriptManager.jQuery.3.3.1\lib\net45\AspNet.ScriptManager.jQuery.dll</HintPath>
</Reference>
<Reference Include="Microsoft.ScriptManager.MSAjax">
<HintPath>..\..\packages\Microsoft.AspNet.ScriptManager.MSAjax.5.0.0\lib\net45\Microsoft.ScriptManager.MSAjax.dll</HintPath>
</Reference>
<Reference Include="Microsoft.ScriptManager.WebForms">
<HintPath>..\..\packages\Microsoft.AspNet.ScriptManager.WebForms.5.0.0\lib\net45\Microsoft.ScriptManager.WebForms.dll</HintPath>
</Reference>
<Reference Include="System.Web.Optimization, Version=1.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
<HintPath>..\..\packages\Microsoft.AspNet.Web.Optimization.1.1.3\lib\net40\System.Web.Optimization.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json">
<HintPath>..\..\packages\Newtonsoft.Json.11.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="WebGrease">
<Private>True</Private>
<HintPath>..\..\packages\WebGrease.1.6.0\lib\WebGrease.dll</HintPath>
</Reference>
<Reference Include="Antlr3.Runtime">
<Private>True</Private>
<HintPath>..\..\packages\Antlr.3.5.0.2\lib\Antlr3.Runtime.dll</HintPath>
</Reference>
<Reference Include="Microsoft.AspNet.Web.Optimization.WebForms">
<Private>True</Private>
<HintPath>..\..\packages\Microsoft.AspNet.Web.Optimization.WebForms.1.1.3\lib\net45\Microsoft.AspNet.Web.Optimization.WebForms.dll</HintPath>
</Reference>
<Reference Include="Microsoft.AspNet.FriendlyUrls">
<HintPath>..\..\packages\Microsoft.AspNet.FriendlyUrls.Core.1.0.2\lib\net45\Microsoft.AspNet.FriendlyUrls.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Reference Include="System.Diagnostics.DiagnosticSource">
<HintPath>..\..\packages\System.Diagnostics.DiagnosticSource.4.4.1\lib\net46\System.Diagnostics.DiagnosticSource.dll</HintPath>
</Reference>
<Reference Include="Microsoft.AspNet.TelemetryCorrelation">
<HintPath>..\..\packages\Microsoft.AspNet.TelemetryCorrelation.1.0.0\lib\net45\Microsoft.AspNet.TelemetryCorrelation.dll</HintPath>
</Reference>
<Reference Include="Microsoft.CodeDom.Providers.DotNetCompilerPlatform">
<HintPath>..\..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.2.0.0\lib\net45\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Content Include="Default.aspx" />
<Content Include="Global.asax" />
<Content Include="Web.config" />
</ItemGroup>
<ItemGroup>
<Compile Include="Default.aspx.cs">
<DependentUpon>Default.aspx</DependentUpon>
<SubType>ASPXCodeBehind</SubType>
</Compile>
<Compile Include="Default.aspx.designer.cs">
<DependentUpon>Default.aspx</DependentUpon>
</Compile>
<Compile Include="Global.asax.cs">
<DependentUpon>Global.asax</DependentUpon>
</Compile>
<Compile Include="IMyDependecy.cs" />
<Compile Include="MyDependency.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Havit.CastleWindsor.WebForms\Havit.CastleWindsor.WebForms.csproj">
<Project>{328ec034-9dc8-4d18-b889-be91e0f1f768}</Project>
<Name>Havit.CastleWindsor.WebForms</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
<None Include="Web.Debug.config">
<DependentUpon>Web.config</DependentUpon>
</None>
<None Include="Web.Release.config">
<DependentUpon>Web.config</DependentUpon>
</None>
</ItemGroup>
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
</PropertyGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Import Project="$(VSToolsPath)\WebApplications\Microsoft.WebApplication.targets" Condition="'$(VSToolsPath)' != ''" />
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" Condition="false" />
<ProjectExtensions>
<VisualStudio>
<FlavorProperties GUID="{349c5851-65df-11da-9384-00065b846f21}">
<WebProjectProperties>
<UseIIS>True</UseIIS>
<AutoAssignPort>True</AutoAssignPort>
<DevelopmentServerPort>23853</DevelopmentServerPort>
<DevelopmentServerVPath>/</DevelopmentServerVPath>
<IISUrl>http://localhost:23853/</IISUrl>
<NTLMAuthentication>False</NTLMAuthentication>
<UseCustomServer>False</UseCustomServer>
<CustomServerUrl>
</CustomServerUrl>
<SaveServerSettingsInUserFile>False</SaveServerSettingsInUserFile>
</WebProjectProperties>
</FlavorProperties>
</VisualStudio>
</ProjectExtensions>
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.2.0.0\build\net46\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.props')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.2.0.0\build\net46\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.props'))" />
</Target>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
7 changes: 7 additions & 0 deletions src/Havit.CastleWindsor.WebForms.Example/IMyDependecy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Havit.CastleWindsor.WebForms.Example
{
public interface IMyDependecy
{
string MyMethod();
}
}
10 changes: 10 additions & 0 deletions src/Havit.CastleWindsor.WebForms.Example/MyDependency.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Havit.CastleWindsor.WebForms.Example
{
public class MyDependency : IMyDependecy
{
public string MyMethod()
{
return "Hello from dependency!";
}
}
}
Loading