Thanks to visit codestin.com
Credit goes to github.com

Skip to content

System.MarshalByRefObject F# snippets #7778

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="source.fs" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//<Snippet1>
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<Worker>.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"
//</Snippet1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net48</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="source.fs" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
open System
open System.Runtime.Remoting.Lifetime
open System.Security.Permissions

// <Snippet1>
type MyClass() =
inherit MarshalByRefObject()

[<SecurityPermissionAttribute(SecurityAction.Demand, Flags=SecurityPermissionFlag.Infrastructure)>]
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
// </Snippet1>
10 changes: 10 additions & 0 deletions snippets/fsharp/System/MarshalByRefObject/Overview/fs.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net48</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="source.fs" />
</ItemGroup>
</Project>
19 changes: 19 additions & 0 deletions snippets/fsharp/System/MarshalByRefObject/Overview/source.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// <Snippet1>
open System
open System.Runtime.Remoting
open System.Security.Permissions

type TestClass() =
inherit MarshalByRefObject()

[<SecurityPermission(SecurityAction.Demand, Flags=SecurityPermissionFlag.RemotingConfiguration)>]
[<EntryPoint>]
let main _ =
let obj = TestClass()

RemotingServices.SetObjectUriForMarshal(obj, "testUri")
RemotingServices.Marshal obj |> ignore

printfn $"{RemotingServices.GetObjectUri obj}"
0
// </Snippet1>
3 changes: 3 additions & 0 deletions xml/System/MarshalByRefObject.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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**
Expand All @@ -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":::

]]></format>
Expand Down Expand Up @@ -293,6 +295,7 @@ For more information about lifetime services, see the <xref:System.Runtime.Remot

:::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR_Classic/classic MarshalByRefObject.InitializeLifetimeService Example/CPP/source.cpp" id="Snippet1":::
:::code language="csharp" source="~/snippets/csharp/System/MarshalByRefObject/InitializeLifetimeService/source.cs" id="Snippet1":::
:::code language="fsharp" source="~/snippets/fsharp/System/MarshalByRefObject/InitializeLifetimeService/source.fs" id="Snippet1":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_Classic/classic MarshalByRefObject.InitializeLifetimeService Example/VB/source.vb" id="Snippet1":::

]]></format>
Expand Down