Home > .NET Framework > Exposing a component thru Remoting and Web Services simultaneously

Exposing a component thru Remoting and Web Services simultaneously

Interesting topic, My google search was not pointing to anything useful.
Hence I thought I’ll blog my way of doing it.
 
Server side:
Web Services usually inherit from the WebService class. This gives them direct access to HttpContext properties. But this inheritance chain is not mandated, it is optional. So no one stops your web service class (asmx codebehind) to inherit from the MarshalByRefObject. If you need access to the HttpContext properties then you still have them via HttpContext.Current.
 
Now you can also host this class from a Windows Service for Remoting.
 
Client Side:
Use a factory to get the appropriate proxy.
 
Code (Optional reading):
 
DTO:
using System;
namespace Sendhil.Samples.Dual.DTO
{
 [Serializable()]
 public class Person
 {
  public Person()
  {
   _firstName = "Sendhil Kumar";
   _lastName = "Ramalingam";
  }
  private string _firstName;
  public string FirstName
  {
   get { return _firstName; }
   set { _firstName = value; }
  }
  private string _lastName;
  public string LastName
  {
   get { return _lastName; }
   set { _lastName = value; }
  }
 }
}
 
Interface
using System;
using Sendhil.Samples.Dual.DTO;
namespace Sendhil.Samples.Dual.Interfaces
{
 public interface IHelloService
 {
  string HelloWorld(Person person);
 }
}
 
Service Class (for remoting as well as web service codebehind)
using System;
using System.Web;
using System.Web.Services;
using Sendhil.Samples.Dual.Interfaces;
using Sendhil.Samples.Dual.DTO;
namespace Sendhil.Samples.Dual.Service
{
 [WebService(Description="Hello World Remoting and Web Service Implementation (Dual)",Namespace="http://spaces.msn.com/sendhil/sendhil.samples.dual/")]
 public class HelloService : MarshalByRefObject, IHelloService
 {
  public HelloService()
  {   
  }
  [WebMethod()]
  public string HelloWorld(Person person)
  {
   return string.Format("Hello {1}, {0}", person.LastName, person.FirstName);
  }
 }
}
 
Client Side ProxyFactory
using System;
using System.Runtime.Remoting;
using Sendhil.Samples.Dual.Interfaces;
namespace Sendhil.Samples.Dual.HelloClient
{
 public class ProxyFactory
 {
  public ProxyFactory()
  {
  }
  private static WellKnownClientTypeEntry entry;
  static ProxyFactory()
  {
            entry = RemotingConfiguration.GetRegisteredWellKnownClientTypes()[0];
  }
  public static IHelloService Create(bool useWebService)
  {
   IHelloService service ;
   if(useWebService)
   {
    service = new HelloService();
    return service;
   }
   else
   {
    service  = (IHelloService)Activator.GetObject(typeof(IHelloService), entry.ObjectUrl);
    return service;
   }
  }
 }
}
 
Advertisements
Categories: .NET Framework
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: