Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / September 2004

Tip: Looking for answers? Try searching our database.

.NET Remoting problem - exception "Object Reference not set to instance of an object"

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
aacool - 26 Sep 2004 00:41 GMT
Hi,

I need some help with a .NET Remoting problem - an "Object Reference not
set to instance of an object"exception is thrown in the client when
accessing methods of a remote object.

Basically, I have a server implemented as a Windows Service, using a
config file, and implementing an object from the General namespace. The
server code is below.

////Data definitions namespace
namespace General
{
    /// <summary>
    /// Interface to access data object
    /// </summary>
    public interface ICustomerManager
    {
        Customer getCustomer(int id);
        int getAge(int id);
    }
    /// <summary>
    /// Data object implementation
    /// </summary>
    [Serializable]
    public class Customer
    {
        //put all functions in an interface
        protected int cust_id;
        public string firstName,lastName;
        public DateTime BirthDate;
        public Customer(int id)
        {
            cust_id = id;
        }
    }
}

/////////Server code
namespace WindowsServiceSrvr
{
    public class ServiceSrvr : System.ServiceProcess.ServiceBase
    {
            private ComponentModel.Container components = null;
            public static string SVC_NAME = "ServiceSrvr";
            public static string cfgFile = "server.config";
            public ServiceSrvr(){...}

            static void Main()
            {
                System.ServiceProcess.ServiceBase[] ServicesToRun;
                ServicesToRun = new ServiceProcess.ServiceBase[] {    
                                                     new ServiceSrvr() };
                evt.Source = SVC_NAME;
                ServiceProcess.ServiceBase.Run(ServicesToRun);
            }
            protected override void OnStart(string[] args)
            {
                RemotingConfiguration.Configure(cfgFile);
            }
    }
       
    [Serializable]
    public class CustomerMgr : MarshalByRefObject,                          
                                       General.ICustomerManager
    {
        public CustomerMgr()
        {
        }

        public Customer getCustomer(int custId)
        {
            General.Customer cust = new Customer(custId);
            cust.BirthDate = new DateTime(1945,12,12);
            return cust;
        }
        public int getAge(int custId)
        {
            General.Customer cust = this.getCustomer(custId);
            TimeSpan tmp = DateTime.Today.Subtract(cust.BirthDate);
            return (int)tmp.Days/355;
        }
       
    }
}

My client uses a Generated_General.dll generated by soapsuds

soapsuds -url:http://localhost:custSrvr?wsdl -oa:gen.dll

The client code is:

namespace ClientUsingService
{
    /// <summary>
    /// Implementation of client - config file, soapsuds generated    
    ///     metaschema, and a server running as a windows service
    /// </summary>
    class Client
    {
        public static string cfgFile = "app.config";
        [STAThread]
        static void Main(string[] args)
        {
            try
            {
                RemotingConfiguration.Configure(cfgFile);
                // Instantion CustomerMgr interface
                WindowsServiceSrvr.CustomerMgr custMgr = new    
                                       WindowsServiceSrvr.CustomerMgr();
                if(custMgr!=null)
                    Console.WriteLine("Got server connection");
                General.Customer cust = custMgr.getCustomer(5434);
                Console.WriteLine(
                                            cust.BirthDate.ToShortDateString());    
                         // throws Invalid Reference Exception
                      Console.WriteLine("Customer 5434 is {0} yrs    
                                                     old",custMgr.getAge(5443));
                Console.ReadLine();
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
                Console.ReadLine();
            }
        }
    }
}

The problem is that when the methods of custMgr are called by the client
the exception

"Object Reference not set to an instance of an object" is thrown.

I am at my wits end!! Please help explain what is wrong.
Sooraj PM - 26 Sep 2004 11:45 GMT
Hi

There can be a reson of the mistake in the configuration files (either
client or server side .config files). Try to do the hosting and activation
through code. I mean create the host channel with port and host the object
and in the client side access the object through this ip address. You can get
examples from net.

This is a small example: (it is in VB.NET)

in OnStart Event of Windows Service (Host)
=============================
ChannelServices.RegisterChannel(New TcpChannel(12345)
RemotingConfiguration.RegisterActivatedServiceType(GetType(WindowsServiceSrvr.CustomerMgr))
RemotingConfiguration.ApplicationName = "CustomerMgr"

in the Client Application
===============

    Dim Attribs(0) As Object
    Dim CustMgr As WindowsServiceSrvr.CustomerMgr
    Dim CustomerMessage As String
    Attribs(0) = New UrlAttribute("tcp://localhost:12345/CustomerMgr")
'Note: The CustomerMgr is the name you have defined in the Host by giving
the line
'         RemotingConfiguration.ApplicationName = "CustomerMgr". Both should
be same
    CustMgr =
Activator.CreateInstance(GetType(WindowsServiceSrvr.CustomerMgr), Nothing,
Attribs)

Now you can call the methods of CustMgr Object. Hope this may help you. Once
it is working successfully analyze the contents of the config file. There can
be some issues with that:

Example Config Files:
Host:
<configuration>
<system.runtime.remoting>
     <application name = "CustomerMgr">
     <service>
           <activated type ="WindowsServiceSrvr.CustomerMgr,CustomerMgr"
objecturi = "CustomerMgr"/>
    </service>
    <channels>
          <channel ref = "Tcp" port ="12345"></channel>
          <formatter ref="binary">
    </formatter>
   </channels>
   </application>
</system.runtime.remoting>
</configuration>

in Client:

<configuration>
<system.runtime.remoting>
      <application name = "CustomerMgr">
      <client url = "tcp://localhost:12345">
           <activated type ="WindowsServiceSrvr.CustomerMgr,CustomerMgr"/>
      </client>
           <channels>
             <channel ref = "Tcp"/>
           </channels>
      </application>
</system.runtime.remoting>
</configuration>

Please compare the same with your config file. Hope this will help you.

Regards

Sooraj
Microsoft Community Star

> Hi,
>
[quoted text clipped - 132 lines]
>
> I am at my wits end!! Please help explain what is wrong.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.