UriFormatException: Invalid URI: The format of the URI could not be determined in nopcommerce

2 months ago
In live site (www.uguideapp.com) whenever I registered new customer at that time it shows error. While the same thing in local it not showing any type of error in nopcommerce 4.60.5

Here is the registered button click event

[HttpPost]
[ValidateCaptcha]
[ValidateHoneypot]
//available even when navigation is not allowed
[CheckAccessPublicStore(ignore: true)]
public virtual async Task<IActionResult> Register(RegisterModel model, string returnUrl, bool captchaValid, IFormCollection form)
{
    //check whether registration is allowed
    if (_customerSettings.UserRegistrationType == UserRegistrationType.Disabled)
        return RedirectToRoute("RegisterResult", new { resultId = (int)UserRegistrationType.Disabled, returnUrl });

    var customer = await _workContext.GetCurrentCustomerAsync();
    if (await _customerService.IsRegisteredAsync(customer))
    {
        //Already registered customer.
        await _authenticationService.SignOutAsync();

        //raise logged out event      
        await _eventPublisher.PublishAsync(new CustomerLoggedOutEvent(customer));

        customer = await _customerService.InsertGuestCustomerAsync();

        //Save a new record
        await _workContext.SetCurrentCustomerAsync(customer);
    }

    var store = await _storeContext.GetCurrentStoreAsync();
    customer.RegisteredInStoreId = store.Id;

    //custom customer attributes
    var customerAttributesXml = await ParseCustomCustomerAttributesAsync(form);
    var customerAttributeWarnings = await _customerAttributeParser.GetAttributeWarningsAsync(customerAttributesXml);
    foreach (var error in customerAttributeWarnings)
    {
        ModelState.AddModelError("", error);
    }

    //validate CAPTCHA
    if (_captchaSettings.Enabled && _captchaSettings.ShowOnRegistrationPage && !captchaValid)
    {
        ModelState.AddModelError("", await _localizationService.GetResourceAsync("Common.WrongCaptchaMessage"));
    }

    //GDPR
    if (_gdprSettings.GdprEnabled)
    {
        var consents = (await _gdprService
            .GetAllConsentsAsync()).Where(consent => consent.DisplayDuringRegistration && consent.IsRequired).ToList();

        ValidateRequiredConsents(consents, form);
    }

    if (ModelState.IsValid)
    {
        var customerUserName = model.Username?.Trim();
        var customerEmail = model.Email?.Trim();

        var isApproved = _customerSettings.UserRegistrationType == UserRegistrationType.Standard;
        var registrationRequest = new CustomerRegistrationRequest(customer,
            customerEmail,
            _customerSettings.UsernamesEnabled ? customerUserName : customerEmail,
            model.Password,
            _customerSettings.DefaultPasswordFormat,
            store.Id,
            isApproved);
        var registrationResult = await _customerRegistrationService.RegisterCustomerAsync(registrationRequest);
        if (registrationResult.Success)
        {
            //properties
            if (_dateTimeSettings.AllowCustomersToSetTimeZone)
                customer.TimeZoneId = model.TimeZoneId;

            //VAT number
            if (_taxSettings.EuVatEnabled)
            {
                customer.VatNumber = model.VatNumber;

                var (vatNumberStatus, _, vatAddress) = await _taxService.GetVatNumberStatusAsync(model.VatNumber);
                customer.VatNumberStatusId = (int)vatNumberStatus;
                //send VAT number admin notification
                if (!string.IsNullOrEmpty(model.VatNumber) && _taxSettings.EuVatEmailAdminWhenNewVatSubmitted)
                    await _workflowMessageService.SendNewVatSubmittedStoreOwnerNotificationAsync(customer, model.VatNumber, vatAddress, _localizationSettings.DefaultAdminLanguageId);
            }

            //form fields
            if (_customerSettings.GenderEnabled)
                customer.Gender = model.Gender;
            if (_customerSettings.FirstNameEnabled)
                customer.FirstName = model.FirstName;
            if (_customerSettings.LastNameEnabled)
                customer.LastName = model.LastName;
            if (_customerSettings.DateOfBirthEnabled)
                customer.DateOfBirth = model.ParseDateOfBirth();
            if (_customerSettings.CompanyEnabled)
                customer.Company = model.Company;
            if (_customerSettings.StreetAddressEnabled)
                customer.StreetAddress = model.StreetAddress;
            if (_customerSettings.StreetAddress2Enabled)
                customer.StreetAddress2 = model.StreetAddress2;
            if (_customerSettings.ZipPostalCodeEnabled)
                customer.ZipPostalCode = model.ZipPostalCode;
            if (_customerSettings.CityEnabled)
                customer.City = model.City;
            if (_customerSettings.CountyEnabled)
                customer.County = model.County;
            if (_customerSettings.CountryEnabled)
                customer.CountryId = model.CountryId;
            if (_customerSettings.CountryEnabled && _customerSettings.StateProvinceEnabled)
                customer.StateProvinceId = model.StateProvinceId;
            if (_customerSettings.PhoneEnabled)
                customer.Phone = model.Phone;
            if (_customerSettings.FaxEnabled)
                customer.Fax = model.Fax;

            //save customer attributes
            customer.CustomCustomerAttributesXML = customerAttributesXml;
            await _customerService.UpdateCustomerAsync(customer);

            //newsletter
            if (_customerSettings.NewsletterEnabled)
            {
                var isNewsletterActive = _customerSettings.UserRegistrationType != UserRegistrationType.EmailValidation;

                //save newsletter value
                var newsletter = await _newsLetterSubscriptionService.GetNewsLetterSubscriptionByEmailAndStoreIdAsync(customerEmail, store.Id);
                if (newsletter != null)
                {
                    if (model.Newsletter)
                    {
                        newsletter.Active = isNewsletterActive;
                        await _newsLetterSubscriptionService.UpdateNewsLetterSubscriptionAsync(newsletter);

                        //GDPR
                        if (_gdprSettings.GdprEnabled && _gdprSettings.LogNewsletterConsent)
                        {
                            await _gdprService.InsertLogAsync(customer, 0, GdprRequestType.ConsentAgree, await _localizationService.GetResourceAsync("Gdpr.Consent.Newsletter"));
                        }
                    }
                    //else
                    //{
                    //When registering, not checking the newsletter check box should not take an existing email address off of the subscription list.
                    //_newsLetterSubscriptionService.DeleteNewsLetterSubscription(newsletter);
                    //}
                }
                else
                {
                    if (model.Newsletter)
                    {
                        await _newsLetterSubscriptionService.InsertNewsLetterSubscriptionAsync(new NewsLetterSubscription
                        {
                            NewsLetterSubscriptionGuid = Guid.NewGuid(),
                            Email = customerEmail,
                            Active = isNewsletterActive,
                            StoreId = store.Id,
                            CreatedOnUtc = DateTime.UtcNow
                        });

                        //GDPR
                        if (_gdprSettings.GdprEnabled && _gdprSettings.LogNewsletterConsent)
                        {
                            await _gdprService.InsertLogAsync(customer, 0, GdprRequestType.ConsentAgree, await _localizationService.GetResourceAsync("Gdpr.Consent.Newsletter"));
                        }
                    }
                }
            }

            if (_customerSettings.AcceptPrivacyPolicyEnabled)
            {
                //privacy policy is required
                //GDPR
                if (_gdprSettings.GdprEnabled && _gdprSettings.LogPrivacyPolicyConsent)
                {
                    await _gdprService.InsertLogAsync(customer, 0, GdprRequestType.ConsentAgree, await _localizationService.GetResourceAsync("Gdpr.Consent.PrivacyPolicy"));
                }
            }

            //GDPR
            if (_gdprSettings.GdprEnabled)
            {
                var consents = (await _gdprService.GetAllConsentsAsync()).Where(consent => consent.DisplayDuringRegistration).ToList();
                foreach (var consent in consents)
                {
                    var controlId = $"consent{consent.Id}";
                    var cbConsent = form[controlId];
                    if (!StringValues.IsNullOrEmpty(cbConsent) && cbConsent.ToString().Equals("on"))
                    {
                        //agree
                        await _gdprService.InsertLogAsync(customer, consent.Id, GdprRequestType.ConsentAgree, consent.Message);
                    }
                    else
                    {
                        //disagree
                        await _gdprService.InsertLogAsync(customer, consent.Id, GdprRequestType.ConsentDisagree, consent.Message);
                    }
                }
            }

            //insert default address (if possible)
            var defaultAddress = new Address
            {
                FirstName = customer.FirstName,
                LastName = customer.LastName,
                Email = customer.Email,
                Company = customer.Company,
                CountryId = customer.CountryId > 0
                    ? (int?)customer.CountryId
                    : null,
                StateProvinceId = customer.StateProvinceId > 0
                    ? (int?)customer.StateProvinceId
                    : null,
                County = customer.County,
                City = customer.City,
                Address1 = customer.StreetAddress,
                Address2 = customer.StreetAddress2,
                ZipPostalCode = customer.ZipPostalCode,
                PhoneNumber = customer.Phone,
                FaxNumber = customer.Fax,
                CreatedOnUtc = customer.CreatedOnUtc
            };
            if (await _addressService.IsAddressValidAsync(defaultAddress))
            {
                //some validation
                if (defaultAddress.CountryId == 0)
                    defaultAddress.CountryId = null;
                if (defaultAddress.StateProvinceId == 0)
                    defaultAddress.StateProvinceId = null;
                //set default address
                //customer.Addresses.Add(defaultAddress);

                await _addressService.InsertAddressAsync(defaultAddress);

                await _customerService.InsertCustomerAddressAsync(customer, defaultAddress);

                customer.BillingAddressId = defaultAddress.Id;
                customer.ShippingAddressId = defaultAddress.Id;

                await _customerService.UpdateCustomerAsync(customer);
            }

            //notifications
            if (_customerSettings.NotifyNewCustomerRegistration)
                await _workflowMessageService.SendCustomerRegisteredStoreOwnerNotificationMessageAsync(customer,
                    _localizationSettings.DefaultAdminLanguageId);

            //raise event      
            await _eventPublisher.PublishAsync(new CustomerRegisteredEvent(customer));
            var currentLanguage = await _workContext.GetWorkingLanguageAsync();

            switch (_customerSettings.UserRegistrationType)
            {
                case UserRegistrationType.EmailValidation:
                    //email validation message
                    await _genericAttributeService.SaveAttributeAsync(customer, NopCustomerDefaults.AccountActivationTokenAttribute, Guid.NewGuid().ToString());
                    await _workflowMessageService.SendCustomerEmailValidationMessageAsync(customer, currentLanguage.Id);

                    //result
                    return RedirectToRoute("RegisterResult", new { resultId = (int)UserRegistrationType.EmailValidation, returnUrl });

                case UserRegistrationType.AdminApproval:
                    return RedirectToRoute("RegisterResult", new { resultId = (int)UserRegistrationType.AdminApproval, returnUrl });

                case UserRegistrationType.Standard:
                    //send customer welcome message
                    await _workflowMessageService.SendCustomerWelcomeMessageAsync(customer, currentLanguage.Id);

                    //raise event      
                    await _eventPublisher.PublishAsync(new CustomerActivatedEvent(customer));

                    returnUrl = Url.RouteUrl("RegisterResult", new { resultId = (int)UserRegistrationType.Standard, returnUrl });
                    return await _customerRegistrationService.SignInCustomerAsync(customer, returnUrl, true);

                default:
                    return RedirectToRoute("Homepage");
            }
        }

        //errors
        foreach (var error in registrationResult.Errors)
            ModelState.AddModelError("", error);
    }

    //If we got this far, something failed, redisplay form
    model = await _customerModelFactory.PrepareRegisterModelAsync(model, true, customerAttributesXml);

    return View(model);
}

