public static DeviceMessage DeserializeEvents(string deviceId, JArray events, ServiceContext serviceContext, IServiceEventSource serviceEventSource)
        {
            DeviceMessage messageRet = null;
            bool          firstEvent = true;

            foreach (JObject evt in events)
            {
                String eventType = GetEventTypeFromEvent(evt);

                if (eventType.Equals(Names.EVENT_TYPE_DEFAULT))
                {
                    serviceEventSource.ServiceMessage(serviceContext, $"Event Registry - Could not find event configuration for event type [{eventType}] - Will parse the event as Default Type");
                }

                EventConfiguration eventConfiguration = EventRegistry.GetEventConfiguration(eventType);

                DeviceEvent deviceEvent = DeserializeEvent(eventConfiguration, evt);

                if (firstEvent)
                {
                    messageRet = new DeviceMessage(eventConfiguration.MessageType, deviceId, deviceEvent);
                    firstEvent = false;
                }
                else
                {
                    messageRet.AddEvent(deviceEvent);
                }
            }

            return(messageRet);
        }
        public Task <string> OpenAsync(CancellationToken cancellationToken)
        {
            _eventSource.ServiceMessage(_serviceContext, "Calling OpenAsync on endpoint {0}", _endpointName);

            var serviceEndpoint = _serviceContext.CodePackageActivationContext.GetEndpoint(_endpointName);

            _eventSource.ServiceMessage(_serviceContext, "Found endpoint with protocol '{0}' port '{1}'", serviceEndpoint.Protocol, serviceEndpoint.Port);

            var statefulServiceContext = _serviceContext as StatefulServiceContext;

            if (statefulServiceContext != null)
            {
                _listeningAddress = string.Format(
                    CultureInfo.InvariantCulture,
                    "{0}://+:{1}/{2}{3}/{4}/{5}",
                    serviceEndpoint.Protocol,
                    serviceEndpoint.Port,
                    string.IsNullOrWhiteSpace(_appRoot)
                        ? string.Empty
                        : _appRoot.TrimEnd('/') + '/',
                    statefulServiceContext.PartitionId,
                    statefulServiceContext.ReplicaId,
                    Guid.NewGuid());
            }
            else if (_serviceContext is StatelessServiceContext)
            {
                _listeningAddress = string.Format(
                    CultureInfo.InvariantCulture,
                    "{0}://+:{1}/{2}",
                    serviceEndpoint.Protocol,
                    serviceEndpoint.Port,
                    string.IsNullOrWhiteSpace(_appRoot)
                        ? string.Empty
                        : _appRoot.TrimEnd('/') + '/');
            }
            else
            {
                throw new InvalidOperationException();
            }

            _publishAddress = _listeningAddress.Replace("+", FabricRuntime.GetNodeContext().IPAddressOrFQDN);

            try
            {
                _eventSource.ServiceMessage(_serviceContext, "Starting web server on " + _listeningAddress);
                _webApp = WebApp.Start(_listeningAddress, appBuilder => _startup.Invoke(appBuilder));
                _eventSource.ServiceMessage(_serviceContext, "Listening on " + _publishAddress);

                return(Task.FromResult(_publishAddress));
            }
            catch (Exception ex)
            {
                _eventSource.ServiceMessage(_serviceContext, "Web server for endpoint {0} failed to open. ", _endpointName, ex.ToString());

                StopWebServer();

                throw;
            }
        }
        public static DeviceMessage DeserializeEvents(string deviceId, string arrayEventStr, ServiceContext serviceContext, IServiceEventSource serviceEventSource)
        {
            DeviceMessage deviceMessageRet = null;

            try
            {
                JArray jarr = JArray.Parse(arrayEventStr);

                deviceMessageRet = DeserializeEvents(deviceId, jarr, serviceContext, serviceEventSource);
            }
            catch (Exception ex)
            {
                serviceEventSource.ServiceMessage(serviceContext, $"Event Registry - Could not parse device id[{deviceId}] message [{arrayEventStr}] Error Message [{ex.Message}]");
            }

            return(deviceMessageRet);
        }