C# Method or Object cannot be called second time

143 Views Asked by At

I am a total noob in C# and tried to add an automated mail services to my backend API for an Angular FrontEnd. It works properly as intended for one time, but cannot be used a second time. I guess I am violating some object rules, maybe someone is able to point out my mistake.

This is an excerpt of my file UserAuthController.cs which includes the register function. When registration on my website is successful it shall also call the API Service from my automated mail system. I didn't know how to include the function properly so I've added it with a new namespace.

namespace Gogo_Api.Controllers
{
    [RoutePrefix("UserAuth")]
    [EnableCors(origins: "*", headers: "*", methods: "*")]
    public class UserAuthController : ApiController
    {
        private readonly IUserAuthManager _userAuthManager;

        public UserAuthController()
        {

        }
        public UserAuthController(IUserAuthManager userAuthManager)
        {
            this._userAuthManager = userAuthManager;
        }

        [HttpPost]
        [Route("Register")]
        public HttpResponseMessage Register(UserDetails user)
        {
            var response = new RegisterResponse();
            response.code = _userAuthManager.Register(user);
            if (response.code == 1)
            {
                response.message = "Registration successful ";

                //Including API Service here...
                Sendinblue.Program RegisterMail = new Sendinblue.Program();
                RegisterMail.Main(user.email, user.displayName, user.country);
                RegisterMail = null;
               
            }
            else if (response.code == 2)
            {
                response.message = "User already registered ";
            }
            else if (response.code == 0)
            {
                response.message = "Error occured";
            }
            return Request.CreateResponse(HttpStatusCode.OK, response);
        }

    }
}



namespace Sendinblue
{
    class Program
    {
        public void Main(string userMail, string userName, string userCountry)
        {
            Configuration.Default.ApiKey.Add("api-key", "MYKEY");

            var apiInstance = new ContactsApi();
            string email = userMail;
            JObject attributes = new JObject();
            attributes.Add("USERNAME", userName);
            attributes.Add("COUNTRY", userCountry);
            List<long?> listIds = new List<long?>();
            listIds.Add(5);
            try
            {
                var createContact = new CreateContact(email, attributes, emailBlacklisted, smsBlacklisted, listIds, updateEnabled, smtpBlacklistSender);
                CreateUpdateContactModel result = apiInstance.CreateContact(createContact);
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);
                Console.WriteLine(e.Message);
            }
        }


    }
}

I have added 'RegisterMail = null;' because I thought I need to delete my object first before using it again, but still it works only for the first time. How would I be able to call my function multiple times?

Thanks

1

There are 1 best solutions below

0
On

Thanks @Richard Barker, your comment helped me fix it. I have moved everything out of the Controller and had to move

Configuration.Default.ApiKey.Add...
apiInstance = new ContactsApi();

to a one-time call in the Initializer, so the API Call and ContactsApi is only created once, instead of everytime.