c# - 发布到表存储时出现 404

标签 c# azure

我尝试在不使用 TableContext 和 Table ServiceEntity 类的情况下写入 Azure 表存储,我只想发送原始 XML(我希望能够灵活地动态定义架构)。

当我执行此操作(并使用 SDK 中的方法签署请求)时,我收到 404。不知道为什么。

该表存在。我省略了我的凭据,但它们在我的原始代码中是正确的。

下面的代码片段说明了问题。

非常欢迎任何建议!

using System.IO;
using System.Net;
using System.Text;
using Microsoft.WindowsAzure.StorageClient.Protocol;

public static class Program
{
    public static void Main()
    {
        Credentials credentials = new Credentials("xxx", @"yyy");
        var uri = string.Format(@"http://{0}.table.core.windows.net/{1}(PartitionKey='{2}',RowKey='{3}')", credentials.AccountName, "TableName", "1", "1");
        var webRequest = (HttpWebRequest)WebRequest.Create(uri);
        webRequest.Method = "POST";
        webRequest.Headers.Add("DataServiceVersion", "2.0;NetFx");
        webRequest.Headers.Add("MaxDataServiceVersion", "2.0;NetFx");
        webRequest.ContentType = @"application/atom+xml";
        WriteToRequestStream(uri, webRequest);
        TableRequest.SignRequestForSharedKeyLite(webRequest, credentials);
        var response = webRequest.GetResponse(); // 404 thrown here
    }

    private static void WriteToRequestStream(string uri, HttpWebRequest webRequest)
    {
        var sb = new StringBuilder();
        sb.Append(@"<?xml version='1.0' encoding='utf-8' standalone='yes'?><entry xmlns:d='http://schemas.microsoft.com/ado/2007/08/dataservices' xmlns:m='http://schemas.microsoft.com/ado/2007/08/dataservices/metadata' xmlns='http://www.w3.org/2005/Atom'><title /><updated>2009-03-18T11:48:34.9840639-07:00</updated><author><name /></author><id>");
        sb.Append(uri);
        sb.Append(@"</id><content type='application/xml'><m:properties></m:properties></content></entry>");
        string body = sb.ToString();
        webRequest.ContentLength = body.Length;
        UTF8Encoding encoding = new UTF8Encoding();
        byte[] bytes = encoding.GetBytes(body);
        using (Stream requestStream = webRequest.GetRequestStream())
        {
            requestStream.Write(bytes, 0, bytes.Length);
        }
    }
}

最佳答案

创建实体时,您有三个选择:

  1. Insert您可以在其中发布到 http://myaccount.table.core.windows.net/mytable
  2. Insert or Merge合并到 http://myaccount.table.core.windows.net/mytable(PartitionKey="myPartitionKey", RowKey="myRowKey1")
  3. Insert or Replace其中您 PUT 到 http://myaccount.table.core.windows.net/mytable(PartitionKey="myPartitionKey", RowKey="myRowKey1")

但是,本地模拟器不支持后两者。

此外,我查看了您的代码使用 Fiddler 发出的请求,它缺少以下 header :

  • 授权
  • 日期或 x-ms-date
  • x-ms-版本

要正确包含 header ,请尝试以下代码:

var webRequest = (HttpWebRequest)WebRequest.Create(uri);
webRequest.Method = "PUT";
webRequest.Headers.Add("DataServiceVersion", "2.0;NetFx");
webRequest.Headers.Add("MaxDataServiceVersion", "2.0;NetFx");
webRequest.Headers.Add("x-ms-version", "2011-08-18");
webRequest.ContentType = @"application/atom+xml";
TableRequest.SignRequestForSharedKeyLite(webRequest, credentials);
WriteToRequestStream(uri, webRequest);
var response = webRequest.GetResponse();

请注意我如何更改执行插入或替换的方法。我还反转了对 SignRequestForSharedKeyLite 的调用,并添加了版本 2011-08-18x-ms-version header ,否则插入或替换不是支持。

关于c# - 发布到表存储时出现 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8987635/

相关文章:

c# - 将 webapi 添加到 mvc 应用程序 - 路由

c# - 如何学习/理解 ASPX 或 ASCX 页面

c# - 有什么方法可以通过提供节点数来访问 TreeView 节点

c# - 应用程序见解 - 没有 'process cpu' 的数据

azure - Windows Azure,blob 未加载到 LocalResource

c# - 哪段代码性能更高?

c# - 应用快捷方式

azure - 应该 +(加号)符号用于 Azure Functions 2 中的路由

azure - 无法连接到已部署的 Azure 云服务

Azure 服务总线 - JAVA 中的消息计数