From 39e36912546f156f7ec951e9bc32eff14dfebd6c Mon Sep 17 00:00:00 2001 From: albert-du <52804499+albert-du@users.noreply.github.com> Date: Mon, 28 Feb 2022 19:30:20 -0800 Subject: [PATCH 1/2] MarshalByRefObject F# snippets --- .../CreateInstanceAndUnwrap/fs.fsproj | 10 ++++++++ .../CreateInstanceAndUnwrap/source.fs | 25 +++++++++++++++++++ .../InitializeLifetimeService/fs.fsproj | 10 ++++++++ .../InitializeLifetimeService/source.fs | 17 +++++++++++++ .../MarshalByRefObject/Overview/fs.fsproj | 10 ++++++++ .../MarshalByRefObject/Overview/source.fs | 18 +++++++++++++ xml/System/MarshalByRefObject.xml | 3 +++ 7 files changed, 93 insertions(+) create mode 100644 snippets/fsharp/System/AppDomain/CreateInstanceAndUnwrap/fs.fsproj create mode 100644 snippets/fsharp/System/AppDomain/CreateInstanceAndUnwrap/source.fs create mode 100644 snippets/fsharp/System/MarshalByRefObject/InitializeLifetimeService/fs.fsproj create mode 100644 snippets/fsharp/System/MarshalByRefObject/InitializeLifetimeService/source.fs create mode 100644 snippets/fsharp/System/MarshalByRefObject/Overview/fs.fsproj create mode 100644 snippets/fsharp/System/MarshalByRefObject/Overview/source.fs diff --git a/snippets/fsharp/System/AppDomain/CreateInstanceAndUnwrap/fs.fsproj b/snippets/fsharp/System/AppDomain/CreateInstanceAndUnwrap/fs.fsproj new file mode 100644 index 00000000000..f0a89cd967d --- /dev/null +++ b/snippets/fsharp/System/AppDomain/CreateInstanceAndUnwrap/fs.fsproj @@ -0,0 +1,10 @@ + + + Exe + net6.0 + + + + + + \ No newline at end of file diff --git a/snippets/fsharp/System/AppDomain/CreateInstanceAndUnwrap/source.fs b/snippets/fsharp/System/AppDomain/CreateInstanceAndUnwrap/source.fs new file mode 100644 index 00000000000..bab52102379 --- /dev/null +++ b/snippets/fsharp/System/AppDomain/CreateInstanceAndUnwrap/source.fs @@ -0,0 +1,25 @@ +// +open System +open System.Reflection + +type Worker() = + inherit MarshalByRefObject() + member _.PrintDomain() = + printfn $"Object is executing in AppDomain \"{AppDomain.CurrentDomain.FriendlyName}\"" + +// Create an ordinary instance in the current AppDomain +let localWorker = Worker() +localWorker.PrintDomain() + +// Create a new application domain, create an instance +// of Worker in the application domain, and execute code +// there. +let ad = AppDomain.CreateDomain "New domain" +let remoteWorker = + ad.CreateInstanceAndUnwrap(typeof.Assembly.FullName, "Worker") :?> Worker +remoteWorker.PrintDomain() + +// This code produces output similar to the following: +// Object is executing in AppDomain "source.exe" +// Object is executing in AppDomain "New domain" +// \ No newline at end of file diff --git a/snippets/fsharp/System/MarshalByRefObject/InitializeLifetimeService/fs.fsproj b/snippets/fsharp/System/MarshalByRefObject/InitializeLifetimeService/fs.fsproj new file mode 100644 index 00000000000..304d2fa939e --- /dev/null +++ b/snippets/fsharp/System/MarshalByRefObject/InitializeLifetimeService/fs.fsproj @@ -0,0 +1,10 @@ + + + Exe + net48 + + + + + + \ No newline at end of file diff --git a/snippets/fsharp/System/MarshalByRefObject/InitializeLifetimeService/source.fs b/snippets/fsharp/System/MarshalByRefObject/InitializeLifetimeService/source.fs new file mode 100644 index 00000000000..d8d51947a51 --- /dev/null +++ b/snippets/fsharp/System/MarshalByRefObject/InitializeLifetimeService/source.fs @@ -0,0 +1,17 @@ +open System +open System.Runtime.Remoting.Lifetime +open System.Security.Permissions + +// +type MyClass() = + inherit MarshalByRefObject() + + [] + override _.InitializeLifetimeService() = + let lease = base.InitializeLifetimeService() :?> ILease + if lease.CurrentState = LeaseState.Initial then + lease.InitialLeaseTime <- TimeSpan.FromMinutes 1 + lease.SponsorshipTimeout <- TimeSpan.FromMinutes 2 + lease.RenewOnCallTime <- TimeSpan.FromSeconds 2 + lease +// \ No newline at end of file diff --git a/snippets/fsharp/System/MarshalByRefObject/Overview/fs.fsproj b/snippets/fsharp/System/MarshalByRefObject/Overview/fs.fsproj new file mode 100644 index 00000000000..304d2fa939e --- /dev/null +++ b/snippets/fsharp/System/MarshalByRefObject/Overview/fs.fsproj @@ -0,0 +1,10 @@ + + + Exe + net48 + + + + + + \ No newline at end of file diff --git a/snippets/fsharp/System/MarshalByRefObject/Overview/source.fs b/snippets/fsharp/System/MarshalByRefObject/Overview/source.fs new file mode 100644 index 00000000000..8ccb19171aa --- /dev/null +++ b/snippets/fsharp/System/MarshalByRefObject/Overview/source.fs @@ -0,0 +1,18 @@ +// +open System +open System.Runtime.Remoting +open System.Security.Permissions + +type TestClass() = + inherit MarshalByRefObject() + +[] +[] +let main _ = + let obj = TestClass() + + RemotingServices.SetObjectUriForMarshal(obj, "testUri") + RemotingServices.Marshal obj |> ignore + + printfn $"{RemotingServices.GetObjectUri obj}" + 0 \ No newline at end of file diff --git a/xml/System/MarshalByRefObject.xml b/xml/System/MarshalByRefObject.xml index 165da40ceae..b79b4782d9e 100644 --- a/xml/System/MarshalByRefObject.xml +++ b/xml/System/MarshalByRefObject.xml @@ -75,6 +75,7 @@ :::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/CreateInstanceAndUnwrap2/cpp/source.cpp" id="Snippet1"::: :::code language="csharp" source="~/snippets/csharp/System/AppDomain/CreateInstanceAndUnwrap/source.cs" id="Snippet1"::: + :::code language="fsharp" source="~/snippets/fsharp/System/AppDomain/CreateInstanceAndUnwrap/source.fs" id="Snippet1"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/CreateInstanceAndUnwrap2/VB/source.vb" id="Snippet1"::: **Example 2** @@ -83,6 +84,7 @@ :::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/RemotingServices.SetObjectUriForMarshal/CPP/source.cpp" id="Snippet1"::: :::code language="csharp" source="~/snippets/csharp/System/MarshalByRefObject/Overview/source.cs" id="Snippet1"::: + :::code language="fsharp" source="~/snippets/fsharp/System/MarshalByRefObject/Overview/source.fs" id="Snippet1"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/RemotingServices.SetObjectUriForMarshal/VB/source.vb" id="Snippet1"::: ]]> @@ -293,6 +295,7 @@ For more information about lifetime services, see the From 230d02c4d9b45f4ca306ecfeae96e0f1c4308437 Mon Sep 17 00:00:00 2001 From: albert-du <52804499+albert-du@users.noreply.github.com> Date: Mon, 28 Feb 2022 20:28:39 -0800 Subject: [PATCH 2/2] fix broken snippet ref --- snippets/fsharp/System/MarshalByRefObject/Overview/source.fs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/snippets/fsharp/System/MarshalByRefObject/Overview/source.fs b/snippets/fsharp/System/MarshalByRefObject/Overview/source.fs index 8ccb19171aa..368e096d1a6 100644 --- a/snippets/fsharp/System/MarshalByRefObject/Overview/source.fs +++ b/snippets/fsharp/System/MarshalByRefObject/Overview/source.fs @@ -15,4 +15,5 @@ let main _ = RemotingServices.Marshal obj |> ignore printfn $"{RemotingServices.GetObjectUri obj}" - 0 \ No newline at end of file + 0 +// \ No newline at end of file