I am using the simplest example of remoting that I could find, sharing an object between a windows service and a windows forms program (client), running on the same machine.
The service instantiates the object like this:
serviceConfigRemote = new serviceConfigDataRemote();
serverChannel = new TcpServerChannel(9090);
ChannelServices.RegisterChannel(serverChannel, false);
RemotingServices.Marshal(this.serviceConfigRemote, "ServiceConfigData");
The client establishes a connection like this:
TcpClientChannel channel = new TcpClientChannel();
ChannelServices.RegisterChannel(channel, false);
configData = (serviceConfigDataRemote)Activator.GetObject(typeof(serviceConfigDataRemote), "tcp://localhost:9090/ServiceConfigData");
The idea is for the service to be able to make changes to some of the parameters of the object, for the client to be able to read those changes.
The object itself is:
public sealed class serviceConfigDataRemote : MarshalByRefObject
{
private bool myConnectedFlag;
private bool mySendingFlag;
private bool myUpdateFlag;
private string myClientConfiguration;
static readonly serviceConfigDataRemote instance = new serviceConfigDataRemote();
static serviceConfigDataRemote()
{
}
public serviceConfigDataRemote()
{
myConnectedFlag = false;
mySendingFlag = false;
myUpdateFlag = false;
myClientConfiguration = "";
}
public static serviceConfigDataRemote Instance
{
get
{
return instance;
}
}
public override object InitializeLifetimeService()
{
return (null);
}
public bool Connected
{
get { return myConnectedFlag; }
set { myConnectedFlag = value; }
}
public bool Sending
{
get { return mySendingFlag; }
set { mySendingFlag = value; }
}
public bool CheckForUpdates
{
get{return myUpdateFlag;}
set { myUpdateFlag = value; }
}
public string ClientConfiguration
{
get { return myClientConfiguration; }
set { myClientConfiguration = value; }
}
}
While the service is running by itself, the Mem Usage in Task Manager stays constant, even though the service is continually updating the object with status information. When the client is started, both begin to increase in Mem Usage, and never go down.
This is the problem that I referred to in My Previous Question about finding memory leaks.
It is appearing differently on different machines, some show no memory increases, but the machines that do will reliably reproduce this problem. Running .NET Memory Profiler shows that on the service, there is an ever increasing number of "New instances", with only one or two "Removed" in the tab Types/Resources where Namespace/System is Kernel and Name/Resource is HeapMemory. I'm still trying to learn how to use the Memory Profiler, so I apologize if this is the wrong information, and tip on where else I should be looking would also be appreciated.
This object is instantiated once, with just a couple of parameters to read and write, no file io, no allocating of memory that I can see, and yet my memory usage only appears to go up the moment I start a connection from the client to that object and read its values. Any and all input would be appreciated, as I would like to avoid pulling this code and replacing it with named pipes or similar, but I'm quickly approaching that point as my only option.
Shouldn't where your service instantiates the object,
serviceConfigRemote = new serviceConfigDataRemote();
look like
serviceConfigRemote = serviceConfigDataRemote.Instance;
instead?
At the very least, the way you have it, you're creating two different instances on the server side, one in the static instance member initializer to be used by the Instance property and another one via the new serviceConfigDataRemote() explicit construction. It may also serve you well to add a private constructor to that class so nothing else can instantiate the singleton other than the static initializer.
This may not be the solution to the ever-increasing memory, but it definitely appears to be something of an issue to address.
EDIT:
Here are a couple more tips I found scouring the 'nets:
Add [MTAThread] to the main method of the host service.
RemotingServices.Disconnect(this.serviceConfigRemote); when you're shutting down the host service.
Hope this may assist.
Have you tried using lazy instantiation on your Singleton. It's possible that it doesn't like the way you're instantiating it.
public sealed class serviceConfigDataRemote : MarshalByRefObject
{
private bool myConnectedFlag;
private bool mySendingFlag;
private bool myUpdateFlag;
private string myClientConfiguration;
static serviceConfigDataRemote instance;
static serviceConfigDataRemote()
{
}
public serviceConfigDataRemote()
{
myConnectedFlag = false;
mySendingFlag = false;
myUpdateFlag = false;
myClientConfiguration = "";
}
public static serviceConfigDataRemote Instance
{
get
{
if (instance == null)
{
lock (new Object())
{
if (instance == null)
{
instance = new serviceConfigDataRemote();
}
return instance;
}
}
return instance;
}
}
public override object InitializeLifetimeService()
{
return (null);
}
public bool Connected
{
get { return myConnectedFlag; }
set { myConnectedFlag = value; }
}
public bool Sending
{
get { return mySendingFlag; }
set { mySendingFlag = value; }
}
public bool CheckForUpdates
{
get { return myUpdateFlag; }
set { myUpdateFlag = value; }
}
public string ClientConfiguration
{
get { return myClientConfiguration; }
set { myClientConfiguration = value; }
}
}
Since the only OS you are seeing this bug in is XP, there are a couple possible issues.
XP has a incoming connection limit of 10 (on pro) or 5 (on home) , and this could play a part in the issue.
Ensure that all service packs/patches are installed. I know this may be a corny and cliche answer to any problems, but the fact this issue only appears in XP implies it is OS related.
Also, not sure how you're using the service, but Windows XP is a desktop OS, not a server OS. If you intend the service to be a server of some type, you really should be using 2000/2003/2008 etc, especially since it only has issues on XP.
Related
I'm working in a WinForm app in 4 layers:
DAL (Data access)
BOL (Bussiness objects)
BAL (Bussiness access)
INT (Intermediate access).
I'm using the Intermediate layer to run any operation needed by the Presentation layer, trying to make it independent, as we can use it in a WinForm, ASP, and so.
I've created a Class that executes those operations like this:
// Clase: ProjectStatusMID
using System.Collections.Generic;
namespace Trevo.FrameWork
{
public class ProjectStatusMID
{
#region Propiedades
private ProjectStatusBOL _Data = new ProjectStatusBOL();
private ProjectStatusBAL _Operations = new ProjectStatusBAL();
private Acciones _Action = Acciones.Nada;
#endregion Propiedades
public ProjectStatusBOL Data
{
get { return _Data; }
set
{
_Data = value;
}
}
public ProjectStatusBAL Operations
{
get { return _Operations; }
set
{
_Operations = value;
}
}
public Acciones Action
{
get { return _Action; }
set
{
_Action = value;
}
}
public int IDProject
{
get { return _Data.IDProject; }
set
{
_Data.IDProject = value;
}
}
public List<Codigos> ProjectsList
{
get { return LoadProjects(); }
}
public ProjectStatusMID()
{
//Load();
}
public void Load()
{
Operations.Consultar(Data);
}
public List<Codigos> LoadProjects()
{
List<Codigos> oRet = new List<Codigos>();
MyProjectsBAL _Operations = new MyProjectsBAL();
MyProjectsBOL _Data = new MyProjectsBOL();
List<MyProjectsBOL> _MyList = _Operations.Lista(_Data);
foreach (MyProjectsBOL o in _MyList)
{
oRet.Add(new Codigos(o.IDProject, o.Project));
}
return oRet;
}
}
}
// Clase: ProjectStatusMID
At the front-end (in this case is WinForm), we are instancing this class as follows:
ProjectStatusMID OO = new ProjectStatusMID();
So, the issue comes when calling one of the methods:
parProject.DataSource = OO.LoadProjects();
Everything is referenced, the app compiles without any problems, the project that contains the class is part of the solution in a separated project (as any other layer), BUT we have the following error:
System.MissingMethodException occurred
HResult=-2146233069
Message=Método no encontrado: 'System.Collections.Generic.List`1 Trevo.FrameWork.ProjectStatusMID.LoadProjects()'.
Source=WorkLoadPresentation
StackTrace:
en Trevo.FrameWork.PS_ProjectStatus_Datos.CargarListas()
en Trevo.FrameWork.PS_ProjectStatus_Datos.PS_ProjectStatus_Datos_Load(Object sender, EventArgs e) en C:\Users\fbravo\OneDrive\Particular_Sistemas\WorkLoad\WorkLoadPresentation\ProjectStatus\PS_ProjectStatus_Datos.cs:línea 25
InnerException:
I've tried to make the class static, re-creating the entire app, deleting the GAC, and so, but a week loose trying different things.
Any help will be appreciated
Could be several issues. The most common one is that you included the DLL library which is the wrong version (e.g. without the method that's missing). Easiest thing to do is to open the exe in the decompiler (e.g. Reflector) and step through it.
Another issue could be the wrong bitness (but probably not).
You have to make sure you referenced the external project dll in your main Winforms application
We're using a library that uses pooled objects (ServiceStack.Redis's PooledRedisClientManager). Objects are created and reused for multiple web requests. However, Dispose should be called after each use to release the object back into the pool.
By default, Ninject only deactivates an object reference if it has not been deactivated before.
What happens is that the pool instantiates an object and marks it as active. Ninject then runs the activation pipeline. At the end of the request (a web request), Ninject runs the deactivation pipeline which calls Dispose (and thus the pool marks the object as inactive). The next request: the first pooled instance is used and the pool marks it as active. However, at the end of the request, Ninject does not run its deactivation pipeline because the ActivationCache has already marked this instance as deactivated (this is in the Pipeline).
Here's a simple sample that we've added in a new MVC project to demonstrate this problem:
public interface IFooFactory
{
IFooClient GetClient();
void DisposeClient(FooClient client);
}
public class PooledFooClientFactory : IFooFactory
{
private readonly List<FooClient> pool = new List<FooClient>();
public IFooClient GetClient()
{
lock (pool)
{
var client = pool.SingleOrDefault(c => !c.Active);
if (client == null)
{
client = new FooClient(pool.Count + 1);
client.Factory = this;
pool.Add(client);
}
client.Active = true;
return client;
}
}
public void DisposeClient(FooClient client)
{
client.Active = false;
}
}
public interface IFooClient
{
void Use();
}
public class FooClient : IFooClient, IDisposable
{
internal IFooFactory Factory { get; set; }
internal bool Active { get; set; }
internal int Id { get; private set; }
public FooClient(int id)
{
this.Id = id;
}
public void Dispose()
{
if (Factory != null)
{
Factory.DisposeClient(this);
}
}
public void Use()
{
Console.WriteLine("Using...");
}
}
public class HomeController : Controller
{
private IFooClient foo;
public HomeController(IFooClient foo)
{
this.foo = foo;
}
public ActionResult Index()
{
foo.Use();
return View();
}
public ActionResult About()
{
return View();
}
}
// In the Ninject configuration (NinjectWebCommon.cs)
private static void RegisterServices(IKernel kernel)
{
kernel.Bind<IFooFactory>()
.To<PooledFooClientFactory>()
.InSingletonScope();
kernel.Bind<IFooClient>()
.ToMethod(ctx => ctx.Kernel.Get<IFooFactory>().GetClient())
.InRequestScope();
}
The solutions that we've come up with thus far are:
Mark these objects as InTransientScope() and use other deactivation mechanism (like an MVC ActionFilter to dispose of the object after each request). We'd lose the benefits of Ninject's deactivation process and require an indirect approach to disposing of the object.
Write a custom IActivationCache that checks the pool to see if the object is active. Here's what I've written so far, but I'd like some one else's eyes to see how robust it is:
public class PooledFooClientActivationCache : DisposableObject, IActivationCache, INinjectComponent, IDisposable, IPruneable
{
private readonly ActivationCache realCache;
public PooledFooClientActivationCache(ICachePruner cachePruner)
{
realCache = new ActivationCache(cachePruner);
}
public void AddActivatedInstance(object instance)
{
realCache.AddActivatedInstance(instance);
}
public void AddDeactivatedInstance(object instance)
{
realCache.AddDeactivatedInstance(instance);
}
public void Clear()
{
realCache.Clear();
}
public bool IsActivated(object instance)
{
lock (realCache)
{
var fooClient = instance as FooClient;
if (fooClient != null) return fooClient.Active;
return realCache.IsActivated(instance);
}
}
public bool IsDeactivated(object instance)
{
lock (realCache)
{
var fooClient = instance as FooClient;
if (fooClient != null) return !fooClient.Active;
return realCache.IsDeactivated(instance);
}
}
public Ninject.INinjectSettings Settings
{
get
{
return realCache.Settings;
}
set
{
realCache.Settings = value;
}
}
public void Prune()
{
realCache.Prune();
}
}
// Wire it up:
kernel.Components.RemoveAll<IActivationCache>();
kernel.Components.Add<IActivationCache, PooledFooClientActivationCache>();
Specifically for ServiceStack.Redis's: use the PooledRedisClientManager.DisposablePooledClient<RedisClient> wrapper so we always get a new object instance. Then let the client object become transient since the wrapper takes care of disposing it. This approach does not tackle the broader concept of pooled objects with Ninject and only fixes it for ServiceStack.Redis.
var clientManager = new PooledRedisClientManager();
kernel.Bind<PooledRedisClientManager.DisposablePooledClient<RedisClient>>()
.ToMethod(ctx => clientManager.GetDisposableClient<RedisClient>())
.InRequestScope();
kernel.Bind<IRedisClient>()
.ToMethod(ctx => ctx.Kernel.Get<PooledRedisClientManager.DisposablePooledClient<RedisClient>>().Client)
.InTransientScope();
Is one of these approaches more appropriate than the other?
I have not use Redis so far so I can not tell you how to do it correctly. But I can give you some input in general:
Disposing is not the only thing that is done by the ActivationPipeline. (E.g. it also does property/method injection and excuting activation/deactivation actions.) By using a custom activation cache that returns false even though it has been activated before will cause that these other actions are executed again (E.g. resulting in property injection done again.)
I have a very simple WCF service running that has a single method that returns an enum based on the result of the method.
I have tested this WCF service through a very simple console app both locally and over the internet to my server and both work perfectly fine. However once I use the, literally exact, code in my WPF application calling the method does nothing. No exception, no timeout. Testing it locally with a breakpoint at the start of the WCF method I found it does not even reach that far when calling it from WPF.
Both service reference configurations that were generated in the app.config are identical between the console and WPF application.
Edit: Had hoped to not need to put as much code in here but I'll just dump the whole thing.
In WPF the code is being called from a button in a dialog. This button triggers an Coroutine call using Caliburn.Micro. The WCF call is then being called in the Execute method from the Coroutine.
public IEnumerator<IResult> SendReport()
{
var result = new Coroutines.SubmitNewExceptionIssueResult(Exception, UserNotes, Character);
yield return result;
if (result.Result == ErrorReportResult.OK)
TryClose();
else
// TODO
}
public class SubmitNewExceptionIssueResult : IResult
{
public event EventHandler<ResultCompletionEventArgs> Completed;
private ErrorReporterClient _client = new ErrorReporterClient();
private Exception _exception { get; set; }
private string _userNotes { get; set; }
private Character _character { get; set; }
public ErrorReportResult Result { get; private set; }
public SubmitNewExceptionIssueResult(Exception ex, string userNotes, Character character)
{
_exception = ex;
_userNotes = userNotes;
_character = character;
}
public void Execute(ActionExecutionContext context)
{
Result = _client.SendErrorReport(JsonConvert.SerializeObject(_exception, new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.All
}),
_userNotes,
JsonConvert.SerializeObject(_character, new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.All
}));
Completed(this, new ResultCompletionEventArgs());
}
}
The cause was indeed threading of some sort. While not happy with the implementation it now works.
Dispatcher.CurrentDispatcher.Invoke(DispatcherPriority.Normal, new System.Action(() => { /* send report code */ }));
I'm calling my custom factory that I created (PhotoServiceFactory), which is a singleton that allows me to get at a specific custom service type back (in this case FacebookService). FacebookService is also a singleton. In FacebookService I've exposed an instance of FacebookAlbumPhoto through a property. I did this because then I don't have to have a ton of the same code over and over again creating a new instance of FacebookAlbumPhoto...I can get an instance using the FacebookService's property.
PhotoServiceFactory service = PhotoServiceFactory.CurrentPhotoServiceFactory;
FacebookService facebookService = (FacebookService)service.GetAPIService(APIType.Facebook);
FacebookAlbumPhoto facebookPhoto = facebookService.FacebookAlbumPhoto.GetFacebookAlbumPhoto(selectedPhotoID);
So this is all set up now, I created all this and just testing it now.
What's happening is my code is bombing out at this line:
FacebookAlbumPhoto facebookPhoto = facebookService.FacebookAlbumPhoto.GetFacebookAlbumPhoto(selectedPhotoID);
The error I get is when I try to reference the facebookService.FacebookAlbumPhoto instance:
CurrentSession = '_singletonInstance.CurrentSession' threw an exception of type 'System.Threading.ThreadAbortException'
So I don't know if it's because the service singleton is on one thread and then it tries to reference another singleton that's on a completely different thread and that's just not possible? That it's not possible to nest singletons like this? Or could this be another issue altogether? Cause I can't see it.
Here's my ServiceFactory:
public class PhotoServiceFactory
{
private static PhotoServiceFactory _singletonInstance;
private PhotoServiceFactory(){}
public static PhotoServiceFactory CurrentPhotoServiceFactory
{
get
{
_singletonInstance = _singletonInstance ?? (_singletonInstance = new PhotoServiceFactory());
return _singletonInstance;
}
}
public object GetAPIService(APIType apiType)
{
object apiService = null;
switch (apiType)
{
case APIType.Facebook:
apiService = FacebookService.CurrentService;
break;
// rest of code
}
return apiService;
}
So the main singleton here Service has a property to get its related Session:
Here's the FacebookServiceClass:
public class FacebookService
{
private static FacebookService _singletonInstance;
private FacebookService(){}
public FacebookSession CurrentSession
{
get
{
return FacebookSession.GetCurrentSession();
}
}
/// <summary>
/// Gets the current facebook service singleton instance.
/// </summary>
/// <value>The current facebook service.</value>
public static FacebookService CurrentService
{
get
{
_singletonInstance = _singletonInstance ?? (_singletonInstance = new FacebookService());
return _singletonInstance;
}
}
public FacebookAlbumPhoto FacebookAlbumPhoto
{
get
{
return new FacebookAlbumPhoto(); // create an instance automatically so we can start working with this object
}
}
}
Here's the session class:
public class FacebookSession
{
const string loginCallbackUrl = "http://localhost/PhotoUpload/FacebookOauth.aspx";
private FacebookSession()
{
}
public string UserID { get; private set; }
public static FacebookSession GetCurrentSession()
{
//....bunch of other logic is here
FacebookSession facebookSession = CreateNewSession();
return facebookSession;
}
public FacebookSession CreateNewSession()
{
//...some code here
FacebookSession newFacebookSession = new FacebookSession
//... rest of code...
return newFacebookSession;
}
// ... rest of code
}
UPDATED:
As requested here's my FacebookAlbumPhoto class that I created:
public class FacebookAlbumPhoto : FacebookPhotoBase
{
private FacebookSession currentSession;
public FacebookAlbumPhoto()
{
currentSession = FacebookService.CurrentService.CurrentSession;
}
#region Methods
public FacebookAlbumPhoto GetFacebookAlbumPhoto(string photoID)
{
...more code
FacebookPhotoRequest request = new FacebookPhotoRequest(currentSession.UserID, photoID);
...more code
FacebookAlbumPhoto facebookPhoto = ParseFacebookPhoto(json);
return facebookPhoto;
}
...rest of code
}
Two things. First, remember to read over Skeet's catalogue of singleton implementations.
Second, try breaking your code just before the spot where the exception occurs, and then bring up your "Exception" dialogue (ctrl-alt-e). Click the "throw" checkbox next to the CLR (second row of dialogue) and hit ok. Continue debugging your code. The results may tell you where the real problem is.
Don't forgot to go back to the Exception dialogue and remove that check from the check box, after you are done. :)
separate instance creation from initialization
I am looking for a class that defines a holding structure for an object. The value for this object could be set at a later time than when this container is created. It is useful to pass such a structure in lambdas or in callback functions etc.
Say:
class HoldObject<T> {
public T Value { get; set; }
public bool IsValueSet();
public void WaitUntilHasValue();
}
// and then we could use it like so ...
HoldObject<byte[]> downloadedBytes = new HoldObject<byte[]>();
DownloadBytes("http://www.stackoverflow.com", sender => downloadedBytes.Value = sender.GetBytes());
It is rather easy to define this structure, but I am trying to see if one is available in FCL. I also want this to be an efficient structure that has all needed features like thread safety, efficient waiting etc.
Any help is greatly appreciated.
Never seen a class like that, but should be pretty simple.
public class ObjectHolder<T>
{
private T value;
private ManualResetEvent waitEvent = new ManualResetEvent(false);
public T Value
{
get { return value; }
set
{
this.value = value;
ManualResetEvent evt = waitEvent;
if(evt != null)
{
evt.Set();
evt.Dispose();
evt = null;
}
}
}
public bool IsValueSet
{
get { return waitEvent == null; }
}
public void WaitUntilHasValue()
{
ManualResetEvent evt = waitEvent;
if(evt != null) evt.WaitOne();
}
}
What you're trying to accomplish feels a lot like a future. Early CTP's of .NET 4.0 TPL had a Future<T> class. With the RTM of .NET 4.0 it has been renamed to Task<T>. If you squint, you can see the resemblance between:
class HoldObject<T>
{
public T Value { get; set; }
public bool IsValueSet();
public void WaitUntilHasValue();
}
and
class Task<T>
{
public T Value { get }
public bool IsCompleted { get; }
public void Wait();
}
If you're not using .NET 4.0 yet, you can download the Reactive Extensions for .NET 3.5sp1. It contains a System.Threading.dll assembly that contains TPL for .NET 3.5.
While the Value is read-only, changing it can of course be done by the return value of the delegate you supply the task. Of course I'm not exactly sure if this meets your requirements, but your example can be written as follows:
var downloadBytesTask = Task<byte[]>.Factory.StartNew(() =>
DownloadBytes("http://www.stackoverflow.com"));
if (!downloadBytesTask.IsCompleted)
{
downloadBytesTask.Wait();
}
var bytes = downloadBytesTask.Value;