0

I have a class in an assembly that I wish to be a singleton. I want it to be created by my Server application then allow Another application on the same machine to access this class instance.

I am using this lazy loading implementation to ensure only one instance exists:

Private Shared ReadOnly _instance As New Lazy(Of PersonX)(Function() New PersonX(), System.Threading.LazyThreadSafetyMode.ExecutionAndPublication)

Public Shared ReadOnly Property Instance() As PersonX
    Get
        Return _instance.Value
    End Get
End Property

And then I am using a Private Sub New with the following remoting code:

Dim uri As String = String.Format("tcp://{0}:{1}/PersonX", "localhost", 1976)
RemotingServices.Marshal(Person.Instance, uri)
RemotingConfiguration.RegisterActivatedServiceType(GetType(PersonX))

The in my Calling code I create my first instance with:

Dim PersonX as SingletonTestClass.PersonX = SingletonTestClass.PersonX.Instance

I am using the following code to connect to the instance using remoting:

Dim newPerson As SingletonTestClass.PersonX
Dim uri As String = String.Format("tcp://{0}:{1}/PersonX", "localhost", 1976)
newPerson = CType(Activator.GetObject(GetType(SingletonTestClass.PersonX), uri), SingletonTestClass.PersonX)

But now when I try to access the object properties I get an error Requested Service not found on this line:

newPerson.Name = "Fred"
Matt Wilko
  • 26,994
  • 10
  • 93
  • 143
  • Just curious, any particular reason you're using Remoting instead of WCF? – rro Nov 22 '12 at 15:33
  • @raymond - I had been led to believe that remoting was simpler to implement and faster in execution – Matt Wilko Nov 22 '12 at 15:35
  • I've had experience in both and I'd prefer WCF. Our large projects have been converted to WCF, binary over TCP. – rro Nov 22 '12 at 15:48

1 Answers1

0

To answer your question, you need to register a channel before you marshal your Remoting object:

See simple, working C# example:

public class Test : MarshalByRefObject
    {
        public string Echo(string message)
        {
            return "Echo: " + message;
        }
    }

private void button1_Click(object sender, EventArgs e)
        {
            ChannelServices.RegisterChannel(new TcpChannel(1234));
            Test test1 = new Test();
            RemotingServices.Marshal(test1, "TestService");

            Test test2 = (Test)Activator.GetObject(typeof(Test), "tcp://localhost:1234/TestService");
            MessageBox.Show(test2.Echo("Hey!"));
        }

Ensure firewall is not blocking port.

rro
  • 619
  • 5
  • 22