在Windows服务中托管远程对象 [英] Hosting remoting objects in Windows service
本文介绍了在Windows服务中托管远程对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有可能在不使用wcf的情况下在Windows服务中托管远程处理对象.如有可能,可以举个例子.
Is there a possibility of hosting remoting objects in windows service without the use of wcf. If possible can any giv an example.
推荐答案
是的,有可能.尝试阅读有关构建基本.NET远程处理的文章. 构建基本的.NET远程应用程序
样例项目
1.客户(控制台项目).
一个. Client.config
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
<configuration>
<system.runtime.remoting>
<application>
<client>
<wellknown
type="HelloWorld, Host"
url="http://localhost:8989/HelloWorld.rem"
/>
</client>
</application>
</system.runtime.remoting>
</configuration>
b.这段代码.
b. this piece of code.
RemotingConfiguration.Configure("Client.config");
IHelloWorld objRemote = Activator.GetObject(typeof(IHelloWorld), "http://localhost:8989/HelloWorld.rem") as IHelloWorld;
Console.WriteLine(objRemote.GetString("Print : "));
2.合同项目(类库项目)
一个. IHelloWorld界面
2. Contract project(Class Library project)
a. IHelloWorld interface
public interface IHelloWorld
{
string GetString(string message);
}
3.主机(Windows服务)
一个. HelloWorld类
3. Host(Windows service)
a. HelloWorld class
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
b. Host.config
<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类
c. Service1 class
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");
}
}
这篇关于在Windows服务中托管远程对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文