Library tutorials & articles
Using Interfaces In .NET Remoting
- Introduction
- Implementation & Server Object
- Creating the Client
- 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.
Related articles
Related discussion
-
Binary Studio | software development outsourcing Ukraine
by shane124 (4 replies)
-
Research topic in software
by reachsangeethamathew (0 replies)
-
career improvement advice
by hnasr82 (0 replies)
-
Advice on studying and preparing for interviews
by caryatid (0 replies)
-
Chart insertation in a windows form...
by pdhanik (1 replies)
Related podcasts
-
More jQuery in ASP.NET
In this episode Chris Brandsma, Rick Strahl, Dave Ward, Bertrand Le Roy, and Scott Koon conclude their discussion of Microsoft's jQuery in ASP.NET announcement1.This episode of the Alt.NET Podcast is brought to you by LLBLGen Pro, the most mature O/R mapper and code generator out there.Are ...
Events coming up
-
Nov
18
15 Minutes of Fame
Dresher, United States
This is a yearly tradition. We select 10 of the favorite speakers from monthly meetings, code camps, and hands on labs. Each one does a 15 minute talk on their favorite .NET technology. This is our 10th anniversary so we plan a gala event with special prizes and refreshments.
Hi,
I too have the same problem. Did you find any solution for that?
Regards
Paul
-----------------------------------VB Code------------------------------------------------------------
Sub Main()
ChannelServices.RegisterChannel(New HttpChannel())
Dim factory As RemotingClientsInterfaces.IAccountantExamFactory 'this is the object Im tring to remote
'this is the line where the excaption accure
factory = CType(Activator.GetObject( _
Type.GetType("RemotingClientsInterfaces.IAccountantExamFactory"), _
"http://10.0.0.172:8080/IExamFactory"), RemotingClientsInterfaces.IAccountantExamFactory)
.
.
.
---------------------------end of Code-------------------------------------------------------------------
See anything suspicious ???
Its time for me to be more specific.
I wrote a server client application in C# using remoting
and it worked. Then I replaced the client with another
clients written in VB.NET and got an exception while trying to create the remote object:
"An unhandled exception of
type 'System.ArgumentNullException' occurred in
mscorlib.dll
Additional information: Value cannot be null."
do you have any suggestions what so ever ???
My E-Mail: assaf@nipendo.com
Thanks
Unless a developer get the comparision between two technologies he/she may not choose the new one.
Ghanshyam.
Hi,
Can you please pass me the Client application source code for your article .Net Remoting. I think you forgot to upload in your site at http://www.developerfusion.com/show/2082/3/
Can you please forward the source code to my e-mail ID @ r_nazermd@yahoo.com
Thanks and Regards
Nazer Mohamed
Good article. The client side code was missing until I went to the printer friendly version. I ran the code and it worked as expected.
I have a couple of questions about interfaces. Is it possible to initialize a type "such as byte [] array" within the main program and allow the interfaces to access that initialized type so that the clients could do something with it? I thought that the use of interfaces would be a good tool for clients to use to get information from a server that is constantly polling external data. The only examples that I have seen so far, show me that the main thread is completely separate from the interfaces. Is this true?
Tom McDaniel
This thread is for discussions of Using Interfaces In .NET Remoting.