Frage

I modified the Broadcast Sample from SignalR website documentation, instead of random values, I read random xml files. I'm using the HttpContext.Current.Server.MapPath method to access the files and avoid hardcoding the physical path. This runs fine in Windows 7 IIS 7.5 and within Visual Studio 2013, but in Windows 8.1 and Windows Server 2012 I found out that HttpContext.Current is null. Before asking I searched other questions but didn't found anything like what's happening here.

Here is the whole class code:

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Threading;

using Microsoft.AspNet.SignalR;
using Microsoft.AspNet.SignalR.Hubs;

namespace SignalRDisplayXML
{
    public struct ReturnDataScheme
    {
        public List<VsDisplaySlotBean> slotsdata;
        public string filename;
    }

    public class XmlTicker
    {
        private readonly static Lazy<XmlTicker> _instance =
            new Lazy<XmlTicker>(() => new XmlTicker(GlobalHost.ConnectionManager.GetHubContext<DisplayXMLHub>().Clients, HttpContext.Current.Server.MapPath("~/Data/")));

        private DAO _dao = new DAO();
        private ReturnDataScheme _server_data = new ReturnDataScheme();

        private string _directoryPath = string.Empty;

        private readonly object _processRandomXMLLock = new object();
        private volatile bool _updatingRandomXML = false;
        private readonly Timer _timer;

        private XmlTicker(IHubConnectionContext clients, string path)
        {
            Clients = clients;
            _directoryPath = path;

            _server_data.filename = GetRandomFileName();
            _server_data.slotsdata = _dao.GetVesselAll(_server_data.filename);

            _timer = new Timer(ProcessRandomXML, null, 10000, 10000);
        }

        public static XmlTicker Instance
        {
            get
            {
                return _instance.Value;
            }
        }

        private IHubConnectionContext Clients
        {
            get;
            set;
        }

        public ReturnDataScheme GetCurrentData()
        {
            return _server_data;
        }

        private void ProcessRandomXML(object state)
        {
            lock (_processRandomXMLLock)
            {
                if (!_updatingRandomXML)
                {
                    _updatingRandomXML = true;

                    _server_data.filename = GetRandomFileName();
                    _server_data.slotsdata = _dao.GetVesselAll(_server_data.filename);

                    Clients.All.DataPush(_server_data);

                    _updatingRandomXML = false;
                }
            }
        }

        private string GetRandomFileName()
        {
            Random randomizer = new Random(DateTime.Now.Millisecond);
            return _directoryPath + "DisplaySlotFDMF00" + randomizer.Next(1, 9).ToString() + ".xml";
        }
    }
}

Thanks.

War es hilfreich?

Lösung

You can use HostingEnvironment.MapPath instead of HttpContext.MapPath.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top