├── src
└── FunctionsV2DiSample.FunctionApp
│ ├── host.json
│ ├── config.json
│ ├── Functions
│ ├── FunctionOptions
│ │ ├── FunctionOptionsBase.cs
│ │ └── GitHubRepositoriesHttpTriggerOptions.cs
│ ├── IGitHubRepositoriesFunction.cs
│ ├── IFunctionFactory.cs
│ ├── IFunction.cs
│ ├── AutofacFunctionFactory.cs
│ ├── CoreFunctionFactory.cs
│ ├── CoreGitHubRepositoriesFunction.cs
│ └── AutofacGitHubRepositoriesFunction.cs
│ ├── Modules
│ ├── Module.cs
│ ├── IModule.cs
│ ├── CoreAppModule.cs
│ └── AutofacAppModule.cs
│ ├── Configs
│ └── GitHub.cs
│ ├── Containers
│ ├── IContainerBuilder.cs
│ └── ContainerBuilder.cs
│ ├── FunctionsV2DiSample.FunctionApp.csproj
│ ├── Extensions
│ └── ConfigurationBinderExtensions.cs
│ ├── CoreGitHubRepositoriesHttpTrigger.cs
│ ├── AutofacGitHubRepositoriesHttpTrigger.cs
│ └── .gitignore
├── .mailmap
├── test
└── FunctionsV2DiSample.FunctionApp.Tests
│ ├── FunctionsV2DiSample.FunctionApp.Tests.csproj
│ ├── Fixtures
│ └── FakeQueryCollection.cs
│ └── CoreGitHubRepositoriesHttpTriggerTests.cs
├── LICENSE
├── README.md
├── Settings.StyleCop
├── AzureFunctionsV2DependencyInjectionSample.sln
└── .gitignore
/src/FunctionsV2DiSample.FunctionApp/host.json:
--------------------------------------------------------------------------------
1 | {
2 | }
--------------------------------------------------------------------------------
/src/FunctionsV2DiSample.FunctionApp/config.json:
--------------------------------------------------------------------------------
1 | {
2 | "github": {
3 | "baseUrl": "https://api.github.com/",
4 | "endpoints": {
5 | "repositories": "{0}/{1}/repos"
6 | }
7 | }
8 | }
--------------------------------------------------------------------------------
/src/FunctionsV2DiSample.FunctionApp/Functions/FunctionOptions/FunctionOptionsBase.cs:
--------------------------------------------------------------------------------
1 | namespace FunctionsV2DiSample.FunctionApp.Functions.FunctionOptions
2 | {
3 | ///
4 | /// This represents a base function options entity. This must be inherited.
5 | ///
6 | public abstract class FunctionOptionsBase
7 | {
8 | }
9 | }
--------------------------------------------------------------------------------
/src/FunctionsV2DiSample.FunctionApp/Functions/IGitHubRepositoriesFunction.cs:
--------------------------------------------------------------------------------
1 | namespace FunctionsV2DiSample.FunctionApp.Functions
2 | {
3 | ///
4 | /// This provides interfaces to the class.
5 | ///
6 | public interface IGitHubRepositoriesFunction : IFunction
7 | {
8 | }
9 | }
--------------------------------------------------------------------------------
/src/FunctionsV2DiSample.FunctionApp/Modules/Module.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.Extensions.DependencyInjection;
2 |
3 | namespace FunctionsV2DiSample.FunctionApp.Modules
4 | {
5 | ///
6 | /// This represents the entity containing a list of dependencies.
7 | ///
8 | public class Module : IModule
9 | {
10 | ///
11 | public virtual void Load(IServiceCollection services)
12 | {
13 | return;
14 | }
15 | }
16 | }
--------------------------------------------------------------------------------
/.mailmap:
--------------------------------------------------------------------------------
1 | #
2 | # This list is used by git-shortlog to fix a few botched name translations
3 | # in the git archive, either because the author's full name was messed up
4 | # and/or not always written the same way, making contributions from the
5 | # same person appearing not to be so.
6 | #
7 | # Reference: https://github.com/git/git/blob/master/.mailmap
8 | #
9 |
10 | Justin Yoo
11 | Justin Yoo
12 | Justin Yoo
13 |
--------------------------------------------------------------------------------
/src/FunctionsV2DiSample.FunctionApp/Modules/IModule.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.Extensions.DependencyInjection;
2 |
3 | namespace FunctionsV2DiSample.FunctionApp.Modules
4 | {
5 | ///
6 | /// This provides interfaces to the class.
7 | ///
8 | public interface IModule
9 | {
10 | ///
11 | /// Loads dependencies to the collection.
12 | ///
13 | /// instance.
14 | void Load(IServiceCollection services);
15 | }
16 | }
--------------------------------------------------------------------------------
/src/FunctionsV2DiSample.FunctionApp/Functions/IFunctionFactory.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.Azure.WebJobs.Host;
2 |
3 | namespace FunctionsV2DiSample.FunctionApp.Functions
4 | {
5 | ///
6 | /// This provides interfaces to the instance.
7 | ///
8 | public interface IFunctionFactory
9 | {
10 | ///
11 | /// Creates a function instance from the IoC container.
12 | ///
13 | /// Type of function.
14 | /// instance.
15 | /// Instance name.
16 | /// Returns the function instance.
17 | TFunction Create(TraceWriter log, string name = null) where TFunction : IFunction;
18 | }
19 | }
--------------------------------------------------------------------------------
/src/FunctionsV2DiSample.FunctionApp/Configs/GitHub.cs:
--------------------------------------------------------------------------------
1 | namespace FunctionsV2DiSample.FunctionApp.Configs
2 | {
3 | ///
4 | /// This represents the config entity for GitHub API.
5 | ///
6 | public class GitHub
7 | {
8 | ///
9 | /// Gets or sets the base URL.
10 | ///
11 | public virtual string BaseUrl { get; set; }
12 |
13 | ///
14 | /// Gets or sets the set of endpoints.
15 | ///
16 | public virtual Endpoints Endpoints { get; set; }
17 | }
18 |
19 | ///
20 | /// This represents the config entity for GitHub API endpoints.
21 | ///
22 | public class Endpoints
23 | {
24 | ///
25 | /// Gets or sets the repository endpoint.
26 | ///
27 | public virtual string Repositories { get; set; }
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/test/FunctionsV2DiSample.FunctionApp.Tests/FunctionsV2DiSample.FunctionApp.Tests.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | netcoreapp2.0
5 |
6 | false
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
--------------------------------------------------------------------------------
/src/FunctionsV2DiSample.FunctionApp/Containers/IContainerBuilder.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | using FunctionsV2DiSample.FunctionApp.Modules;
4 |
5 | namespace FunctionsV2DiSample.FunctionApp.Containers
6 | {
7 | ///
8 | /// This provides interfaces to the class.
9 | ///
10 | public interface IContainerBuilder
11 | {
12 | ///
13 | /// Registers a dependency collection module.
14 | ///
15 | /// instance.
16 | /// Returns instance.
17 | IContainerBuilder RegisterModule(IModule module = null);
18 |
19 | ///
20 | /// Builds the dependency container.
21 | ///
22 | /// Returns instance.
23 | IServiceProvider Build();
24 | }
25 | }
--------------------------------------------------------------------------------
/src/FunctionsV2DiSample.FunctionApp/Functions/IFunction.cs:
--------------------------------------------------------------------------------
1 | using System.Threading.Tasks;
2 |
3 | using FunctionsV2DiSample.FunctionApp.Functions.FunctionOptions;
4 |
5 | using Microsoft.Azure.WebJobs.Host;
6 |
7 | namespace FunctionsV2DiSample.FunctionApp.Functions
8 | {
9 | ///
10 | /// This provides interfaces to functions.
11 | ///
12 | public interface IFunction
13 | {
14 | ///
15 | /// Gets or sets the instance.
16 | ///
17 | TraceWriter Log { get; set; }
18 |
19 | ///
20 | /// Invokes the function.
21 | ///
22 | /// Type of input.
23 | /// Type of output.
24 | /// Input instance.
25 | /// instance.
26 | /// Returns output instance.
27 | Task InvokeAsync(TInput input, FunctionOptionsBase options);
28 | }
29 | }
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2018 Dev Kimchi (https://devkimchi.com)
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/src/FunctionsV2DiSample.FunctionApp/FunctionsV2DiSample.FunctionApp.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 | netstandard2.0
4 | v2
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 | PreserveNewest
15 |
16 |
17 | PreserveNewest
18 | Never
19 |
20 |
21 | PreserveNewest
22 | Never
23 |
24 |
25 |
--------------------------------------------------------------------------------
/src/FunctionsV2DiSample.FunctionApp/Modules/CoreAppModule.cs:
--------------------------------------------------------------------------------
1 | using System.IO;
2 | using System.Net.Http;
3 |
4 | using FunctionsV2DiSample.FunctionApp.Configs;
5 | using FunctionsV2DiSample.FunctionApp.Extensions;
6 | using FunctionsV2DiSample.FunctionApp.Functions;
7 |
8 | using Microsoft.Extensions.Configuration;
9 | using Microsoft.Extensions.DependencyInjection;
10 |
11 | namespace FunctionsV2DiSample.FunctionApp.Modules
12 | {
13 | ///
14 | /// This represents the module entity for dependencies.
15 | ///
16 | public class CoreAppModule : Module
17 | {
18 | ///
19 | public override void Load(IServiceCollection services)
20 | {
21 | var config = new ConfigurationBuilder()
22 | .SetBasePath(Directory.GetCurrentDirectory())
23 | .AddJsonFile("config.json")
24 | .Build();
25 | var github = config.Get("github");
26 |
27 | services.AddSingleton(github);
28 | services.AddSingleton();
29 | services.AddTransient();
30 | }
31 | }
32 | }
--------------------------------------------------------------------------------
/src/FunctionsV2DiSample.FunctionApp/Modules/AutofacAppModule.cs:
--------------------------------------------------------------------------------
1 | //using System.IO;
2 | //using System.Net.Http;
3 |
4 | //using Autofac;
5 |
6 | //using FunctionsV2DiSample.FunctionApp.Configs;
7 | //using FunctionsV2DiSample.FunctionApp.Extensions;
8 | //using FunctionsV2DiSample.FunctionApp.Functions;
9 |
10 | //using Microsoft.Extensions.Configuration;
11 |
12 | //namespace FunctionsV2DiSample.FunctionApp.Modules
13 | //{
14 | // public class AutofacAppModule : Autofac.Module
15 | // {
16 | // protected override void Load(ContainerBuilder builder)
17 | // {
18 | // var config = new ConfigurationBuilder()
19 | // .SetBasePath(Directory.GetCurrentDirectory())
20 | // .AddJsonFile("config.json")
21 | // .Build();
22 | // var github = config.Get("github");
23 | // builder.RegisterInstance(github).As().SingleInstance();
24 |
25 | // var httpClient = new HttpClient();
26 | // builder.RegisterInstance(httpClient).As().SingleInstance();
27 |
28 | // builder.RegisterType().Named("github").InstancePerLifetimeScope();
29 | // }
30 | // }
31 | //}
32 |
--------------------------------------------------------------------------------
/src/FunctionsV2DiSample.FunctionApp/Extensions/ConfigurationBinderExtensions.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | using Microsoft.Extensions.Configuration;
4 |
5 | namespace FunctionsV2DiSample.FunctionApp.Extensions
6 | {
7 | ///
8 | /// This represents the extension entity for class.
9 | ///
10 | public static class ConfigurationBinderExtensions
11 | {
12 | ///
13 | /// Gets the instance from the configuration.
14 | ///
15 | /// Type of instance.
16 | /// instance.
17 | /// Configuration key.
18 | /// Returns the instance from the configuration.
19 | public static T Get(this IConfiguration configuration, string key = null)
20 | {
21 | var instance = Activator.CreateInstance();
22 |
23 | if (string.IsNullOrWhiteSpace(key))
24 | {
25 | configuration.Bind(instance);
26 |
27 | return instance;
28 | }
29 |
30 | configuration.Bind(key, instance);
31 |
32 | return instance;
33 | }
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/src/FunctionsV2DiSample.FunctionApp/Containers/ContainerBuilder.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | using FunctionsV2DiSample.FunctionApp.Modules;
4 |
5 | using Microsoft.Extensions.DependencyInjection;
6 |
7 | namespace FunctionsV2DiSample.FunctionApp.Containers
8 | {
9 | ///
10 | /// This represents the builder entity for IoC container.
11 | ///
12 | public class ContainerBuilder : IContainerBuilder
13 | {
14 | private readonly IServiceCollection _services;
15 |
16 | ///
17 | /// Initializes a new instance of the class.
18 | ///
19 | public ContainerBuilder()
20 | {
21 | this._services = new ServiceCollection();
22 | }
23 |
24 | ///
25 | public IContainerBuilder RegisterModule(IModule module = null)
26 | {
27 | if (module == null)
28 | {
29 | module = new Module();
30 | }
31 |
32 | module.Load(this._services);
33 |
34 | return this;
35 | }
36 |
37 | ///
38 | public IServiceProvider Build()
39 | {
40 | var provider = this._services.BuildServiceProvider();
41 |
42 | return provider;
43 | }
44 | }
45 | }
--------------------------------------------------------------------------------
/src/FunctionsV2DiSample.FunctionApp/Functions/AutofacFunctionFactory.cs:
--------------------------------------------------------------------------------
1 | //using Autofac;
2 |
3 | //using Microsoft.Azure.WebJobs.Host;
4 |
5 | //namespace FunctionsV2DiSample.FunctionApp.Functions
6 | //{
7 | // ///
8 | // /// This represents the factory entity for functions.
9 | // ///
10 | // public class AutofacFunctionFactory : IFunctionFactory
11 | // {
12 | // private readonly IContainer _container;
13 |
14 | // ///
15 | // /// Initializes a new instance of the class.
16 | // ///
17 | // /// instance.
18 | // public AutofacFunctionFactory(Module module = null)
19 | // {
20 | // var builder = new ContainerBuilder();
21 | // builder.RegisterModule(module);
22 |
23 | // this._container = builder.Build();
24 | // }
25 |
26 | // ///
27 | // public TFunction Create(TraceWriter log, string name = null)
28 | // where TFunction : IFunction
29 | // {
30 | // // Resolve the function instance directly from the container.
31 | // var function = this._container.ResolveNamed(name);
32 | // function.Log = log;
33 |
34 | // return function;
35 | // }
36 | // }
37 | //}
--------------------------------------------------------------------------------
/src/FunctionsV2DiSample.FunctionApp/Functions/CoreFunctionFactory.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | using FunctionsV2DiSample.FunctionApp.Containers;
4 | using FunctionsV2DiSample.FunctionApp.Modules;
5 |
6 | using Microsoft.Azure.WebJobs.Host;
7 | using Microsoft.Extensions.DependencyInjection;
8 |
9 | namespace FunctionsV2DiSample.FunctionApp.Functions
10 | {
11 | ///
12 | /// This represents the factory entity for functions.
13 | ///
14 | public class CoreFunctionFactory : IFunctionFactory
15 | {
16 | private readonly IServiceProvider _container;
17 |
18 | ///
19 | /// Initializes a new instance of the class.
20 | ///
21 | /// instance.
22 | public CoreFunctionFactory(IModule module = null)
23 | {
24 | this._container = new ContainerBuilder()
25 | .RegisterModule(module)
26 | .Build();
27 | }
28 |
29 | ///
30 | public TFunction Create(TraceWriter log, string name = null)
31 | where TFunction : IFunction
32 | {
33 | // Resolve the function instance directly from the container.
34 | var function = this._container.GetService();
35 | function.Log = log;
36 |
37 | return function;
38 | }
39 | }
40 | }
--------------------------------------------------------------------------------
/src/FunctionsV2DiSample.FunctionApp/Functions/FunctionOptions/GitHubRepositoriesHttpTriggerOptions.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.AspNetCore.Http;
2 |
3 | namespace FunctionsV2DiSample.FunctionApp.Functions.FunctionOptions
4 | {
5 | ///
6 | /// This represents the options entity for the class.
7 | ///
8 | public class GitHubRepositoriesHttpTriggerOptions : FunctionOptionsBase
9 | {
10 | private readonly HttpRequest _req;
11 |
12 | ///
13 | /// Initializes a new instance of the class.
14 | ///
15 | ///
16 | public GitHubRepositoriesHttpTriggerOptions(HttpRequest req)
17 | {
18 | this._req = req;
19 | }
20 |
21 | ///
22 | /// Gets the repository type - users or organisations.
23 | ///
24 | public string Type => this.GetRepositoryType();
25 |
26 | ///
27 | /// Gets the repository name.
28 | ///
29 | public string Name => this.GetRepositoryName();
30 |
31 | private string GetRepositoryType()
32 | {
33 | var type = this._req.Query["type"];
34 |
35 | return type;
36 | }
37 |
38 | private string GetRepositoryName()
39 | {
40 | var name = this._req.Query["name"];
41 |
42 | return name;
43 | }
44 | }
45 | }
--------------------------------------------------------------------------------
/test/FunctionsV2DiSample.FunctionApp.Tests/Fixtures/FakeQueryCollection.cs:
--------------------------------------------------------------------------------
1 | using System.Collections;
2 | using System.Collections.Generic;
3 |
4 | using Microsoft.AspNetCore.Http;
5 | using Microsoft.Extensions.Primitives;
6 |
7 | namespace FunctionsV2DiSample.FunctionApp.Tests.Fixtures
8 | {
9 | public class FakeQueryCollection : IQueryCollection
10 | {
11 | private readonly Dictionary _values;
12 |
13 | public FakeQueryCollection()
14 | {
15 | this._values = new Dictionary();
16 | }
17 |
18 | public StringValues this[string key]
19 | {
20 | get { return this._values[key]; }
21 | set { this._values[key] = value; }
22 | }
23 |
24 | public int Count => throw new System.NotImplementedException();
25 |
26 | public ICollection Keys => throw new System.NotImplementedException();
27 |
28 | public bool ContainsKey(string key)
29 | {
30 | throw new System.NotImplementedException();
31 | }
32 |
33 | public IEnumerator> GetEnumerator()
34 | {
35 | throw new System.NotImplementedException();
36 | }
37 |
38 | public bool TryGetValue(string key, out StringValues value)
39 | {
40 | throw new System.NotImplementedException();
41 | }
42 |
43 | IEnumerator IEnumerable.GetEnumerator()
44 | {
45 | throw new System.NotImplementedException();
46 | }
47 | }
48 | }
--------------------------------------------------------------------------------
/src/FunctionsV2DiSample.FunctionApp/CoreGitHubRepositoriesHttpTrigger.cs:
--------------------------------------------------------------------------------
1 | using System.Threading.Tasks;
2 |
3 | using FunctionsV2DiSample.FunctionApp.Functions;
4 | using FunctionsV2DiSample.FunctionApp.Functions.FunctionOptions;
5 | using FunctionsV2DiSample.FunctionApp.Modules;
6 |
7 | using Microsoft.AspNetCore.Http;
8 | using Microsoft.AspNetCore.Mvc;
9 | using Microsoft.Azure.WebJobs;
10 | using Microsoft.Azure.WebJobs.Extensions.Http;
11 | using Microsoft.Azure.WebJobs.Host;
12 |
13 | namespace FunctionsV2DiSample.FunctionApp
14 | {
15 | ///
16 | /// This represents the HTTP trigger entity to list all repositories for a given user or organisation from GitHub.
17 | ///
18 | public static class CoreGitHubRepositoriesHttpTrigger
19 | {
20 | public static IFunctionFactory Factory = new CoreFunctionFactory(new CoreAppModule());
21 |
22 | ///
23 | /// Invokes the HTTP trigger.
24 | ///
25 | ///
26 | ///
27 | /// Returns response.
28 | [FunctionName("CoreGitHubRepositoriesHttpTrigger")]
29 | public static async Task Run([HttpTrigger(AuthorizationLevel.Function, "get", Route = "core/repositories")]HttpRequest req, TraceWriter log)
30 | {
31 | var options = GetOptions(req);
32 |
33 | var result = await Factory.Create(log).InvokeAsync(req, options).ConfigureAwait(false);
34 |
35 | return new OkObjectResult(result);
36 | }
37 |
38 | private static GitHubRepositoriesHttpTriggerOptions GetOptions(HttpRequest req)
39 | {
40 | return new GitHubRepositoriesHttpTriggerOptions(req);
41 | }
42 | }
43 | }
--------------------------------------------------------------------------------
/src/FunctionsV2DiSample.FunctionApp/AutofacGitHubRepositoriesHttpTrigger.cs:
--------------------------------------------------------------------------------
1 | //using System.Threading.Tasks;
2 |
3 | //using FunctionsV2DiSample.FunctionApp.Functions;
4 | //using FunctionsV2DiSample.FunctionApp.Functions.FunctionOptions;
5 | //using FunctionsV2DiSample.FunctionApp.Modules;
6 |
7 | //using Microsoft.AspNetCore.Http;
8 | //using Microsoft.AspNetCore.Mvc;
9 | //using Microsoft.Azure.WebJobs;
10 | //using Microsoft.Azure.WebJobs.Extensions.Http;
11 | //using Microsoft.Azure.WebJobs.Host;
12 |
13 | //namespace FunctionsV2DiSample.FunctionApp
14 | //{
15 | // ///
16 | // /// This represents the HTTP trigger entity to list all repositories for a given user or organisation from GitHub.
17 | // ///
18 | // public static class AutofacGitHubRepositoriesHttpTrigger
19 | // {
20 | // public static IFunctionFactory Factory = new AutofacFunctionFactory(new AutofacAppModule());
21 |
22 | // ///
23 | // /// Invokes the HTTP trigger.
24 | // ///
25 | // ///
26 | // ///
27 | // /// Returns response.
28 | // [FunctionName("AutofacGitHubRepositoriesHttpTrigger")]
29 | // public static async Task Run([HttpTrigger(AuthorizationLevel.Function, "get", Route = "autofac/repositories")]HttpRequest req, TraceWriter log)
30 | // {
31 | // var options = GetOptions(req);
32 |
33 | // var result = await Factory.Create(log, "github").InvokeAsync(req, options).ConfigureAwait(false);
34 |
35 | // return new OkObjectResult(result);
36 | // }
37 |
38 | // private static GitHubRepositoriesHttpTriggerOptions GetOptions(HttpRequest req)
39 | // {
40 | // return new GitHubRepositoriesHttpTriggerOptions(req);
41 | // }
42 | // }
43 | //}
--------------------------------------------------------------------------------
/test/FunctionsV2DiSample.FunctionApp.Tests/CoreGitHubRepositoriesHttpTriggerTests.cs:
--------------------------------------------------------------------------------
1 | using System.Threading.Tasks;
2 |
3 | using FluentAssertions;
4 |
5 | using FunctionsV2DiSample.FunctionApp.Functions;
6 | using FunctionsV2DiSample.FunctionApp.Functions.FunctionOptions;
7 | using FunctionsV2DiSample.FunctionApp.Tests.Fixtures;
8 |
9 | using Microsoft.AspNetCore.Http;
10 | using Microsoft.AspNetCore.Mvc;
11 | using Microsoft.Azure.WebJobs.Extensions;
12 | using Microsoft.Azure.WebJobs.Host;
13 | using Microsoft.VisualStudio.TestTools.UnitTesting;
14 |
15 | using Moq;
16 |
17 | namespace FunctionsV2DiSample.FunctionApp.Tests
18 | {
19 | [TestClass]
20 | public class CoreGitHubRepositoriesHttpTriggerTests
21 | {
22 | [TestMethod]
23 | public async Task Given_TypeAndName_Run_Should_Return_Result()
24 | {
25 | var result = new { Hello = "World" };
26 |
27 | var function = new Mock();
28 | function.Setup(p => p.InvokeAsync(It.IsAny(), It.IsAny())).ReturnsAsync(result);
29 |
30 | var factory = new Mock();
31 | factory.Setup(p => p.Create(It.IsAny(), It.IsAny())).Returns(function.Object);
32 |
33 | CoreGitHubRepositoriesHttpTrigger.Factory = factory.Object;
34 |
35 | var query = new FakeQueryCollection();
36 | query["type"] = "lorem";
37 | query["name"] = "ipsum";
38 |
39 | var req = new Mock();
40 | req.SetupGet(p => p.Query).Returns(query);
41 |
42 | var log = new TraceMonitor();
43 | var response = await CoreGitHubRepositoriesHttpTrigger.Run(req.Object, log).ConfigureAwait(false);
44 |
45 | response.Should().BeOfType();
46 | (response as OkObjectResult).Value.Should().Be(result);
47 | }
48 | }
49 | }
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Azure Functions V2 Dependency Injection Sample #
2 |
3 | This provides sample codes for Azure Functions V2, with regards to dependency injections.
4 |
5 |
6 | ## More Readings ##
7 |
8 | * English: Dependency Injections on Azure Functions V2 – [DevKimchi](https://devkimchi.com/#coming-soon), [Mexia](https://blog.mexia.com.au/#coming-soon)
9 | * 한국어: 애저 펑션 V2 에서 의존성 주입 및 관리 – [Aliencube](http://blog.aliencube.com/#coming-soon)
10 |
11 | ## Implementations ##
12 |
13 | * https://github.com/aliencube/AzureFunctions.Extensions
14 | * https://www.nuget.org/packages/Aliencube.AzureFunctions.Extensions.DependencyInjection/
15 |
16 |
17 | ## Contribution ##
18 |
19 | Your contributions are always welcome! All your work should be done in your forked repository. Once you finish your work with corresponding tests, please send us a pull request onto our `master` branch for review.
20 |
21 |
22 | ## License ##
23 |
24 | This is released under [MIT License](http://opensource.org/licenses/MIT)
25 |
26 | > The MIT License (MIT)
27 | >
28 | > Copyright (c) 2018 [devkimchi.com](https://devkimchi.com)
29 | >
30 | > Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
31 | >
32 | > The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
33 | >
34 | > THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 |
--------------------------------------------------------------------------------
/Settings.StyleCop:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | en-US
6 |
7 |
8 |
9 |
10 |
11 |
12 | False
13 |
14 |
15 |
16 |
17 | False
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 | False
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 | False
38 |
39 |
40 |
41 |
42 | True
43 |
44 |
45 |
46 |
47 |
48 |
49 | False
50 |
51 |
52 |
53 |
54 |
55 | db
56 |
57 |
58 |
59 |
60 |
--------------------------------------------------------------------------------
/AzureFunctionsV2DependencyInjectionSample.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio 15
4 | VisualStudioVersion = 15.0.27428.1
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{93282150-FCD3-4B13-99C6-5A71A1D2F967}"
7 | EndProject
8 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FunctionsV2DiSample.FunctionApp", "src\FunctionsV2DiSample.FunctionApp\FunctionsV2DiSample.FunctionApp.csproj", "{5A939504-A3F7-4208-8599-F96FAAC817DF}"
9 | EndProject
10 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{EFC49283-E92E-47C5-8998-721CFBF3BB13}"
11 | EndProject
12 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FunctionsV2DiSample.FunctionApp.Tests", "test\FunctionsV2DiSample.FunctionApp.Tests\FunctionsV2DiSample.FunctionApp.Tests.csproj", "{F50077C3-4650-4330-B631-1DC9E09E19F1}"
13 | EndProject
14 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{ADFD6F77-DC02-4ADD-A190-D046D9E12CD4}"
15 | ProjectSection(SolutionItems) = preProject
16 | .gitignore = .gitignore
17 | .mailmap = .mailmap
18 | LICENSE = LICENSE
19 | README.md = README.md
20 | Settings.StyleCop = Settings.StyleCop
21 | EndProjectSection
22 | EndProject
23 | Global
24 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
25 | Debug|Any CPU = Debug|Any CPU
26 | Release|Any CPU = Release|Any CPU
27 | EndGlobalSection
28 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
29 | {5A939504-A3F7-4208-8599-F96FAAC817DF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
30 | {5A939504-A3F7-4208-8599-F96FAAC817DF}.Debug|Any CPU.Build.0 = Debug|Any CPU
31 | {5A939504-A3F7-4208-8599-F96FAAC817DF}.Release|Any CPU.ActiveCfg = Release|Any CPU
32 | {5A939504-A3F7-4208-8599-F96FAAC817DF}.Release|Any CPU.Build.0 = Release|Any CPU
33 | {F50077C3-4650-4330-B631-1DC9E09E19F1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
34 | {F50077C3-4650-4330-B631-1DC9E09E19F1}.Debug|Any CPU.Build.0 = Debug|Any CPU
35 | {F50077C3-4650-4330-B631-1DC9E09E19F1}.Release|Any CPU.ActiveCfg = Release|Any CPU
36 | {F50077C3-4650-4330-B631-1DC9E09E19F1}.Release|Any CPU.Build.0 = Release|Any CPU
37 | EndGlobalSection
38 | GlobalSection(SolutionProperties) = preSolution
39 | HideSolutionNode = FALSE
40 | EndGlobalSection
41 | GlobalSection(NestedProjects) = preSolution
42 | {5A939504-A3F7-4208-8599-F96FAAC817DF} = {93282150-FCD3-4B13-99C6-5A71A1D2F967}
43 | {F50077C3-4650-4330-B631-1DC9E09E19F1} = {EFC49283-E92E-47C5-8998-721CFBF3BB13}
44 | EndGlobalSection
45 | GlobalSection(ExtensibilityGlobals) = postSolution
46 | SolutionGuid = {1D809443-901B-4D07-8205-2C4B97948128}
47 | EndGlobalSection
48 | EndGlobal
49 |
--------------------------------------------------------------------------------
/src/FunctionsV2DiSample.FunctionApp/Functions/CoreGitHubRepositoriesFunction.cs:
--------------------------------------------------------------------------------
1 | using System.Net.Http;
2 | using System.Net.Http.Headers;
3 | using System.Threading.Tasks;
4 |
5 | using FunctionsV2DiSample.FunctionApp.Configs;
6 | using FunctionsV2DiSample.FunctionApp.Functions.FunctionOptions;
7 |
8 | using Microsoft.Azure.WebJobs.Host;
9 |
10 | using Newtonsoft.Json;
11 |
12 | namespace FunctionsV2DiSample.FunctionApp.Functions
13 | {
14 | ///
15 | /// This represents the function entity for GitHub repositories.
16 | ///
17 | public class CoreGitHubRepositoriesFunction : IGitHubRepositoriesFunction
18 | {
19 | private static MediaTypeWithQualityHeaderValue acceptHeader = new MediaTypeWithQualityHeaderValue("application/vnd.github.v3+json");
20 | private static ProductInfoHeaderValue userAgentHeader = new ProductInfoHeaderValue("Mozilla", "5.0");
21 |
22 | private readonly GitHub _github;
23 | private readonly HttpClient _httpClient;
24 |
25 | public CoreGitHubRepositoriesFunction(GitHub github, HttpClient httpClient)
26 | {
27 | this._github = github;
28 | this._httpClient = httpClient;
29 | }
30 |
31 | ///
32 | public TraceWriter Log { get; set; }
33 |
34 | ///
35 | public async Task InvokeAsync(TInput input, FunctionOptionsBase options)
36 | {
37 | var option = options as GitHubRepositoriesHttpTriggerOptions;
38 |
39 | string result;
40 |
41 | this.AddRequestHeaders();
42 |
43 | var requestUrl = $"{this._github.BaseUrl}{string.Format(this._github.Endpoints.Repositories, option.Type, option.Name)}";
44 | using (var message = await this._httpClient.GetAsync(requestUrl).ConfigureAwait(false))
45 | {
46 | result = await message.Content.ReadAsStringAsync().ConfigureAwait(false);
47 | }
48 |
49 | var res = JsonConvert.DeserializeObject