Skip to content
Open
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
11 changes: 11 additions & 0 deletions WorkData/WorkData.Apollo/WorkData.Apollo.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\WorkData\WorkData.csproj" />
</ItemGroup>

</Project>
21 changes: 21 additions & 0 deletions WorkData/WorkData.ApolloWeb/ApiController/ValuesController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Microsoft.AspNetCore.Mvc;
using WorkData.Code.JwtSecurityTokens;
using WorkData.Dependency;
using Newtonsoft.Json;
using System;
using WorkData.EntityFramework;

namespace WorkData.ApolloWeb.ApiController
{
[ApiController]
[Route("api/Values")]
public class ValuesController : Controller
{
[HttpGet]
public IActionResult Get(string key)
{
var data = IocManager.Instance.ResolveServiceValue<string>(key);
return Json(data);
}
}
}
3 changes: 3 additions & 0 deletions WorkData/WorkData.ApolloWeb/Config/hosting.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"server.urls": "http://+:8100"
}
7 changes: 7 additions & 0 deletions WorkData/WorkData.ApolloWeb/Config/moduleConfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"modules": [
{
"type": "WorkData.ApolloWeb.WorkDataApolloWebModule,WorkData.ApolloWeb"
}
]
}
34 changes: 34 additions & 0 deletions WorkData/WorkData.ApolloWeb/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using Autofac.Extensions.DependencyInjection;
using Com.Ctrip.Framework.Apollo;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using System.IO;
using WorkData.EntityFramework;

namespace WorkData.ApolloWeb
{
public class Program
{
public static void Main(string[] args)
{
var host = BuildWebHost(args);
host.Run();
}

public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)

.ConfigureAppConfiguration((hostingContext, webBuilder) =>
{
webBuilder
.AddApollo(webBuilder.Build().GetSection("apollo"))
.AddDefault();
})
.ConfigureServices(services => services.AddAutofac())
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>()
.Build();
}
}
27 changes: 27 additions & 0 deletions WorkData/WorkData.ApolloWeb/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:55504",
"sslPort": 0
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Production"
}
},
"WorkData.ApolloWeb": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "http://localhost:5000"
}
}
}
52 changes: 52 additions & 0 deletions WorkData/WorkData.ApolloWeb/Startup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Principal;
using System.Threading.Tasks;
using Autofac.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Com.Ctrip.Framework.Apollo;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using WorkData.Code.JwtSecurityTokens;
using WorkData.BaseWeb;

namespace WorkData.ApolloWeb
{
public class Startup:BaseStartup
{
public Startup(IHostingEnvironment env, IConfiguration configuration) : base(env, configuration)
{
}

public IServiceProvider ConfigureServices(IServiceCollection services)
{
//services.Configure<WorkDataBaseJwt>(Configuration.GetSection("WorkDataBaseJwt"));
services.AddMvc();
#region Autofac
BootstrapWarpper.InitiateConfig(new List<string> { "Config/moduleConfig.json" }, services);
#endregion
return new AutofacServiceProvider
(BootstrapWarpper.IocManager.IocContainer);
}


public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment()) app.UseDeveloperExceptionPage();
//静态资源
app.UseStaticFiles();
//MVC
app.UseMvc(routes =>
{
routes.MapRoute(
"default",
"{controller=Home}/{action=Index}/{id?}");
});
}
}
}
43 changes: 43 additions & 0 deletions WorkData/WorkData.ApolloWeb/WorkData.ApolloWeb.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Folder Include="wwwroot\" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Com.Ctrip.Framework.Apollo.Configuration" Version="1.5.1" />
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.1.2" PrivateAssets="All" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\WorkData.Apollo\WorkData.Apollo.csproj" />
<ProjectReference Include="..\WorkData.BaseWeb\WorkData.BaseWeb.csproj" />
<ProjectReference Include="..\WorkData.Code\WorkData.Code.csproj" />
<ProjectReference Include="..\WorkData.EntityFramework\WorkData.EntityFramework.csproj" />
<ProjectReference Include="..\WorkData\WorkData.csproj" />
</ItemGroup>

<ItemGroup>
<Content Update="appsettings.Development.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Update="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Update="appsettings.Production.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Update="Config\hosting.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Update="Config\moduleConfig.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>

