allanmark
asked on
Creating a Singleton class
Greetings all
I am trying to create a SIngleton class (see attached) and get complaints: "Error 3 A get or set accessor expected ".
What have I missed?
in advance, thanks!!!
allanmark
I am trying to create a SIngleton class (see attached) and get complaints: "Error 3 A get or set accessor expected ".
What have I missed?
in advance, thanks!!!
allanmark
attatchment ?
ASKER
Sorry .....
BTW, once the cllass is correctly defined, how do I call / insntiate in my form?
BTW, once the cllass is correctly defined, how do I call / insntiate in my form?
using System;
using System.Collections.Generic;
using System.Text;
namespace BranchPingService
{
public class PingService: IPingService
{
#region Singleton
private static volatile PingService sinPingService;
private PingService()
{
}
public static PingService SinPingService
{
get
{
if (sinPingService == null)
{
sinPingService = new PingService();
}
}
return sinPingService; // HERE IS WHERE THE ERROR IS HAPPENING?
}
#endregion
public bool PingBranch()
{
List<BranchDetail> branches;
try
{
BranchPingLogic logic = new BranchPingLogic();
//List<BranchDetail> branches = logic.GetOpenBranches();
branches = logic.GetOpenBranches();
branches.ForEach(delegate(BranchDetail branch)
{
PingReply pingRep = logic.PingBranch(branch.IpAddress);
if (pingRep != null)
{
bool isBranchCurrentlyUp = pingRep.Status == IPStatus.Success;
if ((isBranchCurrentlyUp && !branch.IsUp) ||
(!isBranchCurrentlyUp && branch.IsUp))
{
logic.WriteBranchHistory(branch.ID, isBranchCurrentlyUp);
if (!isBranchCurrentlyUp)
{
logic.SendBranchDownNotifications(branch);
}
}
}
});
return ("Ping completed at: " + DateTime.Now.TimeOfDay.ToString());
}
catch (Exception ex)
{
IRPLogger logger = RPLoggerFactory.CreateLogger();
logger.Log(ex, "Could not ping the branches");
return "Ping failed!";
}
}
}
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
'PingService.SinPingServic e' to acess it
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Fantatstic!! ThankYou everyone!!!
Can I be really cheeky and ask for a comment on this code, forwarded to me by a colleague - I'm curious as to what the locking is about
private static object _syncRoot = new object();
get
{
// Check that the instance is null
if (_instance == null)
{
// Lock the object
lock (_syncRoot)
{
// Check to make sure its null
if (_instance == null)
{
_instance = new PublicationDataAccess();
}
}
}
// Return the non-null instance of Singleton
return _instance;
}
Can I be really cheeky and ask for a comment on this code, forwarded to me by a colleague - I'm curious as to what the locking is about
private static object _syncRoot = new object();
get
{
// Check that the instance is null
if (_instance == null)
{
// Lock the object
lock (_syncRoot)
{
// Check to make sure its null
if (_instance == null)
{
_instance = new PublicationDataAccess();
}
}
}
// Return the non-null instance of Singleton
return _instance;
}
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Some syntax error in my code
public class PingService: IPingService
{
...
private static volatile PingService sinPingService = new PingService();
private PingService()
{
}
public static PingService SinPingService
{
get
{
return sinPingService;
}
}
...
}
ASKER
Thankyou both!!
philipjonathan:- thanks for the simplified code -- unfortrunately taht want me to do the lockign thingy!
I am happy that my questions has been fully (more than so, in fact) > I now have another issue which I fully appreciate is a separate question. Howeve, for continuity, I'd like to keep this together and increase the points. If you'd prefer a separate question, I will do such ... your help has been great and I'd liek to keep the flow going.
I have now added teh following code to my Console/ Hosting application (the Pingservice) forms part of a WCF service) - see attachgement and tougth all was well.
However, when I compile dteh solution I get this
ON THE PINGSERVICE: "Error 3 Program 'D:\Allan\\BranchPing\Bran chPingServ ice\obj\Re lease\Bran chPingServ ice.exe' does not contain a static 'Main' method suitable for an entry point BranchPingService"
ON THE HOST:
Error 4 Metadata file 'D:\Allan\\BranchPing\Bran chPingServ ice\bin\Re lease\Bran chPingServ ice.exe' could not be found BranchPingHost
Thoughts?
philipjonathan:- thanks for the simplified code -- unfortrunately taht want me to do the lockign thingy!
I am happy that my questions has been fully (more than so, in fact) > I now have another issue which I fully appreciate is a separate question. Howeve, for continuity, I'd like to keep this together and increase the points. If you'd prefer a separate question, I will do such ... your help has been great and I'd liek to keep the flow going.
I have now added teh following code to my Console/ Hosting application (the Pingservice) forms part of a WCF service) - see attachgement and tougth all was well.
However, when I compile dteh solution I get this
ON THE PINGSERVICE: "Error 3 Program 'D:\Allan\\BranchPing\Bran
ON THE HOST:
Error 4 Metadata file 'D:\Allan\\BranchPing\Bran
Thoughts?
HOSTING CODE
ServiceHost<BranchPingService.PingService> host = ServiceHost<BranchPingService.PingService>();
if (host != null)
{
host.Open();
Console.WriteLine("PingService is listening. Press enter to exit...");
Console.ReadLine();
host.Close();
}
else
{
Console.WriteLine("Unable to instantiate service. Press enter to exit...");
Console.ReadLine();
}
PINGSERVICE:
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.NetworkInformation;
using System.Configuration;
using RPLogger;
namespace BranchPingService
{
public sealed class PingService : IPingService
{
#region Singleton
private static volatile PingService _instance;
// Static synchronization root object, for locking
private static object _syncRoot = new object();
private PingService()
{
}
public static PingService Instance
{
get
{
// Check that the instance is null
if (_instance == null)
{
// Lock the object
lock (_syncRoot)
{
// Check to make sure its null
if (_instance == null)
{
_instance = new PingService();
}
}
}
// Return the non-null instance of Singleton
return _instance;
}
}
#endregion
public bool PingBranch()
{
List<BranchDetail> branches;
try
{
BranchPingLogic logic = new BranchPingLogic();
//List<BranchDetail> branches = logic.GetOpenBranches();
branches = logic.GetOpenBranches();
branches.ForEach(delegate(BranchDetail branch)
{
PingReply pingRep = logic.PingBranch(branch.IpAddress);
if (pingRep != null)
{
bool isBranchCurrentlyUp = pingRep.Status == IPStatus.Success;
if ((isBranchCurrentlyUp && !branch.IsUp) ||
(!isBranchCurrentlyUp && branch.IsUp))
{
logic.WriteBranchHistory(branch.ID, isBranchCurrentlyUp);
if (!isBranchCurrentlyUp)
{
logic.SendBranchDownNotifications(branch);
}
}
}
});
return true;
}
catch (Exception ex)
{
IRPLogger logger = RPLoggerFactory.CreateLogger();
logger.Log(ex, "Could not ping the branches");
return false;
}
}
} // public sealed class PingService : IPingService
}
Well, this time the question is not related to the main question. And since you are a premium service member, it will not harm you to open another question. And for the benefit of clarity to others who would read this thread in the future, I'd suggest you do so. It's up to other experts if they still want to reply through this thread though
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Did that. Got:
Error 3 'ServiceModelEx.ServiceHos t<BranchPi ngService. PingServic e>' is a 'type' but is used like a 'variable' D:\Allan\BranchPing\Branch PingServic e\PingServ ice.cs 94 63 BranchPingService
Error 3 'ServiceModelEx.ServiceHos
ServiceHost<PingService> host = ServiceHost<PingService>() ;
and singleton patter u shud use the process class which actually does the process.
and singleton patter u shud use the process class which actually does the process.
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Many thanks for the excellent support!!