首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WCF服务到Windows服务连接性的Signalr实现

WCF服务到Windows服务连接性的Signalr实现
EN

Stack Overflow用户
提问于 2018-02-09 19:06:48
回答 1查看 175关注 0票数 1

我在我的机器上创建了windows服务,创建并托管了另一个WCF服务。我想知道如何使用Signalr通信WCF服务和Windows服务。问题是,我不知道如何将WCF和windows服务结合在一起,从而使连接更加紧密。

另一方面,如果我的WCF服务有一些数据,我想立即将该数据传递给windows服务。

下面是我在windows服务中的代码片段。

命名空间POCWindowsService {公共部分类TestService : ServiceBase { System.Timers.Timer timeDelay;int计数;

代码语言:javascript
复制
    public TestService()
    {
        InitializeComponent();
        timeDelay = new System.Timers.Timer();
        timeDelay.Elapsed += new System.Timers.ElapsedEventHandler(WorkProcess);

    }

    public void WorkProcess(object sender, System.Timers.ElapsedEventArgs e)
    {
        string process = "Timer Tick" + count;
        LogService(process);
    }

    protected override void OnStart(string[] args)
    {
        LogService("POC Service Started");
        timeDelay.Enabled = true;

    }

    private void LogService(string content)
    {
        FileStream fs = new FileStream(@"F:\Test\TestServiceLog.txt", FileMode.OpenOrCreate);
        var sw = new StreamWriter(fs);
        sw.BaseStream.Seek(0, SeekOrigin.End);
        sw.WriteLine(content);
        sw.Flush();
        sw.Close();
    }

    protected override void OnStop()
    {
        LogService("POC Service Stopped");
        timeDelay.Enabled = false;

    }
}

}

我的WCF服务运行在http://localhost:8080/上,我的windows服务名为POCWindowsService。

我想知道如何使用Signalr连接这两个服务。

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-02-10 08:47:39

我不认为它与WCF相关的问题。您所需要的只是将信号R库添加到您的服务中,然后调用它。也许是这样的:

代码语言:javascript
复制
private async void ConnectAsync()
{
    Connection = new HubConnection("http://localhost:8080");
    HubProxy = Connection.CreateHubProxy("YourHub");

    //Handle incoming event from server: use Invoke to write to console from SignalR's thread
    HubProxy.On<string, string>("YourMethod", (params1, params2) => Invoke((Action)(() => LogService(String.Format("{0}: {1}" + Environment.NewLine, params1, params2)))));

    await Connection.Start();
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48712519

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档