简体   繁体   English

在WCF服务与其托管Windows服务之间传递数据

[英]Passing data between a wcf service and its hosting windows service

I have a project that wants me to communicate with a server. 我有一个项目,希望我与服务器通信。 I need to send data processed from a standalone application on client side to the server. 我需要将从客户端上的独立应用程序处理的数据发送到服务器。 For this I am using a wcf service that interacts with the server. 为此,我使用了与服务器交互的wcf服务。 This service is hosted in a windows service. 该服务托管在Windows服务中。 Now my problem begins. 现在我的问题开始了。 I need to monitor a folder, write/read some files and delete them. 我需要监视一个文件夹,写入/读取一些文件并将其删除。 For this I am using the same windows service as the one hosting the wcf service. 为此,我使用与托管wcf服务相同的Windows服务。 How can I pass data between the two services? 如何在两个服务之间传递数据? For example I would like to read a file using the windows service and pass the data to wcf service which then passes it to the server and back. 例如,我想使用Windows服务读取文件并将数据传递给wcf服务,然后将其传递给服务器并返回。

If you just want to be able to communicate between two services hosted in a windows service, one solution I have used is to store a static session state in the windows service itself. 如果只希望能够在Windows服务中托管的两个服务之间进行通信,则我使用的一种解决方案是在Windows服务本身中存储静态会话状态。 In Program.cs, I declare a static field which stores the session state, and I modify the constructors of my two services to take a reference to this object: 在Program.cs中,我声明了一个存储会话状态的静态字段,并且修改了两个服务的构造函数以对该对象进行引用:

static class Program
{
    private static SessionState sessionState = new SessionState() { SessionID = "100" };

    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    static void Main()
    {
        ServiceBase[] servicesToRun;
        servicesToRun = new ServiceBase[] 
            {
                new Service1(sessionState),
                new Service2(sessionState)
            };

        ServiceBase.Run(servicesToRun);
    }
}

I have a class called SessionState which I use to store any data which I want to transfer between my hosted services. 我有一个称为SessionState的类,该类用于存储要在托管服务之间传输的任何数据。 In this instance I am just giving the session an ID property: 在这种情况下,我只是给会话一个ID属性:

public class SessionState
{
    public string SessionID { get; set; }
}

Service1 and Service2 store a reference to the static session state, and in my example I just modify the sessionID in one of the two threads: Service1和Service2存储对静态会话状态的引用,在我的示例中,我只是在两个线程之一中修改了sessionID:

public partial class Service1 : ServiceBase
{
    private SessionState sessionState;

    public Service1(SessionState sessionState)
    {
        this.sessionState = sessionState;
        InitializeComponent();
    }

    protected override void OnStart(string[] args)
    {
        Console.WriteLine("Service 1 started.");
        Task tsk = new Task(() => this.DoStuff());
        tsk.Start();
    }

    protected override void OnStop()
    {
    }

    private void DoStuff()
    {
        Console.WriteLine("Session state for service 1 is " + this.sessionState.SessionID);

        Thread.Sleep(2000);

        Console.WriteLine("Session state for service 1 is " + this.sessionState.SessionID);
    }
}

public partial class Service2 : ServiceBase
{
    private SessionState sessionState;

    public Service2(SessionState sessionState)
    {
        this.sessionState = sessionState;

        InitializeComponent();
    }

    protected override void OnStart(string[] args)
    {
        Console.WriteLine("Service 2 started.");
        Task tsk = new Task(() => this.DoStuff());
        tsk.Start();
    }

    protected override void OnStop()
    {
    }

    private void DoStuff()
    {
        Console.WriteLine("Session state for service 2 is " + this.sessionState.SessionID);

        Thread.Sleep(1000);
        this.sessionState.SessionID = "200";

        Console.WriteLine("Session state for service 2 is " + this.sessionState.SessionID);
    }
}

Now when I run the Windows Service (with a Console window attached) I get the following: 现在,当我运行Windows服务(附加了控制台窗口)时,将得到以下信息: Windows服务从控制台窗口运行

Hope this helps! 希望这可以帮助!

Alex 亚历克斯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM