Library tutorials & articles
Introducing .NET Remoting
- Introduction
- Creating the shared library
- The server object
- The remote client
- Testing it out
Creating the shared library
Click on File->New->Project. Choose to create a new "C# Library" and name it ResumeServerLibrary then click on OK. This will create the "shared vocabulary" that both our .NET Remote client and Server will use to communicate.
The full code is below, if you would like to skip the database access portions, replace the ResumeLoader object with:
public class ResumeLoader : System.MarshalByRefObject
{
public ResumeLoader()
{
System.Console.WriteLine("New Referance Added!");
}
public Resume GetResumeByUserID(decimal userID)
{
return new Resume(1);
}
}
The namespaces required for the object. Please remember, if you're getting errors that the System.Runtime.Remoting.Channels.Tcp namespace does not exist, make sure you added the referance to System.Runtime.Remoting.dll by going to Project->Add Referance.
using System; using System.Runtime; using System.Data.SqlClient;
The namespace we're using for this particular object is DotNetRemoteTest, the object below is a MarshalByRefObject which means that instead of passing ResumeLoader over the wire, we're creating a referance and all of the work including the database work happens completely on the server side.
namespace DotNetRemoteTest
{
public class ResumeLoader : System.MarshalByRefObject
{
private SqlConnection dbConnection;
public ResumeLoader()
{
this.dbConnection = new System.Data.SqlClient.SqlConnection();
this.dbConnection.ConnectionString =
"data source=GRIMSAADO2K;initial catalog=underground;integrated security=SSPI;pers" +
"ist security info=True;workstation id=GRIMSAADO2K;packet size=4096";
/*Your connection string will be different. Database connections are beyond the scope of this article
*If you do not know how to create a database connection, please use the alternate version of this object */
System.Console.WriteLine("New Referance Added!");
}
public Resume GetResumeByUserID(decimal userID)
{
Resume resume = new Resume();
try
{
dbConnection.Open();
SqlCommand cmd = new SqlCommand(
"SELECT ResumeID, UserID, Title, Body FROM Resume as theResume WHERE theResume.UserID="+ userID +""
, dbConnection
);
SqlDataReader aReader = cmd.ExecuteReader();
if(aReader.Read())
{
resume.ResumeID=aReader.GetDecimal(0);
resume.UserID=aReader.GetDecimal(1);
resume.Title=aReader.GetString(2);
resume.Body=aReader.GetString(3);
}
aReader.Close();
dbConnection.Close();
}
catch(Exception x) { resume.Title="Error:"+x; }
return resume;
}
}
The Resume object needs to be serializable in order to be a return type of a remotely referanced .NET Remote object. The reason for this is the object will have to be turned into raw data to be passed over the network, then re-assembled into an object again on the other end.
This object is extremely simple, and to add to the simplicity of this tutorial, the constructor even initializes the fields with some default content.
[Serializable]
public class Resume
{
private decimal resumeID, userID;
private String body, title;
public Resume(decimal resumeID)
{
this.ResumeID=resumeID;
this.UserID=1;
this.Body="This is the default body of the resume";
this.Title="This is the default Title";
}
public decimal ResumeID
{
get { return resumeID; }
set { this.resumeID=value; }
}
public decimal UserID
{
get { return userID; }
set { this.userID=value; }
}
public String Body
{
get { return body; }
set
{
this.body=value;
}
}
public String Title
{
get { return title; }
set
{ this.title=value; }
}
}//END OF RESUME OBJECT
}//END OF DotNetRemoteTest namespace
Compile this project and you should have a DLL avaiable now to be added to your other assemblies.
Related articles
Related discussion
-
Binary Studio | software development outsourcing Ukraine
by shane124 (4 replies)
-
Chart insertation in a windows form...
by pdhanik (1 replies)
-
Point of Sale Developers: Hardware & C# SDK
by ManiGovindan (7 replies)
-
help with the remote frame buffer protocol from real VNC
by poison (0 replies)
-
Need help making a complete program editable, C# or .net I think
by davelee (1 replies)
Related podcasts
-
A Practical Look at Silverlight 2 Part 1
Now that Silverlight 2 is at the Olympics and making a big splash, we wanted to explore this fascinating technology more. Microsoft Silverlight 2 is a cross-browser, cross-platform, and cross-device plug-in for delivering the next generation of .NET based media experiences and rich interactive ap...
Events coming up
-
Dec
9
GL.net Group Meeting - December 2009
Gloucester, United Kingdom
The beginning of this year holiday season will belong to mocks. Ronnie and Stephen will take us for a tour around exciting world of unit testing.
forget that last post ...
I found it in my code ... non-application test code I threw in on the quick ... cause the StackOverflow.
Hey, did you ever find out what your problem was?
I got the same error on a completely different project.
Same M.O. ...
... unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll
during the first call to the object that was returned from the "Activator.GetObject()" call.
I encountered the following error message when running the ResumeClient.exe:
" An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll "
at the following line of code:
Resume resume = loader.GetResumeByUserID(1); ;
but I was able to observe the "New Reference Added!" string on the console of the ResumeSuperServer.
Could I safely assume that I already have "loader" as an object of "ResumeLoader", but I failed to obtain "resume" as an object of "Resume" ?
I was looking for a Remoting Sample for beginners for weeks and this was the first that made absolute sense to me. If you are a person with an average IQ like myself, and want to understand the basics of remoting implimentation, this is the tutorial for you!!
Thanks David for making learning Remoting easy and fun!!
I've been looking for a way to implement a cross application boundary component in C# .NET for most of this week. This looks like it will suit my needs perfectly. Thanks.
Thanks for pointing this out. I'll update the tutorial
change
ResumeLoader loader = (ResumeLoader)Activator.GetObject(
typeof(ResumeServer), "tcp://localhost:9932/ResumeLoader");
if(rs==null)
to
ResumeLoader loader = (ResumeLoader)Activator.GetObject(
typeof(ResumeLoader), "tcp://localhost:9932/ResumeLoader");
if(loader==null)
Note the typeof() change and the change of the test item.
Hope that is right (I would hate to have typos in my post fixing typos)
This thread is for discussions of Introducing .NET Remoting.