C#
  Home arrow C# arrow Page 2 - An Introduction To .NET Remoting
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Moblin 
JMSL Numerical Library 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C#

An Introduction To .NET Remoting
By: David Talbot
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 8
    2002-08-28

    Table of Contents:
  • An Introduction To .NET Remoting
  • Creating The Shared Library
  • The Server Object
  • The Remote Client
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    An Introduction To .NET Remoting - Creating The Shared Library


    (Page 2 of 5 )

    Fire up Visual Studio.NET. 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 shown 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);
    }
    }


    If you're getting errors that the System.Runtime.Remoting.Channels.Tcp namespace does not exist, make sure that you’ve added a reference to the System.Runtime.Remoting.dll by clicking on the Project->Add Reference menu.

    using System;
    using System.Runtime;
    using System.Data.SqlClient;


    The namespace we're using for this particular object is DotNetRemoteTest. The object shown below is a MarshalByRefObject, which means that instead of passing ResumeLoader over the wire, we're creating a reference 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";
    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 referenced .NET Remote object. The reason for this is the object will have to be turned into raw data to be passed over the network and then re-assembled into an object again at 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

    More C# Articles
    More By David Talbot


     

    C# ARTICLES

    - Introduction to Objects and Classes in C#, P...
    - Visual C#.NET, Part 1: Introduction to Progr...
    - C# - An Introduction
    - Hotmail Exposed: Access Hotmail using C#
    - Razor Sharp C#
    - Introduction to Objects and Classes in C#
    - Making Your Code CLS Compliant
    - Programming with MySQL and .NET Technologies
    - Socket Programming in C# - Part II
    - Socket Programming in C# - Part I
    - Creational Patterns in C#
    - Type Conversions
    - Creating Custom Delegates and Events in C#
    - Inheritance and Polymorphism
    - Understanding Properties in C#







    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway