Skip to content

Commit 94c2b2f

Browse files
authored
System.MarshalByRefObject F# snippets (#7778)
* MarshalByRefObject F# snippets * fix broken snippet ref
1 parent c66fa89 commit 94c2b2f

File tree

7 files changed

+94
-0
lines changed

7 files changed

+94
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net6.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<Compile Include="source.fs" />
9+
</ItemGroup>
10+
</Project>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//<Snippet1>
2+
open System
3+
open System.Reflection
4+
5+
type Worker() =
6+
inherit MarshalByRefObject()
7+
member _.PrintDomain() =
8+
printfn $"Object is executing in AppDomain \"{AppDomain.CurrentDomain.FriendlyName}\""
9+
10+
// Create an ordinary instance in the current AppDomain
11+
let localWorker = Worker()
12+
localWorker.PrintDomain()
13+
14+
// Create a new application domain, create an instance
15+
// of Worker in the application domain, and execute code
16+
// there.
17+
let ad = AppDomain.CreateDomain "New domain"
18+
let remoteWorker =
19+
ad.CreateInstanceAndUnwrap(typeof<Worker>.Assembly.FullName, "Worker") :?> Worker
20+
remoteWorker.PrintDomain()
21+
22+
// This code produces output similar to the following:
23+
// Object is executing in AppDomain "source.exe"
24+
// Object is executing in AppDomain "New domain"
25+
//</Snippet1>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net48</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<Compile Include="source.fs" />
9+
</ItemGroup>
10+
</Project>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
open System
2+
open System.Runtime.Remoting.Lifetime
3+
open System.Security.Permissions
4+
5+
// <Snippet1>
6+
type MyClass() =
7+
inherit MarshalByRefObject()
8+
9+
[<SecurityPermissionAttribute(SecurityAction.Demand, Flags=SecurityPermissionFlag.Infrastructure)>]
10+
override _.InitializeLifetimeService() =
11+
let lease = base.InitializeLifetimeService() :?> ILease
12+
if lease.CurrentState = LeaseState.Initial then
13+
lease.InitialLeaseTime <- TimeSpan.FromMinutes 1
14+
lease.SponsorshipTimeout <- TimeSpan.FromMinutes 2
15+
lease.RenewOnCallTime <- TimeSpan.FromSeconds 2
16+
lease
17+
// </Snippet1>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net48</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<Compile Include="source.fs" />
9+
</ItemGroup>
10+
</Project>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// <Snippet1>
2+
open System
3+
open System.Runtime.Remoting
4+
open System.Security.Permissions
5+
6+
type TestClass() =
7+
inherit MarshalByRefObject()
8+
9+
[<SecurityPermission(SecurityAction.Demand, Flags=SecurityPermissionFlag.RemotingConfiguration)>]
10+
[<EntryPoint>]
11+
let main _ =
12+
let obj = TestClass()
13+
14+
RemotingServices.SetObjectUriForMarshal(obj, "testUri")
15+
RemotingServices.Marshal obj |> ignore
16+
17+
printfn $"{RemotingServices.GetObjectUri obj}"
18+
0
19+
// </Snippet1>

xml/System/MarshalByRefObject.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
7676
:::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/CreateInstanceAndUnwrap2/cpp/source.cpp" id="Snippet1":::
7777
:::code language="csharp" source="~/snippets/csharp/System/AppDomain/CreateInstanceAndUnwrap/source.cs" id="Snippet1":::
78+
:::code language="fsharp" source="~/snippets/fsharp/System/AppDomain/CreateInstanceAndUnwrap/source.fs" id="Snippet1":::
7879
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/CreateInstanceAndUnwrap2/VB/source.vb" id="Snippet1":::
7980
8081
**Example 2**
@@ -83,6 +84,7 @@
8384
8485
:::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/RemotingServices.SetObjectUriForMarshal/CPP/source.cpp" id="Snippet1":::
8586
:::code language="csharp" source="~/snippets/csharp/System/MarshalByRefObject/Overview/source.cs" id="Snippet1":::
87+
:::code language="fsharp" source="~/snippets/fsharp/System/MarshalByRefObject/Overview/source.fs" id="Snippet1":::
8688
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/RemotingServices.SetObjectUriForMarshal/VB/source.vb" id="Snippet1":::
8789
8890
]]></format>
@@ -293,6 +295,7 @@ For more information about lifetime services, see the <xref:System.Runtime.Remot
293295
294296
:::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR_Classic/classic MarshalByRefObject.InitializeLifetimeService Example/CPP/source.cpp" id="Snippet1":::
295297
:::code language="csharp" source="~/snippets/csharp/System/MarshalByRefObject/InitializeLifetimeService/source.cs" id="Snippet1":::
298+
:::code language="fsharp" source="~/snippets/fsharp/System/MarshalByRefObject/InitializeLifetimeService/source.fs" id="Snippet1":::
296299
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_Classic/classic MarshalByRefObject.InitializeLifetimeService Example/VB/source.vb" id="Snippet1":::
297300
298301
]]></format>

0 commit comments

Comments
 (0)