Skip to content

Commit

Permalink
Formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
einari committed Feb 29, 2024
1 parent f9d9637 commit 83b8aa1
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 127 deletions.
44 changes: 17 additions & 27 deletions _posts/2007/05/crossappdomainsingleton-update/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,25 @@ categories:
- "csharp"
---

I forgot a couple of very essential things in my [last post about a singleton that lives across appdomains](http://ingebrigtsen.blog/2007/05/18/cross-appdomain-singleton/).
I forgot a couple of very essential things in my [last post about a singleton that lives across appdomains](/2007/05/18/cross-appdomain-singleton/).

When accessing a remote object using remoting, like I did with the singleton implementation, the remoting system obtains a lease for the object. The lease period is implemented in the MarshalByRefObject class through a method called InitializeLifeTimeService(). A singleton like the one we wanted to achieve needs to live forever, therefor you need to override the InitializeLifeTimeService() method and always return null. Returning null means that it should live forever (more details can be at [http://msdn.microsoft.com/msdnmag/issues/03/12/LeaseManager/default.aspx](http://msdn.microsoft.com/msdnmag/issues/03/12/LeaseManager/default.aspx))
When accessing a remote object using remoting, like I did with the singleton implementation, the remoting system obtains a lease for the object. The lease period is implemented in the MarshalByRefObject class through a method called InitializeLifeTimeService(). A singleton like the one we wanted to achieve needs to live forever, therefor you need to override the InitializeLifeTimeService() method and always return null. Returning null means that it should live forever (more details can be found [here](http://msdn.microsoft.com/msdnmag/issues/03/12/LeaseManager/default.aspx))

<table style="background-color:#f2f2f2;border:#e5e5e5 1px solid;" border="0" width="100%" cellspacing="0" cellpadding="0"><tbody><tr style="vertical-align:top;line-height:normal;"><td class="" style="width:40px;text-align:right;"><pre style="border-right:#e7e7e7 1px solid;font-size:11px;margin:0;color:gray;font-family:courier new;padding:2px;">1
2
3
4
5
6
7
8
9
10
11
12
13</pre></td><td class=""><pre style="margin:0;padding:2px 2px 2px 8px;"><span style="font-weight:normal;font-size:11px;color:black;font-family:Courier New;background-color:transparent;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:normal;font-size:11px;color:green;font-family:Courier New;background-color:transparent;">/// &lt;summary&gt;</span>
&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:normal;font-size:11px;color:green;font-family:Courier New;background-color:transparent;">/// Override of lifetime service initialization.</span>
&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:normal;font-size:11px;color:green;font-family:Courier New;background-color:transparent;">/// </span>
&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:normal;font-size:11px;color:green;font-family:Courier New;background-color:transparent;">/// The reason for overriding is to have this singleton live forever</span>
&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:normal;font-size:11px;color:green;font-family:Courier New;background-color:transparent;">/// &lt;/summary&gt;</span>
&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:normal;font-size:11px;color:green;font-family:Courier New;background-color:transparent;">/// &lt;returns&gt;object for the lease period - in our case always null&lt;/returns&gt;</span>
&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:normal;font-size:11px;color:blue;font-family:Courier New;background-color:transparent;">public</span> <span style="font-weight:normal;font-size:11px;color:blue;font-family:Courier New;background-color:transparent;">override</span> <span style="font-weight:normal;font-size:11px;color:blue;font-family:Courier New;background-color:transparent;">object</span> InitializeLifetimeService()
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:normal;font-size:11px;color:green;font-family:Courier New;background-color:transparent;">// We want this singleton to live forever</span>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:normal;font-size:11px;color:green;font-family:Courier New;background-color:transparent;">// In order to have the lease across appdomains live forever,</span>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:normal;font-size:11px;color:green;font-family:Courier New;background-color:transparent;">// we return null.</span>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:normal;font-size:11px;color:blue;font-family:Courier New;background-color:transparent;">return</span> <span style="font-weight:normal;font-size:11px;color:blue;font-family:Courier New;background-color:transparent;">null</span>;
&nbsp;&nbsp;&nbsp;&nbsp;}</span></pre></td></tr></tbody></table>
```csharp
/// <summary>
/// Override of lifetime service initialization.
///
/// The reason for overriding is to have this singleton live forever
/// </summary>
/// <returns>object for the lease period - in our case always null</returns>
public override object InitializeLifetimeService()
{
// We want this singleton to live forever
// In order to have the lease across appdomains live forever,
// we return null.
return null;
}
```

In addition I've made it all threadsafe. So look at the attachment for this post instead..

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,106 +14,58 @@ It all started up yesterday as a simple thing, but I figured that I didn't feel

The module code turns out as follows (I've attached the entire project with source and binaries)

<table style="background-color:#f2f2f2;border:#e5e5e5 1px solid;" border="0" width="100%" cellspacing="0" cellpadding="0"><tbody><tr style="vertical-align:top;line-height:normal;"><td style="width:40px;text-align:right;"><pre style="border-right:#e7e7e7 1px solid;font-size:11px;margin:0;color:gray;font-family:courier new;padding:2px;">1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
</pre></td><td><pre style="margin:0;padding:2px 2px 2px 8px;"><span style="font-weight:normal;font-size:11px;color:black;font-family:Courier New;background-color:transparent;">&nbsp;&nbsp;[Module(<span style="font-weight:normal;font-size:11px;color:#666666;font-family:Courier New;background-color:#e4e4e4;">"SilverlightModule"</span>)]
&nbsp;&nbsp;<span style="font-weight:normal;font-size:11px;color:blue;font-family:Courier New;background-color:transparent;">public</span> <span style="font-weight:normal;font-size:11px;color:blue;font-family:Courier New;background-color:transparent;">class</span> SilverlightModule : BaseModule
&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:normal;font-size:11px;color:blue;font-family:Courier New;background-color:transparent;">private</span> <span style="font-weight:normal;font-size:11px;color:blue;font-family:Courier New;background-color:transparent;">static</span> <span style="font-weight:normal;font-size:11px;color:blue;font-family:Courier New;background-color:transparent;">readonly</span> <span style="font-weight:normal;font-size:11px;color:blue;font-family:Courier New;background-color:transparent;">string</span> SilverlightScript <span style="font-weight:normal;font-size:11px;color:red;font-family:Courier New;background-color:transparent;">=</span> <span style="font-weight:normal;font-size:11px;color:#666666;font-family:Courier New;background-color:#e4e4e4;">"Silverlight"</span>;
&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:normal;font-size:11px;color:blue;font-family:Courier New;background-color:transparent;">private</span> <span style="font-weight:normal;font-size:11px;color:blue;font-family:Courier New;background-color:transparent;">static</span> <span style="font-weight:normal;font-size:11px;color:blue;font-family:Courier New;background-color:transparent;">readonly</span> <span style="font-weight:normal;font-size:11px;color:blue;font-family:Courier New;background-color:transparent;">string</span> SilverlightApplicationScript <span style="font-weight:normal;font-size:11px;color:red;font-family:Courier New;background-color:transparent;">=</span>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:normal;font-size:11px;color:#666666;font-family:Courier New;background-color:#e4e4e4;">"SilverlightApplication"</span>;
<div></div>
&nbsp;&nbsp;&nbsp;&nbsp;[Method(MethodType.PreRender)]
&nbsp;&nbsp;&nbsp;&nbsp;[MethodType(MethodType.PreRender)]
&nbsp;&nbsp;&nbsp;&nbsp;[ApplicationType(ApplicationType.Weblog)]
&nbsp;&nbsp;&nbsp;&nbsp;[ApplicationType(ApplicationType.ContentManagement)]
&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:normal;font-size:11px;color:blue;font-family:Courier New;background-color:transparent;">public</span> <span style="font-weight:normal;font-size:11px;color:blue;font-family:Courier New;background-color:transparent;">string</span> Render(<span style="font-weight:normal;font-size:11px;color:blue;font-family:Courier New;background-color:transparent;">string</span> protocol,<span style="font-weight:normal;font-size:11px;color:blue;font-family:Courier New;background-color:transparent;">string</span> xamlUri)
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:normal;font-size:11px;color:blue;font-family:Courier New;background-color:transparent;">string</span> completeUri <span style="font-weight:normal;font-size:11px;color:red;font-family:Courier New;background-color:transparent;">=</span> protocol <span style="font-weight:normal;font-size:11px;color:red;font-family:Courier New;background-color:transparent;">+</span> <span style="font-weight:normal;font-size:11px;color:#666666;font-family:Courier New;background-color:#e4e4e4;">":"</span> <span style="font-weight:normal;font-size:11px;color:red;font-family:Courier New;background-color:transparent;">+</span> xamlUri;
<div></div>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Page page <span style="font-weight:normal;font-size:11px;color:red;font-family:Courier New;background-color:transparent;">=</span> MethodContext.Current.Page;
<div></div>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:normal;font-size:11px;color:blue;font-family:Courier New;background-color:transparent;">if</span> (!page.ClientScript.
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IsClientScriptBlockRegistered(SilverlightScript))
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;page.ClientScript.
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RegisterClientScriptBlock(&nbsp;&nbsp;<span style="font-weight:normal;font-size:11px;color:blue;font-family:Courier New;background-color:transparent;">typeof</span>(SilverlightModule),
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SilverlightScript,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Resources.Silverlight, <span style="font-weight:normal;font-size:11px;color:blue;font-family:Courier New;background-color:transparent;">true</span>);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
<div></div>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:normal;font-size:11px;color:blue;font-family:Courier New;background-color:transparent;">if</span> (!page.ClientScript.
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IsClientScriptBlockRegistered(SilverlightApplicationScript))
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;page.ClientScript.
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RegisterClientScriptBlock(&nbsp;&nbsp;<span style="font-weight:normal;font-size:11px;color:blue;font-family:Courier New;background-color:transparent;">typeof</span>(SilverlightModule),
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SilverlightApplicationScript,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Resources.SilverlightApplication,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:normal;font-size:11px;color:blue;font-family:Courier New;background-color:transparent;">true</span>);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
<div></div>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Guid applicationGuid <span style="font-weight:normal;font-size:11px;color:red;font-family:Courier New;background-color:transparent;">=</span> Guid.NewGuid();
<div></div>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:normal;font-size:11px;color:blue;font-family:Courier New;background-color:transparent;">string</span> hostName <span style="font-weight:normal;font-size:11px;color:red;font-family:Courier New;background-color:transparent;">=</span> <span style="font-weight:normal;font-size:11px;color:#666666;font-family:Courier New;background-color:#e4e4e4;">"SilverlightControlHost_"</span>+applicationGuid.ToString();
<div></div>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;completeUri <span style="font-weight:normal;font-size:11px;color:red;font-family:Courier New;background-color:transparent;">=</span> completeUri.Trim();
<div></div>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:normal;font-size:11px;color:blue;font-family:Courier New;background-color:transparent;">return</span>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:normal;font-size:11px;color:#6 0;font-family:Courier New;background-color:#e4e4e4;">"&lt;div id=""</span> <span style="font-weight:normal;font-size:11px;color:red;font-family:Courier New;background-color:transparent;">+</span> hostName <span style="font-weight:normal;font-size:11px;color:red;font-family:Courier New;background-color:transparent;">+</span> <span style="font-weight:normal;font-size:11px;color:#666666;font-family:Courier New;background-color:#e4e4e4;">"" style="background-color:Black" &gt;"</span> <span style="font-weight:normal;font-size:11px;color:red;font-family:Courier New;background-color:transparent;">+</span>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:normal;font-size:11px;color:#666666;font-family:Courier New;background-color:#e4e4e4;">"&lt;script type="text/javascript" style="background-color:Black"&gt;"</span> <span style="font-weight:normal;font-size:11px;color:red;font-family:Courier New;background-color:transparent;">+</span>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:normal;font-size:11px;color:#666666;font-family:Courier New;background-color:#e4e4e4;">"createSilverlight(""</span>+completeUri+<span style="font-weight:normal;font-size:11px;color:#666666;font-family:Courier New;background-color:#e4e4e4;">"",""</span>+applicationGuid.ToString()<span style="font-weight:normal;font-size:11px;color:red;font-family:Courier New;background-color:transparent;">+</span><span style="font-weight:normal;font-size:11px;color:#666666;font-family:Courier New;background-color:#e4e4e4;">"");"</span> <span style="font-weight:normal;font-size:11px;color:red;font-family:Courier New;background-color:transparent;">+</span>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:normal;font-size:11px;color:#666666;font-family:Courier New;background-color:#e4e4e4;">"&lt;/script&gt;"</span> <span style="font-weight:normal;font-size:11px;color:red;font-family:Courier New;background-color:transparent;">+</span>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:normal;font-size:11px;color:#666666;font-family:Courier New;background-color:#e4e4e4;">"&lt;/div&gt;"</span>;
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;}</span></pre></td></tr></tbody></table>
```csharp
[Module("SilverlightModule")]
public class SilverlightModule : BaseModule
{
private static readonly string SilverlightScript = "Silverlight";
private static readonly string SilverlightApplicationScript =
"SilverlightApplication";

[Method(MethodType.PreRender)]
[MethodType(MethodType.PreRender)]
[ApplicationType(ApplicationType.Weblog)]
[ApplicationType(ApplicationType.ContentManagement)]
public string Render(string protocol,string xamlUri)
{
string completeUri = protocol + ":" + xamlUri;

Page page = MethodContext.Current.Page;

if (!page.ClientScript.
IsClientScriptBlockRegistered(SilverlightScript))
{
page.ClientScript.
RegisterClientScriptBlock( typeof(SilverlightModule),
SilverlightScript,
Resources.Silverlight, true);
}

if (!page.ClientScript.
IsClientScriptBlockRegistered(SilverlightApplicationScript))
{
page.ClientScript.
RegisterClientScriptBlock( typeof(SilverlightModule),
SilverlightApplicationScript,
Resources.SilverlightApplication,
true);
}

Guid applicationGuid = Guid.NewGuid();

string hostName = "SilverlightControlHost_"+applicationGuid.ToString();

completeUri = completeUri.Trim();

return
"<div id="" + hostName + "" style="background-color:Black" >" +
"<script type="text/javascript" style="background-color:Black">" +
"createSilverlight(""+completeUri+"",""+applicationGuid.ToString()+"");" +
"</script>" +
"</div>";
}
}
```

![CommunityServermoduleforembeddi.1toposts_780E_image_1](images/CommunityServermoduleforembeddi.1toposts_780E_image_1.png)

Expand Down

0 comments on commit 83b8aa1

Please sign in to comment.