Skip to content

Commit

Permalink
fix:dapr starter (masastack#16)
Browse files Browse the repository at this point in the history
* tag: 0.3.0 (masastack#14)

* Feature/cache

* add caller

* chore: minimal support specify Assembly

* support net6.0

* chore: close gitlab package

* chore: add package CI

* feat: Improve DI extensions

* chore: update dapr library package

* Feature/cache

* chore: Added support for adding Caller multiple times, and supports automatic...

* add ghpackageconfig

* Add GetAsync method overload

* Update package_push_nuget.org.yml

* Update package_push_github.yml

* Update package_push_github.yml

* feat: add es

* Elasticsearch init

* chore: add MasaElasticClient

* chore: Elasticsearch init

* chore: elasticsearch init

* chore: Improve MasaElasticClient

* chore: Add DocumentExistsAsync

* chore: adjust ElasticsearchRelations

* chore: Support document batch operation

* chore: adjust AddElasticsearch methods

* chore: add AddElasticsearchClient methods

* chore: adjust GetPaginatedListAsync methods

* chore: add alias

* chore:  add GetIndexByAliasAsync

* chore: adjust GetAliasByIndexAsync methods

* chore: Adjust using references

* chore: add pinyin Filter

* chore: adjust MASA.Utils.Data.Elasticsearch

* chore: change methods name

* chore: Adjustment parameters

* chore: replce default IndexName

* chore: add readonly

* chore: Optimize the AddElasticsearch method Optimize the AddElasticsearch method

* chore: Simplified parameter names

Co-authored-by: zhenlei520 <[email protected]>

* Update package_push_github.yml

* Update nuget.config

* feat: migrate old MASA.Framework to MASA.Utils

* chore: add MASA.Utils.Exceptions

* chore: add MASA.Utils.Exceptions

* chore: adjust package

* chore: add MASA.Utils.Enums、add MASA.Utils.Expressions

* chore: Added MASA.Utils.Security.Cryptography, MASA.Utils.Security.Token class libraries

* chore: Added MASA.Utils.Security.Cryptography

* chore: add MASA.Utils.Security.Authentication

* chore: remove warning

* chore: remove warning

* remove warning

* feat: redis & caller

* chore: Caller adds handling of Service exception reporting

* chore: adjust GlobalException

* chore: Fix redis set error

* chore: Response supports null

Co-authored-by: zhenlei520 <[email protected]>

* chore: Fix GetAsync<TResponse> method error (masastack#7)

* feat:dapr (masastack#6)

* chore: add MASA.Utils.Development.Dapr

* chore: dapr init

* chore: Stop dapr process after project is stopped

* chore: Increase get pid based on port

* chore: add dapr daemon, restart dapr after dapr is shut down abnormally, Added OSX system port handling

* chore: dapr supports the default AppPort obtained from ApplicationUrl by default, automatically assigns the grpc and http ports of dapr and automatically supplements environment variables

Supports automatic restart after the dapr process is closed, and the configuration after restart is consistent with the last successful startup configuration

Supports automatic restart of the dapr process after configuration update. Currently, there are no restrictions on HttpPort and GrpcPort. However, if the configuration update changes HttpPort and GrpcPort, the port obtained by DaprClient will be inconsistent with the actual running one. It needs to be adjusted later here.

* chore: remove redundant code

* chore: remove bad code

* fix: Fix AddDapr method enable dapr slidecar error

* feature: add dapr background options

* feature: add dapr background options

* feature: add dapr background options

* chore: Modify log

Co-authored-by: zhenlei520 <[email protected]>

* Delete package_push_github.yml

* update action

* Update package_push_nuget.org.yml

* chore: publish 0.3.0 (masastack#8) (masastack#9)

* Feature/cache

* add caller

* chore: minimal support specify Assembly

* support net6.0

* chore: close gitlab package

* chore: add package CI

* feat: Improve DI extensions

* chore: update dapr library package

* Feature/cache

* chore: Added support for adding Caller multiple times, and supports automatic...

* add ghpackageconfig

* Add GetAsync method overload

* Update package_push_nuget.org.yml

* Update package_push_github.yml

* Update package_push_github.yml

* feat: add es

* Elasticsearch init

* chore: add MasaElasticClient

* chore: Elasticsearch init

* chore: elasticsearch init

* chore: Improve MasaElasticClient

* chore: Add DocumentExistsAsync

* chore: adjust ElasticsearchRelations

* chore: Support document batch operation

* chore: adjust AddElasticsearch methods

* chore: add AddElasticsearchClient methods

* chore: adjust GetPaginatedListAsync methods

* chore: add alias

* chore:  add GetIndexByAliasAsync

* chore: adjust GetAliasByIndexAsync methods

* chore: Adjust using references

* chore: add pinyin Filter

* chore: adjust MASA.Utils.Data.Elasticsearch

* chore: change methods name

* chore: Adjustment parameters

* chore: replce default IndexName

* chore: add readonly

* chore: Optimize the AddElasticsearch method Optimize the AddElasticsearch method

* chore: Simplified parameter names

Co-authored-by: zhenlei520 <[email protected]>

* Update package_push_github.yml

* Update nuget.config

* feat: migrate old MASA.Framework to MASA.Utils

* chore: add MASA.Utils.Exceptions

* chore: add MASA.Utils.Exceptions

* chore: adjust package

* chore: add MASA.Utils.Enums、add MASA.Utils.Expressions

* chore: Added MASA.Utils.Security.Cryptography, MASA.Utils.Security.Token class libraries

* chore: Added MASA.Utils.Security.Cryptography

* chore: add MASA.Utils.Security.Authentication

* chore: remove warning

* chore: remove warning

* remove warning

* feat: redis & caller

* chore: Caller adds handling of Service exception reporting

* chore: adjust GlobalException

* chore: Fix redis set error

* chore: Response supports null

Co-authored-by: zhenlei520 <[email protected]>

* chore: Fix GetAsync<TResponse> method error (masastack#7)

* feat:dapr (masastack#6)

* chore: add MASA.Utils.Development.Dapr

* chore: dapr init

* chore: Stop dapr process after project is stopped

* chore: Increase get pid based on port

* chore: add dapr daemon, restart dapr after dapr is shut down abnormally, Added OSX system port handling

* chore: dapr supports the default AppPort obtained from ApplicationUrl by default, automatically assigns the grpc and http ports of dapr and automatically supplements environment variables

Supports automatic restart after the dapr process is closed, and the configuration after restart is consistent with the last successful startup configuration

Supports automatic restart of the dapr process after configuration update. Currently, there are no restrictions on HttpPort and GrpcPort. However, if the configuration update changes HttpPort and GrpcPort, the port obtained by DaprClient will be inconsistent with the actual running one. It needs to be adjusted later here.

* chore: remove redundant code

* chore: remove bad code

* fix: Fix AddDapr method enable dapr slidecar error

* feature: add dapr background options

* feature: add dapr background options

* feature: add dapr background options

* chore: Modify log

Co-authored-by: zhenlei520 <[email protected]>

* Delete package_push_github.yml

* update action

* Update package_push_nuget.org.yml

Co-authored-by: zhenlei520 <[email protected]>
Co-authored-by: zhenlei520 <[email protected]>
Co-authored-by: 曹尤先 <[email protected]>
Co-authored-by: 王达 <[email protected]>
Co-authored-by: PollosD <[email protected]>
Co-authored-by: 听雨声 <[email protected]>

Co-authored-by: zhenlei520 <[email protected]>
Co-authored-by: zhenlei520 <[email protected]>
Co-authored-by: 曹尤先 <[email protected]>
Co-authored-by: 王达 <[email protected]>
Co-authored-by: PollosD <[email protected]>
Co-authored-by: 听雨声 <[email protected]>

* refactor: remove MASA

* refactor: change MASA.Utils to Masa.Utils (masastack#13)

* chore: delete .vs

* refactor: change MASA.Utils to Masa.Utils

Co-authored-by: zhenlei520 <[email protected]>
Co-authored-by: zhenlei520 <[email protected]>
Co-authored-by: 曹尤先 <[email protected]>
Co-authored-by: 王达 <[email protected]>
Co-authored-by: PollosD <[email protected]>
Co-authored-by: 听雨声 <[email protected]>

* fix: adjust dapr

* feat: Caller nesting is allowed, but deprecated

* doc: add dapr readme

* Update README.md

* doc: adjust dapr readme

* doc: adjust README.md

* Update README.md

* Update README.zh-CN.md

* Update README.md

* Update README.md

* Update README.md

Co-authored-by: 鬼谷子 <[email protected]>
Co-authored-by: zhenlei520 <[email protected]>
Co-authored-by: zhenlei520 <[email protected]>
Co-authored-by: 曹尤先 <[email protected]>
Co-authored-by: 王达 <[email protected]>
Co-authored-by: PollosD <[email protected]>
  • Loading branch information
7 people authored Mar 4, 2022
1 parent e2c45ed commit d0a0fbf
Show file tree
Hide file tree
Showing 15 changed files with 638 additions and 143 deletions.
2 changes: 1 addition & 1 deletion src/Caller/Masa.Utils.Caller.Core/CallerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

public abstract class CallerBase
{
public string Name { get; set; } = default!;
public abstract string Name { get; set; }

protected CallerOptions CallerOptions { get; private set; } = default!;

Expand Down
21 changes: 18 additions & 3 deletions src/Caller/Masa.Utils.Caller.Core/CallerOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,31 @@ namespace Masa.Utils.Caller.Core;

public class CallerOptions
{
internal List<CallerRelations> Callers = new();
internal readonly List<CallerRelations> Callers = new();

public IServiceCollection Services { get; }

private Assembly[] _assemblies = AppDomain.CurrentDomain.GetAssemblies();

public Assembly[] Assemblies
{
get => _assemblies;
set
{
ArgumentNullException.ThrowIfNull(value, nameof(Assemblies));

_assemblies = value;
}
}

public ServiceLifetime CallerLifetime { get; set; }

public JsonSerializerOptions? JsonSerializerOptions { get; set; }

public CallerOptions(IServiceCollection services, JsonSerializerOptions? jsonSerializerOptions = null)
public CallerOptions(IServiceCollection services)
{
Services = services;
JsonSerializerOptions = jsonSerializerOptions;
CallerLifetime = ServiceLifetime.Scoped;
}

public void AddCaller(string name, bool isDefault, Func<IServiceProvider, ICallerProvider> func)
Expand Down
54 changes: 30 additions & 24 deletions src/Caller/Masa.Utils.Caller.Core/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,33 @@ namespace Masa.Utils.Caller.Core;

public static class ServiceCollectionExtensions
{
public static IServiceCollection AddCaller(this IServiceCollection services)
=> services.AddCaller(AppDomain.CurrentDomain.GetAssemblies());

public static IServiceCollection AddCaller(this IServiceCollection services, params Assembly[] assemblies)
=> services.AddCaller(options => options.Assemblies = assemblies);

private static IServiceCollection AddCaller(this IServiceCollection services,
ServiceLifetime lifetime = ServiceLifetime.Scoped,
params Assembly[] assemblies)
=> services.AddCaller(options =>
{
options.Assemblies = assemblies;
options.CallerLifetime = lifetime;
});

public static IServiceCollection AddCaller(this IServiceCollection services, Action<CallerOptions> options)
{
CallerOptions callerOption = new CallerOptions(services);
options.Invoke(callerOption);

if (callerOption.Callers.Count == 0)
throw new ArgumentNullException("Caller provider is not found, check if Caller is used", (Exception?) null);
throw new ArgumentNullException("Caller provider is not found, check if Caller is used", (Exception?)null);

if (callerOption.Callers.Count(c => c.IsDefault) > 1)
throw new ArgumentNullException("Caller provider can only have one default", (Exception?) null);
throw new ArgumentNullException("Caller provider can only have one default", (Exception?)null);

services.AddAutomaticCaller(callerOption);
services.TryOrUpdateCallerOptions(callerOption);
services.TryAddSingleton<ICallerFactory, DefaultCallerFactory>();
services.TryAddSingleton<IRequestMessage, DefaultRequestMessage>();
Expand Down Expand Up @@ -41,32 +57,22 @@ private static IServiceCollection TryOrUpdateCallerOptions(this IServiceCollecti
return services;
}

public static IServiceCollection AddCaller(this IServiceCollection services,
params Assembly[]? assemblies)
=> services.AddCaller(ServiceLifetime.Scoped, assemblies);

public static IServiceCollection AddCaller(this IServiceCollection services,
ServiceLifetime lifetime = ServiceLifetime.Scoped,
params Assembly[]? assemblies)
private static void AddAutomaticCaller(this IServiceCollection services, CallerOptions callerOptions)
{
if (assemblies == null || assemblies.Length == 0)
var callerTypes = callerOptions.Assemblies.SelectMany(x => x.GetTypes())
.Where(type => typeof(CallerBase).IsAssignableFrom(type) && !type.IsAbstract).ToList();
callerTypes.ForEach(type =>
{
assemblies = AppDomain.CurrentDomain.GetAssemblies();
}
ServiceDescriptor serviceDescriptor = new ServiceDescriptor(type, type, callerOptions.CallerLifetime);
services.TryAdd(serviceDescriptor);
});

return services.AddCaller(options =>
callerTypes.ForEach(type =>
{
var callerTypes = assemblies.SelectMany(x => x.GetTypes())
.Where(type => typeof(CallerBase).IsAssignableFrom(type) && !type.IsAbstract).ToList();
callerTypes.ForEach(type =>
{
ServiceDescriptor serviceDescriptor = new ServiceDescriptor(type, type, lifetime);
services.Add(serviceDescriptor);
var serviceProvider = services.BuildServiceProvider();
var callerBase = (CallerBase) serviceProvider.GetRequiredService(type);
callerBase.SetCallerOptions(options);
callerBase.UseCallerExtension();
});
var serviceProvider = services.BuildServiceProvider();
var callerBase = (CallerBase)serviceProvider.GetRequiredService(type);
callerBase.SetCallerOptions(callerOptions);
callerBase.UseCallerExtension();
});
}
}
7 changes: 3 additions & 4 deletions src/Caller/Masa.Utils.Caller.Core/_Imports.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
global using Google.Protobuf;
global using Masa.Utils.Caller.Core.Internal;
global using Masa.Utils.Exceptions;
global using Microsoft.Extensions.DependencyInjection;
global using Microsoft.Extensions.DependencyInjection.Extensions;
global using System.Reflection;
global using System.Text.Json;
global using System.Net;
global using System.Net.Http.Json;
global using Masa.Utils.Exceptions;

global using System.Reflection;
global using System.Text.Json;
59 changes: 59 additions & 0 deletions src/Development/Masa.Utils.Development.Dapr.AspNetCore/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
[](README.zh-CN.md) | EN

## Masa.Utils.Development.Dapr.AspNetCore

Responsibilities:

Assist in managing the dapr process to reduce the dependency on docker compose during development

### Basic usage:

1. Install Masa.Utils.Development.Dapr.AspNetCore
``` C#
Install-Package Masa.Utils.Development.Dapr.AspNetCore
```

2. Add DaprStarter to assist in managing the dapr process (recommended to be used in the development environment)

``` C#
builder.Services.AddDaprStarter();
```

### Advanced usage:

1. Specify the configuration in the code

``` C#
builder.Services.AddDaprStarter(opt =>
{
opt.AppId = "masa-dapr-test";
opt.AppPort = 5001;
opt.AppIdSuffix = "";
opt.DaprHttpPort = 8080;
opt.DaprGrpcPort = 8081;
});
```

2. The configuration file specifies the configuration

First step:

``` appsettings.json
{
"DaprOptions": {
"AppId": "masa-dapr-test",
"AppPort": 5001,
"AppIdSuffix": "",
"DaprHttpPort": 8080,
"DaprGrpcPort": 8081
}
}
```

Step 2:

``` C#
builder.Services.AddDaprStarter(builder.Configuration.GetSection("DaprOptions");
```

Advantages: After the configuration is changed, the dapr process is restarted and updated, and the project does not need to be restarted
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
中 | [EN](README.md)

## Masa.Utils.Development.Dapr.AspNetCore

职责:

协助管理dapr进程,用于开发时减少对docker compose的依赖

### 基本用法:

1. 安装Masa.Utils.Development.Dapr.AspNetCore
```C#
Install-Package Masa.Utils.Development.Dapr.AspNetCore
```

2. 添加DaprStarter协助管理dapr进程(建议在开发环境使用)

```C#
builder.Services.AddDaprStarter();
```

### 高级用法:

1. 代码中指定配置

```C#
builder.Services.AddDaprStarter(opt =>
{
opt.AppId = "masa-dapr-test";
opt.AppPort = 5001;
opt.AppIdSuffix = "";
opt.DaprHttpPort = 8080;
opt.DaprGrpcPort = 8081;
});
```

2. 配置文件指定配置

第一步:

``` appsettings.json
{
"DaprOptions": {
"AppId": "masa-dapr-test",
"AppPort": 5001,
"AppIdSuffix": "",
"DaprHttpPort": 8080,
"DaprGrpcPort": 8081
}
}
```

第二步:

``` C#
builder.Services.AddDaprStarter(builder.Configuration.GetSection("DaprOptions"));
```

优势:支持配置变更后,dapr 进程重启更新,项目无需重新启动
Original file line number Diff line number Diff line change
@@ -1,42 +1,38 @@
namespace Masa.Utils.Development.Dapr.AspNetCore;
namespace Masa.Utils.Development.Dapr.AspNetCore;

public static class ServiceCollectionExtensions
{
public static IServiceCollection AddDaprStarter(
this IServiceCollection services,
Action<DaprOptions>? action = null,
Action<DaprBackgroundOptions>? daprBackgroundOptionsAction = null)
Action<DaprOptions>? daprOptionAction = null)
{
DaprOptions daprOptions = new();
action?.Invoke(daprOptions);
return services.AddDaprStarter(() => daprOptions, daprBackgroundOptionsAction);
return services.AddDaprStarter(opt =>
{
daprOptionAction?.Invoke(opt);
}, _ => { });
}

public static IServiceCollection AddDaprStarter(this IServiceCollection services,
Func<DaprOptions> func,
Action<DaprBackgroundOptions>? action = null)
Action<DaprOptions> daprOptionAction,
Action<DaprBackgroundOptions> action)
{
if (services.Any(service => service.ImplementationType == typeof(DaprService)))
{
return services;
}
services.AddSingleton<DaprService>();

if (action != null)
services.Configure(action);
services.Configure(action);

services.AddHostedService<DaprBackgroundService>();
services.Configure<HostOptions>(opts => opts.ShutdownTimeout = TimeSpan.FromSeconds(15));
return services.AddDaprStarterCore(func);
return services.AddDaprStarterCore(daprOptionAction);
}

public static IServiceCollection AddDaprStarter(this IServiceCollection services, IConfiguration configuration)
{
if (services.Any(service => service.ImplementationType == typeof(DaprService)))
{
return services;
}
services.AddSingleton<DaprService>();

services.AddHostedService<DaprBackgroundService>();
services.Configure<DaprBackgroundOptions>(configuration);
return services.AddDaprStarterCore(configuration);
Expand Down
Loading

0 comments on commit d0a0fbf

Please sign in to comment.