while the error is coming from following line (get by debug mode)

await _workflowMessageService.SendCustomerWelcomeMessageAsync(customer, currentLanguage.Id);

Here is the SendCustomerWelcomeMessageAsync Function code

public virtual async Task<IList<int>> SendCustomerWelcomeMessageAsync(Customer customer, int languageId)
{
    if (customer == null)
        throw new ArgumentNullException(nameof(customer));

    var store = await _storeContext.GetCurrentStoreAsync();
    languageId = await EnsureLanguageIsActiveAsync(languageId, store.Id);

    var messageTemplates = await GetActiveMessageTemplatesAsync(MessageTemplateSystemNames.CustomerWelcomeMessage, store.Id);
    if (!messageTemplates.Any())
        return new List<int>();

    //tokens
    var commonTokens = new List<Token>();
    await _messageTokenProvider.AddCustomerTokensAsync(commonTokens, customer);

    return await messageTemplates.SelectAwait(async messageTemplate =>
    {
        //email account
        var emailAccount = await GetEmailAccountOfMessageTemplateAsync(messageTemplate, languageId);

        var tokens = new List<Token>(commonTokens);
        await _messageTokenProvider.AddStoreTokensAsync(tokens, store, emailAccount);

        //event notification
        await _eventPublisher.MessageTokensAddedAsync(messageTemplate, tokens);

        var toEmail = customer.Email;
        var toName = await _customerService.GetCustomerFullNameAsync(customer);

        return await SendNotificationAsync(messageTemplate, emailAccount, languageId, tokens, toEmail, toName);
    }).ToListAsync();
}

From this function code stopped from this line

await _messageTokenProvider.AddCustomerTokensAsync(commonTokens, customer);

Now, the surprised thing is that in local it working perfectly while in server why it coming issue.
2 months ago
Post the entire System Log error message here.
Be sure  you have URLs properly configured for all your stores. Include leading http:// (or https:// if using SSL) and trailing /.
E.g.
http://yourstore.com/
2 months ago
Full message from log

System.UriFormatException: Invalid URI: The format of the URI could not be determined.     at System.Uri.CreateThis(String uri, Boolean dontEscape, UriKind uriKind, UriCreationOptions& creationOptions)     at System.Uri..ctor(String uriString)     at Nop.Services.Messages.MessageTokenProvider.RouteUrlAsync(Int32 storeId, String routeName, Object routeValues)
2 months ago
From where URLs configured? I didn't change anything in coding
2 months ago
URLs are configure in Admin > Stores
2 months ago
Done Sir. Thank you.