</Project>
25 changes: 25 additions & 0 deletions WorkData/WorkData.ApolloWeb/WorkDataApolloWebModule.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// ------------------------------------------------------------------------------
// Copyright 吴来伟个人 版权所有。
// 项目名:WorkData.Web
// 文件名:WorkDataWebModule.cs
// 创建标识:吴来伟 2018-06-11 10:40
// 创建描述:
//
// 修改标识:吴来伟2018-06-11 10:40
// 修改描述:
// ------------------------------------------------------------------------------

using Autofac;
using Microsoft.AspNetCore.Identity;
using WorkData.Extensions.Modules;

namespace WorkData.ApolloWeb
{
public class WorkDataApolloWebModule : WorkDataBaseModule
{
protected override void Load(ContainerBuilder builder)
{

}
}
}
7 changes: 7 additions & 0 deletions WorkData/WorkData.ApolloWeb/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"apollo": {
"AppId": "workdata",
"Env": "dev",
"MetaServer": "http://127.0.0.1:31001"
}
}
7 changes: 7 additions & 0 deletions WorkData/WorkData.ApolloWeb/appsettings.Production.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"apollo": {
"AppId": "workdata",
"Env": "pro",
"MetaServer": "http://127.0.0.1:31001"
}
}
7 changes: 7 additions & 0 deletions WorkData/WorkData.ApolloWeb/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"apollo": {
"AppId": "workdata",
"Env": "dev",
"MetaServer": "http://127.0.0.1:31001"
}
}
76 changes: 76 additions & 0 deletions WorkData/WorkData.AutofacServiceLocator/AutofacServiceLocator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Autofac;
using CommonServiceLocator;

namespace WorkData.AutofacServiceLocator
{
/// <summary>
/// Autofac implementation of the Microsoft CommonServiceLocator.
/// </summary>
public class AutofacServiceLocator : ServiceLocatorImplBase
{
/// <summary>
/// The <see cref="Autofac.IComponentContext"/> from which services
/// should be located.
/// </summary>
private readonly IComponentContext _container;

/// <summary>
/// Initializes a new instance of the <see cref="Autofac.Extras.CommonServiceLocator.AutofacServiceLocator" /> class.
/// </summary>
/// <param name="container">
/// The <see cref="Autofac.IComponentContext"/> from which services
/// should be located.
/// </param>
/// <exception cref="System.ArgumentNullException">
/// Thrown if <paramref name="container" /> is <see langword="null" />.
/// </exception>
public AutofacServiceLocator(IComponentContext container)
{
_container = container ?? throw new ArgumentNullException(nameof(container));
}

/// <summary>
/// Resolves the requested service instance.
/// </summary>
/// <param name="serviceType">Type of instance requested.</param>
/// <param name="key">Name of registered service you want. May be <see langword="null" />.</param>
/// <returns>The requested service instance.</returns>
/// <exception cref="System.ArgumentNullException">
/// Thrown if <paramref name="serviceType" /> is <see langword="null" />.
/// </exception>
protected override object DoGetInstance(Type serviceType, string key)
{
if (serviceType == null)
{
throw new ArgumentNullException(nameof(serviceType));
}

return key != null ? _container.ResolveNamed(key, serviceType) : _container.Resolve(serviceType);
}

/// <summary>
/// Resolves all requested service instances.
/// </summary>
/// <param name="serviceType">Type of instance requested.</param>
/// <returns>Sequence of service instance objects.</returns>
/// <exception cref="System.ArgumentNullException">
/// Thrown if <paramref name="serviceType" /> is <see langword="null" />.
/// </exception>
protected override IEnumerable<object> DoGetAllInstances(Type serviceType)
{
if (serviceType == null)
{
throw new ArgumentNullException(nameof(serviceType));
}

var enumerableType = typeof(IEnumerable<>).MakeGenericType(serviceType);

var instance = _container.Resolve(enumerableType);
return ((IEnumerable)instance).Cast<object>();
}
}
}
33 changes: 33 additions & 0 deletions WorkData/WorkData.AutofacServiceLocator/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Autofac;
using CommonServiceLocator;
using System;
using System.Collections.Generic;

namespace WorkData.AutofacServiceLocator
{
class Program
{
private static IServiceLocator _locator;
static void Main(string[] args)
{
var builder = new ContainerBuilder();

builder
.RegisterType<SimpleLogger>()
.Named<ILogger>(typeof(SimpleLogger).FullName)
.SingleInstance()
.As<ILogger>();


var container = builder.Build();

_locator = new AutofacServiceLocator(container);

for (int i = 0; i < 10000000; i++)
{
var log = _locator.GetInstance<ILogger>();
log.Get();
}
}
}
}
Loading