Skip to content

Commit 37aaa3b

Browse files
vildar82Vildar
andauthored
ReuseSeparatedContext - один контекст на приложение. (#115)
* ReuseSeparatedContext * fix test * тесты конфликта контекстов * review fix ConcurrentDictionary * remove ConcurrentCallsCreateExactlyOneContext --------- Co-authored-by: Vildar <khisyametdinovvt@pik.ru>
1 parent 40ad815 commit 37aaa3b

34 files changed

Lines changed: 1156 additions & 7 deletions

‎RxBim.slnx‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,18 @@
6464
<Project Path="src/Revit/RxBim.Nuke.Revit/RxBim.Nuke.Revit.csproj" />
6565
</Folder>
6666
<Folder Name="/tests/">
67+
<Project Path="tests/RxBim.Shared.Tests/RxBim.Shared.Tests.csproj" />
6768
<Project Path="tests/RxBim.Analyzers.Tests/RxBim.Analyzers.Tests.csproj" />
6869
<Project Path="tests/RxBim.Application.Ribbon.Tests/RxBim.Application.Ribbon.Tests.csproj" />
6970
<Project Path="tests/RxBim.Di.Tests/RxBim.Di.Tests.csproj" />
7071
<Project Path="tests/RxBim.Nuke.Tests/RxBim.Nuke.Tests.csproj" />
7172
</Folder>
73+
<Folder Name="/tests/Fixtures/">
74+
<Project Path="tests/RxBim.Shared.Tests/Fixtures/Dependency/RxBim.Shared.Tests.Dependency.csproj" />
75+
<Project Path="tests/RxBim.Shared.Tests/Fixtures/First/RxBim.Shared.Tests.First.csproj" />
76+
<Project Path="tests/RxBim.Shared.Tests/Fixtures/Second/RxBim.Shared.Tests.Second.csproj" />
77+
<Project Path="tests/RxBim.Shared.Tests/Fixtures/WpfProbe/RxBim.Shared.Tests.WpfProbe.csproj" />
78+
</Folder>
7279
<Project Path="build/_build.csproj">
7380
<Build Project="false" />
7481
</Project>

‎Samples/Autocad/RxBim.Sample.Application.Menu.Fluent.Autocad/App.cs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ namespace RxBim.Sample.Application.Menu.Fluent.Autocad
1212
/// <inheritdoc />
1313
public class App : RxBimApplication
1414
{
15+
#if NETCOREAPP
16+
/// <inheritdoc />
17+
protected override bool RunInSeparatedContext => true;
18+
19+
/// <inheritdoc />
20+
protected override bool ReuseSeparatedContext => true;
21+
#endif
22+
1523
/// <summary>
1624
/// Start application.
1725
/// </summary>

‎Samples/Autocad/RxBim.Sample.Application.Menu.Fluent.Autocad/Commands/Cmd1.cs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@
1818
HelpUrl = "https://github.com/ReactiveBIM/RxBim")]
1919
public class Cmd1 : RxBimCommand
2020
{
21+
#if NETCOREAPP
22+
/// <inheritdoc />
23+
protected override bool RunInSeparatedContext => true;
24+
25+
/// <inheritdoc />
26+
protected override bool ReuseSeparatedContext => true;
27+
#endif
28+
2129
/// <summary>
2230
/// Executes the command.
2331
/// </summary>

‎Samples/Autocad/RxBim.Sample.Application.Menu.Fluent.Autocad/Commands/Cmd2.cs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@
1717
HelpUrl = "https://www.google.com/")]
1818
public class Cmd2 : RxBimCommand
1919
{
20+
#if NETCOREAPP
21+
/// <inheritdoc />
22+
protected override bool RunInSeparatedContext => true;
23+
24+
/// <inheritdoc />
25+
protected override bool ReuseSeparatedContext => true;
26+
#endif
27+
2028
/// <summary>
2129
/// Command execution.
2230
/// </summary>

‎Samples/Autocad/RxBim.Sample.Application.Menu.Fluent.Autocad/Commands/Cmd3.cs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@
1717
HelpUrl = "https://www.autodesk.com/")]
1818
public class Cmd3 : RxBimCommand
1919
{
20+
#if NETCOREAPP
21+
/// <inheritdoc />
22+
protected override bool RunInSeparatedContext => true;
23+
24+
/// <inheritdoc />
25+
protected override bool ReuseSeparatedContext => true;
26+
#endif
27+
2028
/// <summary>
2129
/// Command execution.
2230
/// </summary>

‎Samples/Revit/RxBim.Sample.Application.Menu.Fluent.Revit/App.cs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@
99
/// <inheritdoc />
1010
public class App : RxBimApplication
1111
{
12+
#if NETCOREAPP
13+
/// <inheritdoc />
14+
protected override bool RunInSeparatedContext => true;
15+
16+
/// <inheritdoc />
17+
protected override bool ReuseSeparatedContext => true;
18+
#endif
19+
1220
/// <summary>
1321
/// Start application.
1422
/// </summary>

‎Samples/Revit/RxBim.Sample.Application.Menu.Fluent.Revit/Cmd1.cs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@
2121
HelpUrl = "https://github.com/ReactiveBIM/RxBim")]
2222
public class Cmd1 : RxBimCommand
2323
{
24+
#if NETCOREAPP
25+
/// <inheritdoc />
26+
protected override bool RunInSeparatedContext => true;
27+
28+
/// <inheritdoc />
29+
protected override bool ReuseSeparatedContext => true;
30+
#endif
31+
2432
/// <summary>
2533
/// cmd.
2634
/// </summary>

‎Samples/Revit/RxBim.Sample.Application.Menu.Fluent.Revit/Cmd2.cs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@
1919
HelpUrl = "https://www.google.com/")]
2020
public class Cmd2 : RxBimCommand
2121
{
22+
#if NETCOREAPP
23+
/// <inheritdoc />
24+
protected override bool RunInSeparatedContext => true;
25+
26+
/// <inheritdoc />
27+
protected override bool ReuseSeparatedContext => true;
28+
#endif
29+
2230
/// <summary>
2331
/// cmd.
2432
/// </summary>

‎Samples/Revit/RxBim.Sample.Application.Menu.Fluent.Revit/Cmd3.cs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@
1919
HelpUrl = "https://www.autodesk.com/")]
2020
public class Cmd3 : RxBimCommand
2121
{
22+
#if NETCOREAPP
23+
/// <inheritdoc />
24+
protected override bool RunInSeparatedContext => true;
25+
26+
/// <inheritdoc />
27+
protected override bool ReuseSeparatedContext => true;
28+
#endif
29+
2230
/// <summary>
2331
/// cmd.
2432
/// </summary>

‎src/Autocad/RxBim.Application.Autocad/RxBimApplication.cs‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ public abstract class RxBimApplication : IExtensionApplication
2323
/// via Addin Manager.
2424
/// </summary>
2525
protected virtual bool RunInSeparatedContext => false;
26+
27+
/// <summary>
28+
/// Reuses the context for the application's DLL directory until AutoCAD exits.
29+
/// Applies only when <see cref="RunInSeparatedContext"/> is enabled.
30+
/// Enable this setting for the application's commands as well to share the same context.
31+
/// </summary>
32+
protected virtual bool ReuseSeparatedContext => false;
2633
#endif
2734

2835
/// <inheritdoc />
@@ -34,6 +41,12 @@ public void Initialize()
3441
var type = GetType();
3542
if (!PluginContext.IsCurrentContextRxBim(type))
3643
{
44+
if (ReuseSeparatedContext)
45+
{
46+
InitializeInReusedContext(type);
47+
return;
48+
}
49+
3750
var appInstance = PluginContext.CreateInstanceInNewContext(type);
3851
if (appInstance is IExtensionApplication application)
3952
{
@@ -67,6 +80,26 @@ public void Terminate()
6780
/// </summary>
6881
protected virtual bool CanBeStarted() => true;
6982

83+
#if NETCOREAPP
84+
private void InitializeInReusedContext(Type type)
85+
{
86+
IExtensionApplication application;
87+
88+
try
89+
{
90+
application = (IExtensionApplication)PluginContext.CreateInstanceInReusedContext(type);
91+
}
92+
catch (Exception exception)
93+
{
94+
// Report loading failures without initializing in the original context.
95+
Application.ShowAlertDialog($"Error: {exception}");
96+
return;
97+
}
98+
99+
application.Initialize();
100+
}
101+
#endif
102+
70103
private void ApplicationOnIdle(object? sender, EventArgs e)
71104
{
72105
Application.Idle -= ApplicationOnIdle;

0 commit comments

Comments
 (0)