为什么我的 ASP.NET Web 应用程序没有向我的 Azure Table 存储添加新用户?

Why does my ASP.NET web application not add a new user to my Azure Table storage?

我在 Visual Studio 2015 中创建了一个新的网络应用程序要求新用户注册一个新帐户。用户通过 Windows 表单完成他们的详细信息,然后我有一个 Azure 云服务(包括一个具有 WCF 网络服务的网络角色),它被消耗并旨在更新远程 Azure Table 存储。

我遇到的问题是,当用户提交表单时,HTTP 响应代码(使用 Fiddler Web 调试器检索)是 200 成功,但没有任何结果 created/added 到 Azure Table 存储帐户。我不是 C# 开发人员,网络服务/Azure 对我来说都是新手,所以很可能我的 code/configuration 有缺陷。

RegisterUserService.svc.cs:

using System.ServiceModel;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Table;
using Microsoft.WindowsAzure;
using System;
using System.Diagnostics;

namespace WCFServiceWebRoleRegisterUser
{
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
    public class RegisterUserService : IRegisterService
    {
        public void AddNewUser(UserEntity user)
        {
            try
            {
            // Retrieve the storage account from the connection string.
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
                CloudConfigurationManager.GetSetting
                 ("registerCloudService40083714"));

            // Create the table client.
            CloudTableClient tableClient = storageAccount.CreateCloudTableClient();

            // Create the table if it doesn't exist.
            CloudTable table = tableClient.GetTableReference("users");
            table.CreateIfNotExists();

            // Create a new user entity.
            UserEntity newUserEntry = new UserEntity(user.Surname, user.Email);
            newUserEntry.Forename = user.Forename;
            newUserEntry.Password = user.Password;

            // Create the TableOperation object that inserts the user entity.
            TableOperation insertOperation = TableOperation.Insert(newUserEntry);

            // Execute the insert operation.
            table.Execute(insertOperation);

            if (table.Execute(insertOperation).HttpStatusCode == 403 ||
                table.Execute(insertOperation).HttpStatusCode == 409)
            {
                Debug.WriteLine("Account already registered with that Email Address");
            }
            else if (table.Execute(insertOperation) != null &&
                table.Execute(insertOperation).HttpStatusCode == 200)
            {
                Debug.WriteLine("Account successfully registered");
            }
        }
        catch (Exception)
        {
            throw;
        }
      }
    }
}

Register.aspx.cs:(这是消费WCF云服务的代码)

using _40083714webapp.RegisterUserServiceReference;
using System;
using System.Web.UI;

namespace _40083714webapp
{
    public partial class Home : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.UnobtrusiveValidationMode = UnobtrusiveValidationMode.None;
        }

        protected void registerButton_Click(object sender, EventArgs e)
        {
            RegisterServiceClient client = new RegisterServiceClient();
            client.Open();
            UserEntity user = new UserEntity(surnameTextBox.ToString(), 
              emailTextBox.ToString());
            user.Password = passwordTextBox.ToString();
            user.Forename = forenameTextBox.ToString();
            // client.AddNewUser(user);
            client.AddNewUserAsync(user);
            client.Close();
        }
    }
}

IRegisterService.cs:

using System.ServiceModel;

namespace WCFServiceWebRoleRegisterUser
{
    [ServiceContract]
    public interface IRegisterService
    {
        [OperationContract]
        void AddNewUser(UserEntity user);
    }
}

UserEntity.cs:

using Microsoft.WindowsAzure.Storage.Table;

namespace WCFServiceWebRoleRegisterUser
{
    public class UserEntity : TableEntity
    {
        public UserEntity(string surname, string email)
        {
            this.PartitionKey = surname;
            this.RowKey = email;
        }

        public UserEntity() { }

        public string Forename { get; set; }

        public string Surname { get; set; }

        public string Password { get; set; }

        public string Email { get; set; }
    }
}

我在您的代码中注意到了一些事情。请看看他们:

  1. 假设 client.AddNewUserAsync(user); 是一个 async 操作,您可能需要等待。所以你的代码将是:

    等待client.AddNewUserAsync(用户);

  2. 因此,您需要制作 registerButton_Click 方法 async

  3. 有一件事我不明白,为什么您要在 AddNewUser 代码中多次执行插入操作。理想情况下,您应该只执行一次此操作并捕获存储异常并在那里进行所有错误处理。

        // Execute the insert operation.
        table.Execute(insertOperation);//Wrap this in its own try/catch block and catch and handle StorageException type Exception.
        //This piece of code is not really needed.
        /*
        if (table.Execute(insertOperation).HttpStatusCode == 403 ||
            table.Execute(insertOperation).HttpStatusCode == 409)
        {
            Debug.WriteLine("Account already registered with that Email Address");
        }
        else if (table.Execute(insertOperation) != null &&
            table.Execute(insertOperation).HttpStatusCode == 200)
        {
            Debug.WriteLine("Account successfully registered");
        }
        */