Using Interfaces In .NET Remoting

Page 2 of 4
  1. Introduction
  2. Implementation & Server Object
  3. Creating the Client
  4. Conclusion

Implementation & Server Object

Now that we have defined the interfaces we plan on using, we need to create an implementation of these interfaces that does something. In a real project, you would create another shared library with you implementation to aid code reuse and keep your project modular. In this simple example, we'll just create a console application that provides an inline implementation of our interfaces.

Go to File->New->Project and choose C# Console application, then click on OK.

Since we are making use of the System.Runtime.Remoting namespace, we will need to add a referance to the correct DLL. This is done by clicking Project->Add Referance, click on the ".NET" tab and chose System.Runtime.Remoting.dll.

We also need to add a referance to the interfaces DLL we created in Step 1. This is done by choosing Project->Add Referance, click "Browse" and choose the .DLL file in the bin/Debug directory of the RemotingExampleInterfaces project we created in Step 1.

public class Resume : MarshalByRefObject, IResume
public class RemotingExampleService : MarshalByRefObject, IRemotingExampleService

Take a look at these two class declarations. Each is a subclass of the MarshalByRefObject type. This is very important to the use of these objects in .NET remoting. Also, each of these objects is an implementation of the IResume and IRemotingExampleService interfaces we created in Step 1. Note that in these classes are methods that match the names of the methods we defined in our interface.

If you have questions about the TcpServerChannel or related topics, check out my article entitled, "Introduction to .NET Remoting" hosted on this site.

The complete code for the object is below.

using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using RemotingExampleInterfaces;
using System.Runtime.Serialization;

namespace RemotingInterfaceServer
{
  public class EntryPoint
  {
    public static void Main(string[] args)
    {
      TcpServerChannel channel = new TcpServerChannel(9988);
      ChannelServices.RegisterChannel(channel);
      RemotingConfiguration.RegisterWellKnownServiceType(
          typeof(RemotingExampleService),
          "RemotingExampleService", WellKnownObjectMode.SingleCall);
      System.Console.WriteLine("Press Any Key");
      System.Console.ReadLine();
    }
  }

  public class RemotingExampleService : MarshalByRefObject, IRemotingExampleService
  {
    public RemotingExampleService() {}

    public IResume GetResume()
    {
      Resume resume = new Resume();
      resume.FirstName="David";
      resume.LastName="Talbot";
      resume.Title="Senior Software Architect";
      resume.Body="Did some cool stuff";
      return (IResume)resume;
    }

    public String GetFormattedResume()
    {
      return GetResume().GetFormattedResume();
    }
  }

  [Serializable]
  public class Resume : MarshalByRefObject, IResume
  {
    public String FirstName, LastName, Body, Title;

    public String GetFormattedResume()
    {
      System.Text.StringBuilder sb = new System.Text.StringBuilder();
      sb.Append("*"+ GetFormattedName() +"*\n");
      sb.Append("--"+ Title +"--\n");
      sb.Append("--------------------------\n");
      sb.Append(Body);
      return sb.ToString();
    }

    public String GetFormattedName()
    {
      return FirstName +" "+ LastName;
    }
  }//END OF Resume Object
}

Compile this program by going to Build->Build and note the location of the generated executable file found in the /bin/debug subdirectory of this project.

You might also like...

Comments

About the author

David Talbot United States

David Talbot is an experienced Software Architect with a diverse background including creating network applicances, working with television set top boxes, building Billing/CRM systems, Web Port...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Walking on water and developing software from a specification are easy if both are frozen.” - Edward V Berard