public Registration Create(Registration device)
        {
            try
            {
                if (ApiUrl == string.Empty)
                {
                    throw new Exception(Errors.ERR_DEVICEM_MISSING_APIURL);
                }

                var uriBuilder = new UriBuilder(ApiUrl + "/registrations");

                if (DevKey != string.Empty)
                {
                    uriBuilder.Query = "subscription-key=" + DevKey;
                }

                var payload = ModelManager.ModelToJson<Registration>(device);

                var json = Rest.Post(uriBuilder.Uri, payload);

                device = ModelManager.JsonToModel<Registration>(json);
            }
            catch (Exception err)
            {
                var errString = string.Format(Errors.ERR_DEVICEM_MODEL_NOT_CREATED, device.model) + ", " + err.Message;
                if (err.InnerException != null)
                    errString += ", " + err.InnerException.Message;
                throw new Exception(errString);
            }

            return device;
        }
        private async static Task AddDeviceAsync(Registration deviceRegistration)
        {
            //if (deviceRegistration.key != string.Empty) return;

            // create a unique id for this device
            var deviceId = deviceRegistration.model + "-" + deviceRegistration.id;

            // this class represents the device registered with IoT Hub
            Device device;

            // register or lookup the device
            try
            {
                device = await _registryManager.AddDeviceAsync(new Device(deviceId));
                device.Authentication.SymmetricKey;
            }
            catch (DeviceAlreadyExistsException)
            {
                device = await _registryManager.GetDeviceAsync(deviceId);
            }

            try
            {
                // update the application registry
                deviceRegistration.key = device.Authentication.SymmetricKey.PrimaryKey;
                _registryM.Update(deviceRegistration);
                Console.WriteLine("{0} Generated device {1} key: {2}", _count++, deviceRegistration.model, device.Authentication.SymmetricKey.PrimaryKey);
                Thread.Sleep(3000);
            }
            catch (Exception err)
            {
                Console.WriteLine("{0} {1}",  _count++, err.Message);
            }
        }
        public Registration Update(Registration device)
        {
            try
            {
                if (device.isValid())
                    this._persist.Update(device);
                else
                {
                    throw new Exception(Errors.ERR_DEVICEM_MODEL_NOT_VALID);
                }
            }
            catch (Exception err)
            {
                throw new Exception(string.Format(Errors.ERR_DEVICEM_MODEL_NOT_UPDATED, device.model), err);
            }

            return device;
        }
        public Registration Create(Registration device)
        {
            try
            {
                if (device.isValid())
                    this._persist.Insert(device);
                else
                {
                    throw new Exception(Errors.ERR_DEVICEM_MODEL_NOT_VALID);
                }
            }
            catch (Exception err)
            {
                throw new Exception(Errors.ERR_DEVICEM_BAGREQUEST, err);
            }

            return device;
        }
        static void Main(string[] args)
        {
            var count = 0;

            var configM = new ConfigM { ApiUrl = ConfigurationManager.AppSettings["ConfigM"] };

            var profileManifest = configM.GetByName("ProfileM");
            var refManifest = configM.GetByName("RefM");

            var profileM = new ProfileM {ApiUrl = profileManifest.lineitems[LineitemsKey.PublicAPI]};
            var participants = profileM.GetAllByType(UserType.Participant);

            var refM = new RefM {ApiUrl = refManifest.lineitems[LineitemsKey.PublicAPI]};
            var products = refM.GetAllByDomain("Products");
            var models = refM.GetAllByDomain("Models");

            // create  a device registration for each particpant
            foreach (var p in participants)
            {
                var r = new Registration();

                foreach (var m in models)
                {
                    if (m.code == "BIOMAX-HOME")
                    {
                        r.key = string.Empty;
                        r.productline = products[0].code;
                        r.model = m.code;
                        r.version = m.attributes["version"];
                        r.firmwareversion = m.attributes["firmware"];
                    }
                }

                r.participantid = p.id;

                var json = ModelManager.ModelToJson<Registration>(r);
                var filename = AppDomain.CurrentDomain.BaseDirectory + @"data\deviceregistration" + count + ".json";
                System.IO.File.WriteAllText(filename, json);

                Console.WriteLine("Device Registration " + count + " generated");
                count++;
            }
        }