├── .gitattributes ├── .gitignore ├── .travis.yml ├── Hangfire.StructureMap.sln ├── Hangfire.StructureMap.sln.DotSettings ├── LICENSE ├── README.md ├── appveyor.yml ├── build.cmd ├── build.sh ├── src └── Hangfire.StructureMap │ ├── BootstrapperConfigurationExtensions.cs │ ├── GlobalConfigurationExtensions.cs │ ├── Hangfire.StructureMap.csproj │ ├── StructureMapJobActivator.cs │ └── StructureMapJobActivatorScope.cs └── test └── Hangfire.StructureMap.Test ├── Hangfire.StructureMap.Test.csproj ├── StructureMapJobActivatorTest.cs └── TestJob.cs /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | *.sln merge=union 7 | *.csproj merge=union 8 | *.vbproj merge=union 9 | *.fsproj merge=union 10 | *.dbproj merge=union 11 | 12 | # Standard to msysgit 13 | *.doc diff=astextplain 14 | *.DOC diff=astextplain 15 | *.docx diff=astextplain 16 | *.DOCX diff=astextplain 17 | *.dot diff=astextplain 18 | *.DOT diff=astextplain 19 | *.pdf diff=astextplain 20 | *.PDF diff=astextplain 21 | *.rtf diff=astextplain 22 | *.RTF diff=astextplain 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | 4 | project.lock.json 5 | artifacts/ 6 | 7 | # User-specific files 8 | *.suo 9 | *.user 10 | *.sln.docstates 11 | 12 | # Build results 13 | [Dd]ebug/ 14 | [Dd]ebugPublic/ 15 | [Rr]elease/ 16 | [Rr]eleases/ 17 | x64/ 18 | x86/ 19 | bld/ 20 | [Bb]in/ 21 | [Oo]bj/ 22 | 23 | # Roslyn cache directories 24 | *.ide/ 25 | .idea/ 26 | .vs 27 | .vscode 28 | 29 | *_i.c 30 | *_p.c 31 | *_i.h 32 | *.ilk 33 | *.meta 34 | *.obj 35 | *.pch 36 | *.pdb 37 | *.pgc 38 | *.pgd 39 | *.rsp 40 | *.sbr 41 | *.tlb 42 | *.tli 43 | *.tlh 44 | *.tmp 45 | *.tmp_proj 46 | *.log 47 | *.vspscc 48 | *.vssscc 49 | .builds 50 | *.pidb 51 | *.svclog 52 | *.scc 53 | *.srl 54 | 55 | # Visual Studio profiler 56 | *.psess 57 | *.vsp 58 | *.vspx 59 | 60 | # ReSharper is a .NET coding add-in 61 | _ReSharper*/ 62 | *.[Rr]e[Ss]harper 63 | *.DotSettings.user 64 | 65 | # Click-Once directory 66 | publish/ 67 | 68 | # Publish Web Output 69 | *.[Pp]ublish.xml 70 | *.azurePubxml 71 | # TODO: Comment the next line if you want to checkin your web deploy settings 72 | # but database connection strings (with potential passwords) will be unencrypted 73 | *.pubxml 74 | *.publishproj 75 | 76 | # NuGet Packages 77 | *.nupkg 78 | # The packages folder can be ignored because of Package Restore 79 | **/packages/* 80 | # except build/, which is used as an MSBuild target. 81 | !**/packages/build/ 82 | # If using the old MSBuild-Integrated Package Restore, uncomment this: 83 | #!**/packages/repositories.config 84 | 85 | # Others 86 | sql/ 87 | *.Cache 88 | ClientBin/ 89 | [Ss]tyle[Cc]op.* 90 | ~$* 91 | *~ 92 | *.dbmdl 93 | *.dbproj.schemaview 94 | *.pfx 95 | *.publishsettings 96 | node_modules/ 97 | 98 | # Backup & report files from converting an old project file 99 | # to a newer Visual Studio version. Backup files are not needed, 100 | # because we have git ;-) 101 | _UpgradeReport_Files/ 102 | Backup*/ 103 | UpgradeLog*.XML 104 | UpgradeLog*.htm 105 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: csharp 2 | sudo: required 3 | dist: bionic 4 | mono: none 5 | dotnet: 5.0.17 6 | 7 | install: 8 | - export DOTNET_CLI_TELEMETRY_OPTOUT=1 9 | - export DOTNET_SKIP_FIRST_TIME_EXPERIENCE=1 10 | 11 | before_script: 12 | - chmod a+x ./build.sh 13 | 14 | script: 15 | - ./build.sh 16 | -------------------------------------------------------------------------------- /Hangfire.StructureMap.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 15 4 | VisualStudioVersion = 15.0.27130.2010 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Hangfire.StructureMap", "src\Hangfire.StructureMap\Hangfire.StructureMap.csproj", "{8E00108B-C5C7-49DC-8C58-6CB6533AB701}" 7 | EndProject 8 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Hangfire.StructureMap.Test", "test\Hangfire.StructureMap.Test\Hangfire.StructureMap.Test.csproj", "{A6D48A11-E7CD-44DC-BE39-049DA418B25B}" 9 | EndProject 10 | Global 11 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 12 | Debug|Any CPU = Debug|Any CPU 13 | Release|Any CPU = Release|Any CPU 14 | EndGlobalSection 15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 16 | {8E00108B-C5C7-49DC-8C58-6CB6533AB701}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 17 | {8E00108B-C5C7-49DC-8C58-6CB6533AB701}.Debug|Any CPU.Build.0 = Debug|Any CPU 18 | {8E00108B-C5C7-49DC-8C58-6CB6533AB701}.Release|Any CPU.ActiveCfg = Release|Any CPU 19 | {8E00108B-C5C7-49DC-8C58-6CB6533AB701}.Release|Any CPU.Build.0 = Release|Any CPU 20 | {A6D48A11-E7CD-44DC-BE39-049DA418B25B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 21 | {A6D48A11-E7CD-44DC-BE39-049DA418B25B}.Debug|Any CPU.Build.0 = Debug|Any CPU 22 | {A6D48A11-E7CD-44DC-BE39-049DA418B25B}.Release|Any CPU.ActiveCfg = Release|Any CPU 23 | {A6D48A11-E7CD-44DC-BE39-049DA418B25B}.Release|Any CPU.Build.0 = Release|Any CPU 24 | EndGlobalSection 25 | GlobalSection(SolutionProperties) = preSolution 26 | HideSolutionNode = FALSE 27 | EndGlobalSection 28 | GlobalSection(ExtensibilityGlobals) = postSolution 29 | SolutionGuid = {31BC39AE-4BC6-4271-9780-D18F4ABCA94E} 30 | EndGlobalSection 31 | EndGlobal 32 | -------------------------------------------------------------------------------- /Hangfire.StructureMap.sln.DotSettings: -------------------------------------------------------------------------------- 1 |  2 | 3 3 | <Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /> 4 | <Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /> 5 | <Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /> 6 | <Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /> 7 | <Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /> 8 | <Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /> 9 | <Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" /> 10 | <Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /> 11 | <Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" /> 12 | <Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /> 13 | <Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" /> 14 | <Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /> 15 | <Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /> 16 | <Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /> 17 | <Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" /> 18 | <Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" /> 19 | <Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" /> 20 | <Policy Inspect="True" Prefix="I" Suffix="" Style="AaBb" /> 21 | <Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" /> 22 | <Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" /> 23 | <Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /> 24 | <Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /> 25 | <Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /> 26 | <Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /> 27 | <Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /> 28 | <Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /> 29 | <Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /> 30 | <Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /> 31 | <Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /> 32 | <Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /> 33 | <Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /> 34 | <Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /> 35 | <Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /> 36 | <Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /> 37 | <Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" /> 38 | <Policy Inspect="True" Prefix="T" Suffix="" Style="AaBb" /> 39 | <Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" /> 40 | <Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" /> 41 | <Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" /> 42 | <Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" /> 43 | <Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /> 44 | <Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" /> 45 | <Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" /> 46 | True 47 | C:\Data\Projects\OSS\Hangfire.StructureMap\Hangfire.StructureMap.sln.DotSettings 48 | 49 | True 50 | 1 51 | LIVE_MONITOR 52 | LIVE_MONITOR 53 | DO_NOTHING 54 | LIVE_MONITOR 55 | LIVE_MONITOR 56 | LIVE_MONITOR 57 | LIVE_MONITOR 58 | LIVE_MONITOR 59 | LIVE_MONITOR 60 | LIVE_MONITOR 61 | LIVE_MONITOR 62 | DO_NOTHING 63 | LIVE_MONITOR 64 | True -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, and 10 | distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by the copyright 13 | owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all other entities 16 | that control, are controlled by, or are under common control with that entity. 17 | For the purposes of this definition, "control" means (i) the power, direct or 18 | indirect, to cause the direction or management of such entity, whether by 19 | contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the 20 | outstanding shares, or (iii) beneficial ownership of such entity. 21 | 22 | "You" (or "Your") shall mean an individual or Legal Entity exercising 23 | permissions granted by this License. 24 | 25 | "Source" form shall mean the preferred form for making modifications, including 26 | but not limited to software source code, documentation source, and configuration 27 | files. 28 | 29 | "Object" form shall mean any form resulting from mechanical transformation or 30 | translation of a Source form, including but not limited to compiled object code, 31 | generated documentation, and conversions to other media types. 32 | 33 | "Work" shall mean the work of authorship, whether in Source or Object form, made 34 | available under the License, as indicated by a copyright notice that is included 35 | in or attached to the work (an example is provided in the Appendix below). 36 | 37 | "Derivative Works" shall mean any work, whether in Source or Object form, that 38 | is based on (or derived from) the Work and for which the editorial revisions, 39 | annotations, elaborations, or other modifications represent, as a whole, an 40 | original work of authorship. For the purposes of this License, Derivative Works 41 | shall not include works that remain separable from, or merely link (or bind by 42 | name) to the interfaces of, the Work and Derivative Works thereof. 43 | 44 | "Contribution" shall mean any work of authorship, including the original version 45 | of the Work and any modifications or additions to that Work or Derivative Works 46 | thereof, that is intentionally submitted to Licensor for inclusion in the Work 47 | by the copyright owner or by an individual or Legal Entity authorized to submit 48 | on behalf of the copyright owner. For the purposes of this definition, 49 | "submitted" means any form of electronic, verbal, or written communication sent 50 | to the Licensor or its representatives, including but not limited to 51 | communication on electronic mailing lists, source code control systems, and 52 | issue tracking systems that are managed by, or on behalf of, the Licensor for 53 | the purpose of discussing and improving the Work, but excluding communication 54 | that is conspicuously marked or otherwise designated in writing by the copyright 55 | owner as "Not a Contribution." 56 | 57 | "Contributor" shall mean Licensor and any individual or Legal Entity on behalf 58 | of whom a Contribution has been received by Licensor and subsequently 59 | incorporated within the Work. 60 | 61 | 2. Grant of Copyright License. 62 | 63 | Subject to the terms and conditions of this License, each Contributor hereby 64 | grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, 65 | irrevocable copyright license to reproduce, prepare Derivative Works of, 66 | publicly display, publicly perform, sublicense, and distribute the Work and such 67 | Derivative Works in Source or Object form. 68 | 69 | 3. Grant of Patent License. 70 | 71 | Subject to the terms and conditions of this License, each Contributor hereby 72 | grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, 73 | irrevocable (except as stated in this section) patent license to make, have 74 | made, use, offer to sell, sell, import, and otherwise transfer the Work, where 75 | such license applies only to those patent claims licensable by such Contributor 76 | that are necessarily infringed by their Contribution(s) alone or by combination 77 | of their Contribution(s) with the Work to which such Contribution(s) was 78 | submitted. If You institute patent litigation against any entity (including a 79 | cross-claim or counterclaim in a lawsuit) alleging that the Work or a 80 | Contribution incorporated within the Work constitutes direct or contributory 81 | patent infringement, then any patent licenses granted to You under this License 82 | for that Work shall terminate as of the date such litigation is filed. 83 | 84 | 4. Redistribution. 85 | 86 | You may reproduce and distribute copies of the Work or Derivative Works thereof 87 | in any medium, with or without modifications, and in Source or Object form, 88 | provided that You meet the following conditions: 89 | 90 | You must give any other recipients of the Work or Derivative Works a copy of 91 | this License; and 92 | You must cause any modified files to carry prominent notices stating that You 93 | changed the files; and 94 | You must retain, in the Source form of any Derivative Works that You distribute, 95 | all copyright, patent, trademark, and attribution notices from the Source form 96 | of the Work, excluding those notices that do not pertain to any part of the 97 | Derivative Works; and 98 | If the Work includes a "NOTICE" text file as part of its distribution, then any 99 | Derivative Works that You distribute must include a readable copy of the 100 | attribution notices contained within such NOTICE file, excluding those notices 101 | that do not pertain to any part of the Derivative Works, in at least one of the 102 | following places: within a NOTICE text file distributed as part of the 103 | Derivative Works; within the Source form or documentation, if provided along 104 | with the Derivative Works; or, within a display generated by the Derivative 105 | Works, if and wherever such third-party notices normally appear. The contents of 106 | the NOTICE file are for informational purposes only and do not modify the 107 | License. You may add Your own attribution notices within Derivative Works that 108 | You distribute, alongside or as an addendum to the NOTICE text from the Work, 109 | provided that such additional attribution notices cannot be construed as 110 | modifying the License. 111 | You may add Your own copyright statement to Your modifications and may provide 112 | additional or different license terms and conditions for use, reproduction, or 113 | distribution of Your modifications, or for any such Derivative Works as a whole, 114 | provided Your use, reproduction, and distribution of the Work otherwise complies 115 | with the conditions stated in this License. 116 | 117 | 5. Submission of Contributions. 118 | 119 | Unless You explicitly state otherwise, any Contribution intentionally submitted 120 | for inclusion in the Work by You to the Licensor shall be under the terms and 121 | conditions of this License, without any additional terms or conditions. 122 | Notwithstanding the above, nothing herein shall supersede or modify the terms of 123 | any separate license agreement you may have executed with Licensor regarding 124 | such Contributions. 125 | 126 | 6. Trademarks. 127 | 128 | This License does not grant permission to use the trade names, trademarks, 129 | service marks, or product names of the Licensor, except as required for 130 | reasonable and customary use in describing the origin of the Work and 131 | reproducing the content of the NOTICE file. 132 | 133 | 7. Disclaimer of Warranty. 134 | 135 | Unless required by applicable law or agreed to in writing, Licensor provides the 136 | Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, 137 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, 138 | including, without limitation, any warranties or conditions of TITLE, 139 | NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are 140 | solely responsible for determining the appropriateness of using or 141 | redistributing the Work and assume any risks associated with Your exercise of 142 | permissions under this License. 143 | 144 | 8. Limitation of Liability. 145 | 146 | In no event and under no legal theory, whether in tort (including negligence), 147 | contract, or otherwise, unless required by applicable law (such as deliberate 148 | and grossly negligent acts) or agreed to in writing, shall any Contributor be 149 | liable to You for damages, including any direct, indirect, special, incidental, 150 | or consequential damages of any character arising as a result of this License or 151 | out of the use or inability to use the Work (including but not limited to 152 | damages for loss of goodwill, work stoppage, computer failure or malfunction, or 153 | any and all other commercial damages or losses), even if such Contributor has 154 | been advised of the possibility of such damages. 155 | 156 | 9. Accepting Warranty or Additional Liability. 157 | 158 | While redistributing the Work or Derivative Works thereof, You may choose to 159 | offer, and charge a fee for, acceptance of support, warranty, indemnity, or 160 | other liability obligations and/or rights consistent with this License. However, 161 | in accepting such obligations, You may act only on Your own behalf and on Your 162 | sole responsibility, not on behalf of any other Contributor, and only if You 163 | agree to indemnify, defend, and hold each Contributor harmless for any liability 164 | incurred by, or claims asserted against, such Contributor by reason of your 165 | accepting any such warranty or additional liability. 166 | 167 | END OF TERMS AND CONDITIONS 168 | 169 | APPENDIX: How to apply the Apache License to your work 170 | 171 | To apply the Apache License to your work, attach the following boilerplate 172 | notice, with the fields enclosed by brackets "[]" replaced with your own 173 | identifying information. (Don't include the brackets!) The text should be 174 | enclosed in the appropriate comment syntax for the file format. We also 175 | recommend that a file or class name and description of purpose be included on 176 | the same "printed page" as the copyright notice for easier identification within 177 | third-party archives. 178 | 179 | Copyright [yyyy] [name of copyright owner] 180 | 181 | Licensed under the Apache License, Version 2.0 (the "License"); 182 | you may not use this file except in compliance with the License. 183 | You may obtain a copy of the License at 184 | 185 | http://www.apache.org/licenses/LICENSE-2.0 186 | 187 | Unless required by applicable law or agreed to in writing, software 188 | distributed under the License is distributed on an "AS IS" BASIS, 189 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 190 | See the License for the specific language governing permissions and 191 | limitations under the License. 192 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Hangfire.StructureMap 2 | ===================== 3 | 4 | [![Windows Build Status](https://img.shields.io/appveyor/ci/cocowalla/hangfire-structuremap.svg?label=Windows%20Build)](https://ci.appveyor.com/project/cocowalla/hangfire-structuremap) 5 | [![Linux Build status](https://img.shields.io/travis/cocowalla/Hangfire.StructureMap.svg?label=Linux%20Build)](https://travis-ci.org/cocowalla/Hangfire.StructureMap) 6 | [![NuGet](https://img.shields.io/nuget/v/Hangfire.StructureMap.svg)](https://www.nuget.org/packages/Hangfire.StructureMap) 7 | 8 | This package provides [StructureMap](http://structuremap.github.io/) support for [Hangfire](http://hangfire.io), allowing nested StructureMap containers to resolve job type instances and their dependencies, and to manage the lifetime of resolved instances. 9 | 10 | Getting started 11 | --------------- 12 | 13 | Install the [Hangfire.StructureMap](https://www.nuget.org/packages/Hangfire.StructureMap) package from NuGet: 14 | 15 | ```powershell 16 | Install-Package Hangfire.StructureMap 17 | ``` 18 | 19 | To configure Hangfire to use StructureMap, configure your container and call the `IGlobalConfiguration` extension method, `UseStructureMapActivator`: 20 | 21 | ```csharp 22 | var container = new Container(); 23 | // container.Configure... 24 | 25 | GlobalConfiguration.Configuration.UseStructureMapActivator(container); 26 | ``` 27 | 28 | After configuration, when jobs are started a StructureMap-based implementation of the `JobActivator` class is used to resolve job type instances and all of their dependencies. 29 | 30 | Object Lifecycles 31 | ----------------- 32 | 33 | *Hangfire.StructureMap* doesn't rely on a specific object lifecycle - you can configure your dependencies as `Singleton`, `ContainerScoped`, `Transient`, `AlwaysUnique` or `ThreadLocal` as normal. 34 | 35 | *Hangfire.StructureMap* creates a [*nested container*](http://structuremap.github.io/the-container/nested-containers/) for each job execution, so using `ContainerScoped` will scope dependency lifetimes to that of the job. 36 | 37 | ```csharp 38 | container.For().ContainerScoped().Use(); 39 | ``` 40 | 41 | The nested container is disposed when jobs ends, and all dependencies that implement the `IDisposable` interface are also automatically disposed (`Singleton` scoped instances are of course an exception). 42 | -------------------------------------------------------------------------------- /appveyor.yml: -------------------------------------------------------------------------------- 1 | version: '{build}' 2 | image: Visual Studio 2019 3 | configuration: Release 4 | platform: Any CPU 5 | skip_tags: true 6 | 7 | environment: 8 | DOTNET_CLI_TELEMETRY_OPTOUT: true 9 | DOTNET_SKIP_FIRST_TIME_EXPERIENCE: true 10 | 11 | build_script: 12 | - cmd: .\build.cmd 13 | 14 | test: off 15 | 16 | artifacts: 17 | - path: src\Hangfire.StructureMap\**\Release\Hangfire.StructureMap.*.nupkg 18 | -------------------------------------------------------------------------------- /build.cmd: -------------------------------------------------------------------------------- 1 | set START_DIR=%cd% 2 | 3 | dotnet restore .\Hangfire.StructureMap.sln 4 | dotnet build .\src\Hangfire.StructureMap\Hangfire.StructureMap.csproj --configuration Release 5 | 6 | dotnet test 7 | 8 | cd %START_DIR% 9 | 10 | dotnet pack .\src\Hangfire.StructureMap -c Release 11 | -------------------------------------------------------------------------------- /build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | set -ev 3 | 4 | dotnet restore ./Hangfire.StructureMap.sln 5 | dotnet build ./src/Hangfire.StructureMap/Hangfire.StructureMap.csproj --configuration Release 6 | 7 | dotnet test 8 | 9 | cd ${TRAVIS_BUILD_DIR} 10 | -------------------------------------------------------------------------------- /src/Hangfire.StructureMap/BootstrapperConfigurationExtensions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using StructureMap; 3 | 4 | namespace Hangfire.StructureMap 5 | { 6 | #if NET452 7 | /// 8 | /// Bootstrapper extensions for StructureMap job activation 9 | /// 10 | public static class BootstrapperConfigurationExtensions 11 | { 12 | /// 13 | /// Use the specified StructureMap container to create a for resolving job dependencies 14 | /// 15 | /// Bootstrapper configuration 16 | /// Container used to create nested containers, which will in turn build job dependencies 17 | /// during the activation process 18 | /// 19 | /// , 20 | [Obsolete("Please use `GlobalConfiguration.Configuration.UseStructureMapActivator` method instead. Will be removed in version 2.0.0.")] 21 | public static void UseStructureMapActivator(this IBootstrapperConfiguration configuration, IContainer container) 22 | { 23 | if (configuration == null) throw new ArgumentNullException(nameof(configuration)); 24 | 25 | configuration.UseActivator(new StructureMapJobActivator(container)); 26 | } 27 | } 28 | #endif 29 | } 30 | -------------------------------------------------------------------------------- /src/Hangfire.StructureMap/GlobalConfigurationExtensions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using StructureMap; 3 | using Hangfire.Annotations; 4 | 5 | namespace Hangfire.StructureMap 6 | { 7 | /// 8 | /// Global Configuration extensions for StructureMap job activation 9 | /// 10 | public static class GlobalConfigurationExtensions 11 | { 12 | /// 13 | /// Use the specified StructureMap container to create a for resolving job dependencies 14 | /// 15 | /// Global configuration 16 | /// Container used to create nested containers, which will in turn build job dependencies 17 | /// during the activation process 18 | /// 19 | /// An instance of , configured to use StructureMap 20 | /// to resolve job dependencies 21 | /// 22 | /// , 23 | public static IGlobalConfiguration UseStructureMapActivator([NotNull] this IGlobalConfiguration configuration, 24 | [NotNull] IContainer container) 25 | { 26 | if (configuration == null) throw new ArgumentNullException(nameof(configuration)); 27 | if (container == null) throw new ArgumentNullException(nameof(container)); 28 | 29 | return configuration.UseActivator(new StructureMapJobActivator(container)); 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/Hangfire.StructureMap/Hangfire.StructureMap.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | StructureMap IoC container support for Hangfire. Supports both .NET Framework and .NET Standard. 5 | 2.0.2 6 | Colin Anderson 7 | Copyright © Colin Anderson 2018 8 | net462;netstandard2.0;netstandard2.1;net5.0 9 | netstandard2.0;netstandard2.1;net5.0 10 | latest 11 | true 12 | Hangfire.StructureMap 13 | Hangfire.StructureMap 14 | Hangfire.StructureMap 15 | hangfire;background;tasks;jobs;scheduler 16 | https://github.com/cocowalla/Hangfire.StructureMap 17 | Apache-2.0 18 | https://github.com/cocowalla/Hangfire.StructureMap 19 | git 20 | true 21 | false 22 | true 23 | true 24 | snupkg 25 | 26 | 27 | 28 | 29 | all 30 | runtime; build; native; contentfiles; analyzers 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /src/Hangfire.StructureMap/StructureMapJobActivator.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using StructureMap; 3 | 4 | namespace Hangfire.StructureMap 5 | { 6 | /// 7 | /// StructureMap Job Activator - builds job dependencies using nested StructureMap containers 8 | /// 9 | public class StructureMapJobActivator : JobActivator 10 | { 11 | private readonly IContainer container; 12 | 13 | /// 14 | /// Initialize a new instance of with a StructureMap container 15 | /// 16 | /// Container used to create nested containers, which will in turn build job dependencies 17 | /// during the activation process 18 | /// 19 | public StructureMapJobActivator(IContainer container) 20 | { 21 | this.container = container ?? throw new ArgumentNullException(nameof(container)); 22 | } 23 | 24 | /// 25 | /// 26 | /// Activate a job using the parent container 27 | /// 28 | /// An activated job of type 29 | public override object ActivateJob(Type jobType) 30 | { 31 | return this.container.GetInstance(jobType); 32 | } 33 | 34 | /// 35 | /// 36 | /// Begin a new job activation scope using a nested container 37 | /// 38 | /// Job activator context 39 | /// A new job activation scope 40 | public override JobActivatorScope BeginScope(JobActivatorContext context) 41 | { 42 | return new StructureMapDependencyScope(this.container.GetNestedContainer()); 43 | } 44 | 45 | #pragma warning disable CS0672 // Member overrides obsolete member 46 | /// 47 | public override JobActivatorScope BeginScope() 48 | #pragma warning restore CS0672 // Member overrides obsolete member 49 | { 50 | return new StructureMapDependencyScope(this.container.GetNestedContainer()); 51 | } 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/Hangfire.StructureMap/StructureMapJobActivatorScope.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using StructureMap; 3 | 4 | namespace Hangfire.StructureMap 5 | { 6 | internal class StructureMapDependencyScope : JobActivatorScope 7 | { 8 | private readonly IContainer container; 9 | 10 | public StructureMapDependencyScope(IContainer container) 11 | { 12 | this.container = container; 13 | } 14 | 15 | public override object Resolve(Type type) 16 | { 17 | return this.container.GetInstance(type); 18 | } 19 | 20 | public override void DisposeScope() 21 | { 22 | this.container.Dispose(); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /test/Hangfire.StructureMap.Test/Hangfire.StructureMap.Test.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | StructureMap IoC container support for Hangfire - Tests 5 | Colin Anderson 6 | Copyright © Colin Anderson 2018 7 | net462;netcoreapp2.1;netcoreapp3.1;net5.0 8 | netcoreapp2.1;netcoreapp3.1;net5.0 9 | latest 10 | false 11 | Hangfire.StructureMap.Test 12 | Hangfire.StructureMap.Test 13 | git 14 | false 15 | false 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | all 35 | runtime; build; native; contentfiles; analyzers 36 | 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /test/Hangfire.StructureMap.Test/StructureMapJobActivatorTest.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using FakeItEasy; 3 | using Shouldly; 4 | using StructureMap; 5 | using Xunit; 6 | 7 | namespace Hangfire.StructureMap.Test 8 | { 9 | public class StructureMapJobActivatorTest 10 | { 11 | private readonly IContainer container; 12 | 13 | public StructureMapJobActivatorTest() 14 | { 15 | this.container = new Container(); 16 | } 17 | 18 | [Fact] 19 | public void Ctor_Should_Throw_When_Container_Is_Null() 20 | { 21 | // ReSharper disable once ObjectCreationAsStatement 22 | Should.Throw(() => new StructureMapJobActivator(null)); 23 | } 24 | 25 | [Fact] 26 | public void Class_Is_Based_On_JobActivator() 27 | { 28 | var activator = CreateActivator(); 29 | 30 | // ReSharper disable once IsExpressionAlwaysTrue 31 | var isJobActivator = activator is JobActivator; 32 | isJobActivator.ShouldBeTrue(); 33 | } 34 | 35 | [Fact] 36 | public void ActivateJob_Resolves_Instance_Using_StructureMap() 37 | { 38 | var dependency = new BackgroundJobDependency(); 39 | this.container.Inject(dependency); 40 | var activator = CreateActivator(); 41 | 42 | var result = activator.ActivateJob(typeof(BackgroundJobDependency)); 43 | result.ShouldBe(dependency); 44 | } 45 | 46 | [Fact] 47 | public void Container_Scoped_Instance_Is_Disposed_When_Job_Scope_Is_Disposed() 48 | { 49 | this.container.Configure(c => c.For().ContainerScoped()); 50 | 51 | BackgroundJobDependency disposable; 52 | using (var scope = BeginJobScope()) 53 | { 54 | disposable = (BackgroundJobDependency)scope.Resolve(typeof(BackgroundJobDependency)); 55 | 56 | disposable.Disposed.ShouldBeFalse(); 57 | } 58 | 59 | // Now the scope is disposed, dependencies should be too 60 | disposable.Disposed.ShouldBeTrue(); 61 | } 62 | 63 | [Fact] 64 | public void Singleton_Scoped_Instance_Is_Not_Disposed_When_Job_Scope_Is_Disposed() 65 | { 66 | var disposable = new BackgroundJobDependency(); 67 | this.container.Configure(c => c.For().Singleton().Use(disposable)); 68 | 69 | using (var scope = BeginJobScope()) 70 | { 71 | var instance = scope.Resolve(typeof(BackgroundJobDependency)) as BackgroundJobDependency; 72 | 73 | instance.ShouldBe(disposable); 74 | instance.Disposed.ShouldBeFalse(); 75 | } 76 | 77 | // Singletons should live on after the scope is disposed 78 | disposable.Disposed.ShouldBeFalse(); 79 | } 80 | 81 | [Fact] 82 | public void Transient_Scoped_Instance_Is_Disposed_When_Job_Scope_Is_Disposed() 83 | { 84 | this.container.Configure(c => c.For().Use(() => new BackgroundJobDependency())); 85 | 86 | BackgroundJobDependency disposable; 87 | using (var scope = BeginJobScope()) 88 | { 89 | disposable = scope.Resolve(typeof(BackgroundJobDependency)) as BackgroundJobDependency; 90 | 91 | disposable.ShouldNotBeNull(); 92 | disposable.Disposed.ShouldBeFalse(); 93 | } 94 | 95 | // Now the scope is disposed, dependencies should be too 96 | disposable.Disposed.ShouldBeTrue(); 97 | } 98 | 99 | [Fact] 100 | public void AlwaysUnique_Scoped_Instance_Is_Disposed_When_Job_Scope_Is_Disposed() 101 | { 102 | this.container.Configure(c => c.For().Use(() => new BackgroundJobDependency()).AlwaysUnique()); 103 | 104 | BackgroundJobDependency disposable; 105 | using (var scope = BeginJobScope()) 106 | { 107 | disposable = scope.Resolve(typeof(BackgroundJobDependency)) as BackgroundJobDependency; 108 | 109 | disposable.ShouldNotBeNull(); 110 | disposable.Disposed.ShouldBeFalse(); 111 | } 112 | 113 | // Now the scope is disposed, dependencies should be too 114 | disposable.Disposed.ShouldBeTrue(); 115 | } 116 | 117 | /// 118 | /// Injecting an existing object into the Container makes it a de facto singleton (or at least, it's up to 119 | /// the injector to manage its lifecycle) 120 | /// 121 | /// 122 | [Fact] 123 | public void Implicitly_Singleton_Scoped_Instance_Is_Not_Disposed_When_Job_Scope_Is_Disposed() 124 | { 125 | var existingInstance = new BackgroundJobDependency(); 126 | this.container.Configure(c => c.For().Use(existingInstance)); 127 | 128 | using (var scope = BeginJobScope()) 129 | { 130 | var disposable = scope.Resolve(typeof(BackgroundJobDependency)) as BackgroundJobDependency; 131 | 132 | disposable.ShouldBe(existingInstance); 133 | disposable.Disposed.ShouldBeFalse(); 134 | } 135 | 136 | existingInstance.Disposed.ShouldBeFalse(); 137 | } 138 | 139 | [Fact] 140 | public void Container_Scoped_Instances_Are_Not_Reused_Between_Different_Job_Scopes() 141 | { 142 | this.container.Configure(c => c.For().Use(() => new object()).ContainerScoped()); 143 | 144 | object instance1; 145 | using (var scope1 = BeginJobScope()) 146 | { 147 | instance1 = scope1.Resolve(typeof(object)); 148 | } 149 | 150 | object instance2; 151 | using (var scope2 = BeginJobScope()) 152 | { 153 | instance2 = scope2.Resolve(typeof(object)); 154 | } 155 | 156 | instance1.ShouldNotBe(instance2); 157 | } 158 | 159 | [Fact] 160 | public void Container_Scoped_Instance_Is_Reused_Within_Same_Job_Scope() 161 | { 162 | this.container.Configure(c => c.For().ContainerScoped()); 163 | 164 | using (var scope = BeginJobScope()) 165 | { 166 | var instance = (TestJob)scope.Resolve(typeof(TestJob)); 167 | 168 | instance.BackgroundJobDependency.ShouldBe(instance.SameDependencyObject.BackgroundJobDependency); 169 | } 170 | } 171 | 172 | [Fact] 173 | public void AlwaysUnique_Scoped_Instance_Is_Not_Reused_Within_Same_Job_Scope() 174 | { 175 | this.container.Configure(c => c.For().AlwaysUnique()); 176 | 177 | using (var scope = BeginJobScope()) 178 | { 179 | var instance = (TestJob)scope.Resolve(typeof(TestJob)); 180 | 181 | instance.UniqueDependency.ShouldNotBe(instance.SameDependencyObject.UniqueDependency); 182 | } 183 | } 184 | 185 | private JobActivatorScope BeginJobScope() 186 | { 187 | var activator = CreateActivator(); 188 | #if NET452 189 | #pragma warning disable CS0618 // Type or member is obsolete 190 | return activator.BeginScope(); 191 | #pragma warning restore CS0618 // Type or member is obsolete 192 | #else 193 | return activator.BeginScope(null); 194 | #endif 195 | } 196 | 197 | #if NET452 198 | [Fact] 199 | public void Bootstrapper_Use_StructureMapActivator_Passes_Correct_Activator() 200 | { 201 | #pragma warning disable 618 202 | var configuration = A.Fake(); 203 | 204 | configuration.UseStructureMapActivator(this.container); 205 | 206 | A.CallTo(() => configuration.UseActivator(A.That.IsNotNull())).MustHaveHappened(); 207 | #pragma warning restore 618 208 | } 209 | #endif 210 | 211 | private StructureMapJobActivator CreateActivator() => new StructureMapJobActivator(this.container); 212 | } 213 | } 214 | -------------------------------------------------------------------------------- /test/Hangfire.StructureMap.Test/TestJob.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Hangfire.StructureMap.Test 4 | { 5 | public class TestJob 6 | { 7 | public BackgroundJobDependency BackgroundJobDependency { get; } 8 | public UniqueDependency UniqueDependency { get; } 9 | public ObjectDependsOnSameDependency SameDependencyObject { get; } 10 | 11 | public TestJob(BackgroundJobDependency backgroundJobDependency, UniqueDependency uniqueDependency, 12 | ObjectDependsOnSameDependency sameDependencyObject) 13 | { 14 | this.BackgroundJobDependency = backgroundJobDependency; 15 | this.UniqueDependency = uniqueDependency; 16 | this.SameDependencyObject = sameDependencyObject; 17 | } 18 | } 19 | 20 | public class ObjectDependsOnSameDependency 21 | { 22 | public BackgroundJobDependency BackgroundJobDependency { get; } 23 | public UniqueDependency UniqueDependency { get; } 24 | 25 | public ObjectDependsOnSameDependency(BackgroundJobDependency backgroundJobDependency, UniqueDependency uniqueDependency) 26 | { 27 | this.BackgroundJobDependency = backgroundJobDependency; 28 | this.UniqueDependency = uniqueDependency; 29 | } 30 | } 31 | 32 | public class BackgroundJobDependency : IDisposable 33 | { 34 | public bool Disposed { get; private set; } 35 | 36 | public void Dispose() 37 | { 38 | this.Disposed = true; 39 | } 40 | } 41 | 42 | public class UniqueDependency : IDisposable 43 | { 44 | public bool Disposed { get; private set; } 45 | 46 | public void Dispose() 47 | { 48 | this.Disposed = true; 49 | } 50 | } 51 | } 52 | --------------------------------------------------------------------------------