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

时间:2016-03-25 15:13:43

标签: c# wcf azure azure-storage azure-table-storage

我在 Visual Studio 2015 中创建了一个新的网络应用程序,首先要求新用户注册新帐户。用户通过Windows窗体完成其详细信息,然后我有一个 Azure 云服务(包括一个具有WCF Web服务的Web角色),该服务被使用并旨在更新远程 Azure < / em>表存储。

我遇到的问题是,当用户提交表单时,HTTP响应代码(使用 Fiddler Web调试器检索)是成功的200但是没有创建/添加到 Azure 表存储帐户。我不是C#开发人员,而web-services / Azure 对我来说都是新手,所以我的代码/配置极有可能存在缺陷。

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 Cloud服务的代码)

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 个答案:

答案 0 :(得分:0)

我在代码中注意到了一些事情。请看一下它们:

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

    await client.AddNewUserAsync(user);

  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");
        }
        */