├── .gitattributes
├── .gitignore
├── Azure.Functions.Dependency.Injection.sln
├── FuncInjector
├── .gitignore
├── FuncInjector.csproj
├── Injection
│ ├── InjectAttribute.cs
│ ├── InjectBinding.cs
│ └── InjectBindingProvider.cs
└── RegisterServicesTrigger
│ ├── RegisterServicesTrigger.cs
│ ├── RegisterServicesTriggerAttribute.cs
│ ├── RegisterServicesTriggerBinding.cs
│ ├── RegisterServicesTriggerBindingProvider.cs
│ ├── RegisterServicesTriggerListener.cs
│ └── ReleaseScopedServicesFilter.cs
├── FunctionProject
├── .gitignore
├── FunctionProject.csproj
├── GreeterFunction.cs
├── Properties
│ └── PublishProfiles
│ │ └── mv10blog - Web Deploy.pubxml
└── host.json
├── LICENSE
├── Library
├── .gitignore
├── GreeterService
│ ├── AGreeter.cs
│ ├── CountUpGreeter.cs
│ ├── Counter.cs
│ ├── GreeterInjectionExtensions.cs
│ ├── ICounter.cs
│ └── IGreeter.cs
├── Library.csproj
└── ScopedService
│ ├── GreeterConsumer.cs
│ ├── GreeterConsumerInjectionExtensions.cs
│ └── IGreeterConsumer.cs
└── README.md
/.gitattributes:
--------------------------------------------------------------------------------
1 | ###############################################################################
2 | # Set default behavior to automatically normalize line endings.
3 | ###############################################################################
4 | * text=auto
5 |
6 | ###############################################################################
7 | # Set default behavior for command prompt diff.
8 | #
9 | # This is need for earlier builds of msysgit that does not have it on by
10 | # default for csharp files.
11 | # Note: This is only used by command line
12 | ###############################################################################
13 | #*.cs diff=csharp
14 |
15 | ###############################################################################
16 | # Set the merge driver for project and solution files
17 | #
18 | # Merging from the command prompt will add diff markers to the files if there
19 | # are conflicts (Merging from VS is not affected by the settings below, in VS
20 | # the diff markers are never inserted). Diff markers may cause the following
21 | # file extensions to fail to load in VS. An alternative would be to treat
22 | # these files as binary and thus will always conflict and require user
23 | # intervention with every merge. To do so, just uncomment the entries below
24 | ###############################################################################
25 | #*.sln merge=binary
26 | #*.csproj merge=binary
27 | #*.vbproj merge=binary
28 | #*.vcxproj merge=binary
29 | #*.vcproj merge=binary
30 | #*.dbproj merge=binary
31 | #*.fsproj merge=binary
32 | #*.lsproj merge=binary
33 | #*.wixproj merge=binary
34 | #*.modelproj merge=binary
35 | #*.sqlproj merge=binary
36 | #*.wwaproj merge=binary
37 |
38 | ###############################################################################
39 | # behavior for image files
40 | #
41 | # image files are treated as binary by default.
42 | ###############################################################################
43 | #*.jpg binary
44 | #*.png binary
45 | #*.gif binary
46 |
47 | ###############################################################################
48 | # diff behavior for common document formats
49 | #
50 | # Convert binary document formats to text before diffing them. This feature
51 | # is only available from the command line. Turn it on by uncommenting the
52 | # entries below.
53 | ###############################################################################
54 | #*.doc diff=astextplain
55 | #*.DOC diff=astextplain
56 | #*.docx diff=astextplain
57 | #*.DOCX diff=astextplain
58 | #*.dot diff=astextplain
59 | #*.DOT diff=astextplain
60 | #*.pdf diff=astextplain
61 | #*.PDF diff=astextplain
62 | #*.rtf diff=astextplain
63 | #*.RTF diff=astextplain
64 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | .vs/
2 | bin
3 | obj
--------------------------------------------------------------------------------
/Azure.Functions.Dependency.Injection.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio 15
4 | VisualStudioVersion = 15.0.27130.2027
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FuncInjector", "FuncInjector\FuncInjector.csproj", "{DB5F3085-923D-4E29-99C6-0C7D41344ED8}"
7 | EndProject
8 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FunctionProject", "FunctionProject\FunctionProject.csproj", "{E8649CD1-8192-4477-AF91-C96943DA0154}"
9 | EndProject
10 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Library", "Library\Library.csproj", "{ED8FCE60-0C06-4BBD-AE33-CE53444AE964}"
11 | EndProject
12 | Global
13 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
14 | Debug|Any CPU = Debug|Any CPU
15 | Release|Any CPU = Release|Any CPU
16 | EndGlobalSection
17 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
18 | {DB5F3085-923D-4E29-99C6-0C7D41344ED8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
19 | {DB5F3085-923D-4E29-99C6-0C7D41344ED8}.Debug|Any CPU.Build.0 = Debug|Any CPU
20 | {DB5F3085-923D-4E29-99C6-0C7D41344ED8}.Release|Any CPU.ActiveCfg = Release|Any CPU
21 | {DB5F3085-923D-4E29-99C6-0C7D41344ED8}.Release|Any CPU.Build.0 = Release|Any CPU
22 | {E8649CD1-8192-4477-AF91-C96943DA0154}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
23 | {E8649CD1-8192-4477-AF91-C96943DA0154}.Debug|Any CPU.Build.0 = Debug|Any CPU
24 | {E8649CD1-8192-4477-AF91-C96943DA0154}.Release|Any CPU.ActiveCfg = Release|Any CPU
25 | {E8649CD1-8192-4477-AF91-C96943DA0154}.Release|Any CPU.Build.0 = Release|Any CPU
26 | {ED8FCE60-0C06-4BBD-AE33-CE53444AE964}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
27 | {ED8FCE60-0C06-4BBD-AE33-CE53444AE964}.Debug|Any CPU.Build.0 = Debug|Any CPU
28 | {ED8FCE60-0C06-4BBD-AE33-CE53444AE964}.Release|Any CPU.ActiveCfg = Release|Any CPU
29 | {ED8FCE60-0C06-4BBD-AE33-CE53444AE964}.Release|Any CPU.Build.0 = Release|Any CPU
30 | EndGlobalSection
31 | GlobalSection(SolutionProperties) = preSolution
32 | HideSolutionNode = FALSE
33 | EndGlobalSection
34 | GlobalSection(ExtensibilityGlobals) = postSolution
35 | SolutionGuid = {9DE6AA97-0CD9-479E-A9C2-9112BE8D8FC7}
36 | EndGlobalSection
37 | EndGlobal
38 |
--------------------------------------------------------------------------------
/FuncInjector/.gitignore:
--------------------------------------------------------------------------------
1 | ## Ignore Visual Studio temporary files, build results, and
2 | ## files generated by popular Visual Studio add-ons.
3 |
4 | # Azure Functions localsettings file
5 | local.settings.json
6 |
7 | # User-specific files
8 | *.suo
9 | *.user
10 | *.userosscache
11 | *.sln.docstates
12 |
13 | # User-specific files (MonoDevelop/Xamarin Studio)
14 | *.userprefs
15 |
16 | # Build results
17 | [Dd]ebug/
18 | [Dd]ebugPublic/
19 | [Rr]elease/
20 | [Rr]eleases/
21 | x64/
22 | x86/
23 | bld/
24 | [Bb]in/
25 | [Oo]bj/
26 | [Ll]og/
27 |
28 | # Visual Studio 2015 cache/options directory
29 | .vs/
30 | # Uncomment if you have tasks that create the project's static files in wwwroot
31 | #wwwroot/
32 |
33 | # MSTest test Results
34 | [Tt]est[Rr]esult*/
35 | [Bb]uild[Ll]og.*
36 |
37 | # NUNIT
38 | *.VisualState.xml
39 | TestResult.xml
40 |
41 | # Build Results of an ATL Project
42 | [Dd]ebugPS/
43 | [Rr]eleasePS/
44 | dlldata.c
45 |
46 | # DNX
47 | project.lock.json
48 | project.fragment.lock.json
49 | artifacts/
50 |
51 | *_i.c
52 | *_p.c
53 | *_i.h
54 | *.ilk
55 | *.meta
56 | *.obj
57 | *.pch
58 | *.pdb
59 | *.pgc
60 | *.pgd
61 | *.rsp
62 | *.sbr
63 | *.tlb
64 | *.tli
65 | *.tlh
66 | *.tmp
67 | *.tmp_proj
68 | *.log
69 | *.vspscc
70 | *.vssscc
71 | .builds
72 | *.pidb
73 | *.svclog
74 | *.scc
75 |
76 | # Chutzpah Test files
77 | _Chutzpah*
78 |
79 | # Visual C++ cache files
80 | ipch/
81 | *.aps
82 | *.ncb
83 | *.opendb
84 | *.opensdf
85 | *.sdf
86 | *.cachefile
87 | *.VC.db
88 | *.VC.VC.opendb
89 |
90 | # Visual Studio profiler
91 | *.psess
92 | *.vsp
93 | *.vspx
94 | *.sap
95 |
96 | # TFS 2012 Local Workspace
97 | $tf/
98 |
99 | # Guidance Automation Toolkit
100 | *.gpState
101 |
102 | # ReSharper is a .NET coding add-in
103 | _ReSharper*/
104 | *.[Rr]e[Ss]harper
105 | *.DotSettings.user
106 |
107 | # JustCode is a .NET coding add-in
108 | .JustCode
109 |
110 | # TeamCity is a build add-in
111 | _TeamCity*
112 |
113 | # DotCover is a Code Coverage Tool
114 | *.dotCover
115 |
116 | # NCrunch
117 | _NCrunch_*
118 | .*crunch*.local.xml
119 | nCrunchTemp_*
120 |
121 | # MightyMoose
122 | *.mm.*
123 | AutoTest.Net/
124 |
125 | # Web workbench (sass)
126 | .sass-cache/
127 |
128 | # Installshield output folder
129 | [Ee]xpress/
130 |
131 | # DocProject is a documentation generator add-in
132 | DocProject/buildhelp/
133 | DocProject/Help/*.HxT
134 | DocProject/Help/*.HxC
135 | DocProject/Help/*.hhc
136 | DocProject/Help/*.hhk
137 | DocProject/Help/*.hhp
138 | DocProject/Help/Html2
139 | DocProject/Help/html
140 |
141 | # Click-Once directory
142 | publish/
143 |
144 | # Publish Web Output
145 | *.[Pp]ublish.xml
146 | *.azurePubxml
147 | # TODO: Comment the next line if you want to checkin your web deploy settings
148 | # but database connection strings (with potential passwords) will be unencrypted
149 | #*.pubxml
150 | *.publishproj
151 |
152 | # Microsoft Azure Web App publish settings. Comment the next line if you want to
153 | # checkin your Azure Web App publish settings, but sensitive information contained
154 | # in these scripts will be unencrypted
155 | PublishScripts/
156 |
157 | # NuGet Packages
158 | *.nupkg
159 | # The packages folder can be ignored because of Package Restore
160 | **/packages/*
161 | # except build/, which is used as an MSBuild target.
162 | !**/packages/build/
163 | # Uncomment if necessary however generally it will be regenerated when needed
164 | #!**/packages/repositories.config
165 | # NuGet v3's project.json files produces more ignoreable files
166 | *.nuget.props
167 | *.nuget.targets
168 |
169 | # Microsoft Azure Build Output
170 | csx/
171 | *.build.csdef
172 |
173 | # Microsoft Azure Emulator
174 | ecf/
175 | rcf/
176 |
177 | # Windows Store app package directories and files
178 | AppPackages/
179 | BundleArtifacts/
180 | Package.StoreAssociation.xml
181 | _pkginfo.txt
182 |
183 | # Visual Studio cache files
184 | # files ending in .cache can be ignored
185 | *.[Cc]ache
186 | # but keep track of directories ending in .cache
187 | !*.[Cc]ache/
188 |
189 | # Others
190 | ClientBin/
191 | ~$*
192 | *~
193 | *.dbmdl
194 | *.dbproj.schemaview
195 | *.jfm
196 | *.pfx
197 | *.publishsettings
198 | node_modules/
199 | orleans.codegen.cs
200 |
201 | # Since there are multiple workflows, uncomment next line to ignore bower_components
202 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
203 | #bower_components/
204 |
205 | # RIA/Silverlight projects
206 | Generated_Code/
207 |
208 | # Backup & report files from converting an old project file
209 | # to a newer Visual Studio version. Backup files are not needed,
210 | # because we have git ;-)
211 | _UpgradeReport_Files/
212 | Backup*/
213 | UpgradeLog*.XML
214 | UpgradeLog*.htm
215 |
216 | # SQL Server files
217 | *.mdf
218 | *.ldf
219 |
220 | # Business Intelligence projects
221 | *.rdl.data
222 | *.bim.layout
223 | *.bim_*.settings
224 |
225 | # Microsoft Fakes
226 | FakesAssemblies/
227 |
228 | # GhostDoc plugin setting file
229 | *.GhostDoc.xml
230 |
231 | # Node.js Tools for Visual Studio
232 | .ntvs_analysis.dat
233 |
234 | # Visual Studio 6 build log
235 | *.plg
236 |
237 | # Visual Studio 6 workspace options file
238 | *.opt
239 |
240 | # Visual Studio LightSwitch build output
241 | **/*.HTMLClient/GeneratedArtifacts
242 | **/*.DesktopClient/GeneratedArtifacts
243 | **/*.DesktopClient/ModelManifest.xml
244 | **/*.Server/GeneratedArtifacts
245 | **/*.Server/ModelManifest.xml
246 | _Pvt_Extensions
247 |
248 | # Paket dependency manager
249 | .paket/paket.exe
250 | paket-files/
251 |
252 | # FAKE - F# Make
253 | .fake/
254 |
255 | # JetBrains Rider
256 | .idea/
257 | *.sln.iml
258 |
259 | # CodeRush
260 | .cr/
261 |
262 | # Python Tools for Visual Studio (PTVS)
263 | __pycache__/
264 | *.pyc
--------------------------------------------------------------------------------
/FuncInjector/FuncInjector.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 | net461
4 | true
5 | 1.0.0.0
6 |
7 |
8 | latest
9 |
10 |
11 | latest
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/FuncInjector/Injection/InjectAttribute.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.Azure.WebJobs.Description;
2 | using System;
3 |
4 | namespace FuncInjector
5 | {
6 | ///
7 | /// Declares a dependency on an interface to be resolved at runtime. The name of a
8 | /// RegisterServicesTrigger is optional. If omitted, a service registration trigger
9 | /// named "RegisterServices" must exist.
10 | ///
11 | [Binding]
12 | [AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false)]
13 | public class InjectAttribute : Attribute
14 | {
15 | public string RegisterServicesFunctionName { get; private set; }
16 |
17 | public InjectAttribute(string configFunctionName = "RegisterServices")
18 | {
19 | RegisterServicesFunctionName = configFunctionName;
20 | }
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/FuncInjector/Injection/InjectBinding.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.Azure.WebJobs.Host.Bindings;
2 | using Microsoft.Azure.WebJobs.Host.Protocols;
3 | using Microsoft.Extensions.DependencyInjection;
4 | using System;
5 | using System.Reflection;
6 | using System.Threading.Tasks;
7 |
8 | namespace FuncInjector
9 | {
10 | ///
11 | /// Tracks the Type declared as a dependency and the RegisterServices trigger which
12 | /// prepares the dependency graph for injection. The trigger is stored as the value-provider,
13 | /// so resolving the dependency causes trigger execution.
14 | ///
15 | public class InjectBinding : IBinding
16 | {
17 | private readonly bool isArray =false;
18 | private readonly bool hasDefaultValue = false;
19 | private readonly object defaultValue;
20 |
21 | private readonly Type elementType;
22 | private readonly RegisterServicesTrigger triggerAttribute;
23 | private readonly string triggerFunction;
24 |
25 | ///
26 | ///
27 | ///
28 | ///
29 | ///
30 | public InjectBinding(ParameterInfo parameterInfo, RegisterServicesTrigger regSvcTrigger, string regSvcFunctionName)
31 | {
32 | this.hasDefaultValue = parameterInfo.HasDefaultValue;
33 | this.defaultValue = parameterInfo.DefaultValue;
34 |
35 | this.isArray = parameterInfo.ParameterType.IsArray;
36 | this.elementType = parameterInfo.ParameterType.IsArray ?
37 | parameterInfo.ParameterType.GetElementType() :
38 | parameterInfo.ParameterType.GetType();
39 |
40 | triggerAttribute = regSvcTrigger;
41 | triggerFunction = regSvcFunctionName;
42 | }
43 |
44 | public bool FromAttribute => true;
45 |
46 | public Task BindAsync(object value, ValueBindingContext context)
47 | => Task.FromResult((IValueProvider)new InjectValueProvider(value));
48 |
49 | public async Task BindAsync(BindingContext context)
50 | {
51 | var provider = triggerAttribute.GetServiceProvider(triggerFunction);
52 | var scope = triggerAttribute.Scopes.GetOrAdd(context.FunctionInstanceId, (_) => provider.CreateScope());
53 |
54 |
55 | var services = hasDefaultValue ?
56 | (
57 | isArray ?
58 | BindAsync(
59 | scope.ServiceProvider.GetServices(elementType),
60 | context.ValueContext
61 | )
62 | :
63 | BindAsync(
64 | scope.ServiceProvider.GetService(elementType),
65 | context.ValueContext
66 | )
67 | ) : (
68 | isArray ?
69 | BindAsync(
70 | scope.ServiceProvider.GetServices(elementType),
71 | context.ValueContext
72 | )
73 | :
74 | BindAsync(
75 | scope.ServiceProvider.GetRequiredService(elementType),
76 | context.ValueContext
77 | )
78 | );
79 | return await services;
80 | }
81 |
82 | public ParameterDescriptor ToParameterDescriptor() => new ParameterDescriptor();
83 |
84 | private class InjectValueProvider : IValueProvider
85 | {
86 | private readonly object value;
87 |
88 | public InjectValueProvider(object value) => this.value = value;
89 |
90 | public Type Type => value.GetType();
91 |
92 | public Task