Click here to Skip to main content
15,884,041 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Is there a possibility of hosting remoting objects in windows service without the use of wcf. If possible can any giv an example.
Posted

1 solution

Yes it is possible. Try to read this article on building a basic .NET remoting. Building a Basic .NET Remoting Application

Sample Project
1. Client(Console project).
a. Client.config
HTML
<configuration>
  <system.runtime.remoting>
    <application>
      <client>
        <wellknown
            type="HelloWorld, Host"
           url="http://localhost:8989/HelloWorld.rem"
            />
      </client>
    </application>
  </system.runtime.remoting>
</configuration>

b. this piece of code.
C#
RemotingConfiguration.Configure("Client.config");
IHelloWorld objRemote = Activator.GetObject(typeof(IHelloWorld), "http://localhost:8989/HelloWorld.rem") as IHelloWorld;
Console.WriteLine(objRemote.GetString("Print : "));

2. Contract project(Class Library project)
a. IHelloWorld interface

C#
public interface IHelloWorld
    {
        string GetString(string message);
    }


3. Host(Windows service)
a. HelloWorld class
C#
public class HelloWorld : MarshalByRefObject, IHelloWorld
    {
        public string GetString(string message)
        {
            return string.Concat(message, " Hello world");
        }

        public string GetDate()
        {
            return DateTime.Now.ToString();
        }
    }

b. Host.config
HTML
<configuration>
  <system.runtime.remoting>
    <application>
      <service>
        <wellknown
           mode="Singleton"
           type="Host.HelloWorld, Host"
           objectUri="HelloWorld.rem"
            />
      </service>
      <channels>
        <channel ref="http" port="8989"/>
      </channels>
    </application>
  </system.runtime.remoting>
</configuration>

c. Service1 class
C#
public partial class Service1 : ServiceBase
    {
        public Service1()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            HostObject();
        }

        protected override void OnStop()
        {
        }

        internal void HostObject()
        {
            RemotingConfiguration.Configure("Host.config");
        }
    }
 
Share this answer
 
Comments
Member 8570559 15-Feb-12 3:42am    
its throwing error as unable to connect to remote server...
can u give the proper code....please
Michael dg 15-Feb-12 8:27am    
make sure they are running on the same machine. the host should be running first. and then the client.
Member 8570559 15-Feb-12 22:57pm    
Yaw but i'am not able to start the service. It's showing that "Some services stop automatically if they have no work to do". What should i do now???

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900