C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




Remoting in ASP.NET


Posted Date: 07 Jul 2008    Resource Type: Code Snippets    Category: ASP.NET WebForms
Author: Kumar VeluMember Level: Diamond    
Rating: Points: 12



Remoting in Asp.net



The following are the general steps to be followed to achieve remoting in ASP.NET :

1. Create interface for all the classes you have to use (create separate class library project for all the interfaces).

2. Create class file and inherit with MarshalByRefObject and their corresponding Interfaces too (create separate class library for the business object) . Eg. public class User : MarshalByRefObject, IMyObj

3. Compile both projects (above two), you will have to give reference to the interfaces .dll in the class library project.

4. Put class library .dll with the interface .dll on the remote server bin folder.

5. In the client application, reference interface .dll so that the application will run successfully or remote objects behavior is identified in the application. This will ease in coding.

In place of above code (instantiation with new keyword in the first way) use the following code.

Note: In this way there is no need of a client.config file.


String str = string.Empty;
IMyObj thisO = (IMyObj) Activator.GetObject(typeof(IMyObj), "http://172.21.14.21/ACD_BusinessLogic/RemotingObjects.soap");
str = thisO.returnSomething();
Response.Write(str);


So total class code, interface code and the application code will be like this

Class code (myObj.cs)


using System;
using System.Runtime.Remoting;

namespace ACD
{
public class myObj : MarshalByRefObject, IMyObj
{
public string returnSomething()
{
return "

First Namespace with Interface

Checked on 05th October 2006" +
"
server name: " + System.Environment.MachineName + "

";

}
}
}



Interface code(IMyObj.cs)


using System;

///
/// Summary description for Class1
///

public interface IMyObj
{
string returnSomething();
}


Client side application code


IMyObj thisO = (IMyObj) Activator.GetObject(typeof(IMyObj), "http://172.21.14.21/ACD_BusinessLogic/myObj.soap");
str = thisO.returnSomething();
thisO = null;


(here ACD_BusinessLogic is the virtual directory on the remote machine. myObj.SOAP is the objectUri value of the config file. returnSomething is the method that return string value.)

Issues at the time of development :

If you are not sure that the application you are running is getting executed on the remote server try to put the following method and call them in your application, it will give the name of the computer where the object is being executed.


public string getCompName()
{
return System.Environment.MachineName;
}


If sometimes, the object or assembly is being executed from your local machine try restarting the IIS of your local machine (it is possible only when you are opting for the first way of doing Remoting). Also confirm the application by stopping the IIS of the remote server and running the application, if the application is giving error (something like connection is closed…. ) then its confirmed that the object is being created on the remote machine and you have successfully setup Remoting. Now start the remote server’s IIS again and enjoys REMOTING.

Sometimes the following error occurs

System.NullReferenceException: Object reference not set to an instance of an object. at
System.Runtime.Remoting.MetadataServices.RealSchemaType.Resolve(StringBuilder sb) at

To overcome this error keep class and interface in different namespace.

Config file when more than one assembly need to be used from the remote machine

Sample configuration files:

For Server web.config file


<system.runtime.remoting>
<application>

<channels>
<channel ref="http" >
<serverProviders>
<provider ref="wsdl" />
<formatter ref="soap" typeFilterLevel="Full" />
<formatter ref="binary" typeFilterLevel="Full" />
</serverProviders>
<clientProviders>
<formatter ref="binary" />
</clientProviders>
</channel>
</channels>



<service>
<wellknown
mode="SingleCall" objectUri="CustomerMaster.rem"
type="SupportLib.CustomerMaster,SupportLib"/>
</service>

<service>
<wellknown
mode="SingleCall" objectUri="BuyerMaster.rem"
type="SupportLib.BuyerMaster,SupportLib"/>
</service>

</application>
</system.runtime.remoting>


For client config file


<system.runtime.remoting>
<application>
<channels>
<channel ref="http">
<clientProviders>
<formatter ref="binary" />
</clientProviders>
</channel>
</channels>

<client>
<wellknown
url="http://bigg:80/dashboardserver/BuyerMaster.rem"
type="SupportLib.BuyerMaster,SupportLib"
/>
</client>

<client>
<wellknown
url="http://bigg:80/dashboardserver/CustomerMaster.rem"
type="SupportLib.CustomerMaster,SupportLib"
/>
</client >
</application>
</system.runtime.remoting>





Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Remoting in Asp.net  .  Introduction to Remoting in ASP.NET  .  Introduction to Remoting in .NET  .  ASP.NET Remoting  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: dynamic control at runtime with maintaining viewstate
Previous Resource: Create Menus in ASP.NET web pages
Return to Discussion Resource Index
Post New Resource
Category: ASP.NET WebForms


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design


Contact Us    Privacy Policy    Terms Of Use