加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – WCF:如何从服务库实例中访问服务实例?

发布时间:2020-12-15 22:18:42 所属栏目:百科 来源:网络整理
导读:我有一个在 Windows服务中托管的长期运行的WCF服务.我有一个服务库,其目的是报告服务的状态.如何从服务库的实例中获取服务实例? 为了说明,我创建了一个记录其开始时间的服务,并公开了一种方法来报告它运行的时间.服务库需要能够调用服务的ElapsedSeconds()
我有一个在 Windows服务中托管的长期运行的WCF服务.我有一个服务库,其目的是报告服务的状态.如何从服务库的实例中获取服务实例?

为了说明,我创建了一个记录其开始时间的服务,并公开了一种方法来报告它运行的时间.服务库需要能够调用服务的ElapsedSeconds()方法,因此库需要对正在运行的服务的引用.

我以为我可以使用OperationContext.Current.这是我的服务库类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;

namespace TimerService
{
public class TimerServiceLib : ITimerServiceLib
{
    TheTimerService m_timerService;

    public TimerServiceLib()
    {
        var currentContext = OperationContext.Current;
        var instanceContext = currentContext.InstanceContext;
        m_timerService = (TheTimerService)instanceContext.GetServiceInstance();
    }

    public double SecondsSinceStart()
    {
        return m_timerService.ElapsedSeconds();
    }
}
}

但是对GetServiceInstance()的调用会创建一个TimerServiceLib()的新实例,这当然会给我一个无限循环.那么,这样做的正确方法是什么?

这是我的服务类,实际上是在控制台应用程序中托管:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;

namespace TimerService
{
public partial class TheTimerService : ServiceBase
{
    private DateTime m_startTime;
    ServiceHost m_svcHost;

    public TheTimerService()
    {
        InitializeComponent();
        Init();
        m_startTime = DateTime.Now;
    }

    public double ElapsedSeconds()
    {
        return (DateTime.Now - m_startTime).TotalSeconds;
    }

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

    protected override void OnStop()
    {
    }

    public void Init()
    {
        if (m_svcHost != null)
        {
            m_svcHost.Close();
        }

        string httpAddress = "http://localhost:1234/TimerService";
        string tcpAddress = "net.tcp://localhost:1235/TimerService";

        Uri[] adrbase = { new Uri(httpAddress),new Uri(tcpAddress) };
        m_svcHost = new ServiceHost(typeof(TimerServiceLib),adrbase);

        ServiceMetadataBehavior mBehave = new ServiceMetadataBehavior();
        m_svcHost.Description.Behaviors.Add(mBehave);

        var debugBehavior = m_svcHost.Description.Behaviors.Find<ServiceDebugBehavior>();
        debugBehavior.IncludeExceptionDetailInFaults = true;

        BasicHttpBinding httpBinding = new BasicHttpBinding();
        m_svcHost.AddServiceEndpoint(typeof(ITimerServiceLib),httpBinding,httpAddress);
        m_svcHost.AddServiceEndpoint(typeof(IMetadataExchange),MetadataExchangeBindings.CreateMexHttpBinding(),"mex");
        NetTcpBinding tcpBinding = new NetTcpBinding();
        m_svcHost.AddServiceEndpoint(typeof(ITimerServiceLib),tcpBinding,tcpAddress);
        m_svcHost.AddServiceEndpoint(typeof(IMetadataExchange),MetadataExchangeBindings.CreateMexTcpBinding(),"mex");

        m_svcHost.Open();

        // SimShopServiceLib.m_shop = new CSimShopManager();
    }

}

}

解决方法

你可以这样做:

首先,修改TimerServiceLib并执行TheTimerService的构造函数注入:

public class TimerServiceLib : ITimerServiceLib
{
    private readonly TheTimerService m_timerService;

    public TimerServiceLib(TheTimerService theTimerService)
    {
         m_timerService = theTimerService;
    }

    public double SecondsSinceStart()
    {
        return m_timerService.ElapsedSeconds();
    }
}

然后,在创建ServiceHost时的Init()中,首先实例化您的服务并传递TheTimerService.由于您正在Windows服务中创建ServiceHost,因此您可以通过TheTimerService传递此信息.

Uri[] adrbase = { new Uri(httpAddress),new Uri(tcpAddress) };
var timerServiceLib = new TimerServiceLib(this)
m_svcHost = new ServiceHost(timerServiceLib,adrbase);

有关在您的服务中传递对象的更多详细信息,请参阅此link.

免责声明:以上代码未经过测试.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读