【问题标题】:Azure Mobile Services Endpoint not found (.NET backend)未找到 Azure 移动服务终结点(.NET 后端)
【发布时间】:2014-08-04 01:04:21
【问题描述】:

我正在构建一个简单的费用跟踪系统。当我在本地运行代码时,代码运行良好,但是当我尝试连接到 Azure 中的端点时,出现 404 错误。我什至尝试完全删除移动服务并重新发布它,但它只适用于我的本地计算机。

spoofy@spoofers:~$ curl -v https://mxpense.azure-mobile.net/tables/Expense
* About to connect() to mxpense.azure-mobile.net port 443 (#0)
*   Trying 191.236.80.12... connected
* successfully set certificate verify locations:
*   CAfile: none
  CApath: /etc/ssl/certs
* SSLv3, TLS handshake, Client hello (1):
* SSLv3, TLS handshake, Server hello (2):
* SSLv3, TLS handshake, CERT (11):
* SSLv3, TLS handshake, Server finished (14):
* SSLv3, TLS handshake, Client key exchange (16):
* SSLv3, TLS change cipher, Client hello (1):
* SSLv3, TLS handshake, Finished (20):
* SSLv3, TLS change cipher, Client hello (1):
* SSLv3, TLS handshake, Finished (20):
* SSL connection using RC4-SHA
* Server certificate:
*        subject: C=US; ST=WA; L=Redmond; O=Microsoft; OU=OrganizationName; CN=*.azurewebsites.net
*        start date: 2014-03-24 21:01:22 GMT
*        expire date: 2016-03-23 21:01:22 GMT
*        subjectAltName: mxpense.azure-mobile.net matched
*        issuer: DC=com; DC=microsoft; DC=corp; DC=redmond; CN=MSIT Machine Auth CA 2
*        SSL certificate verify ok.
> GET /tables/Expense HTTP/1.1
> User-Agent: curl/7.22.0 (x86_64-pc-linux-gnu) libcurl/7.22.0 OpenSSL/1.0.1 zlib/1.2.3.4 libidn/1.23 librtmp/2.3
> Host: mxpense.azure-mobile.net
> Accept: */*
>
< HTTP/1.1 404 Not Found
< Content-Length: 0
< Server: Microsoft-IIS/8.0
< X-Powered-By: ASP.NET
< Set-Cookie: ARRAffinity=11de5874c445a15c401ea654cfeb62ad8ed363d5c21bc48e2b33658554d05d9b;Path=/;Domain=mxpense.azure-mobile.net
< Date: Fri, 01 Aug 2014 00:12:03 GMT
<
* Connection #0 to host mxpense.azure-mobile.net left intact
* Closing connection #0
* SSLv3, TLS alert, Client hello (1):

我用一些在 Azure SQL DB 中看到的示例数据为数据库播种,所以我知道数据在那里

当然还有我的种子方法

    List<Expense> expenses = new List<Expense>{ new Expense { Id = System.Guid.NewGuid().ToString(), Amount = 7.77, Client = "Cisco", Location = "Houston,tx", Date = DateTime.UtcNow},
    new Expense {Id = System.Guid.NewGuid().ToString(),Amount = 10.99, Client= "Microsoft", Location = "Remond, WA", Date = DateTime.UtcNow}};

    foreach (Expense exp in expenses)
    {
        context.Set<Expense>().Add(exp);
    }
    base.Seed(context);

对象的类定义

    public class Expense: EntityData
    {
        public double Amount { get; set; }

        public ExpenseCurrency? Currency { get; set; }

        public DateTime? Date { get; set; }

        public string Vender { get; set; }

        public ExpenseType? Type { get; set; }

        public string Location { get; set; }

        public string Client { get; set; }

        public string Project { get; set; }

        public bool? PersonalExpense { get; set; }

        public string UserId { get; set; }

        public int? CompanyId { get; set; }
}

更新

Azure 移动服务 SDK 似乎请求了错误的路径。它不是请求 http://{service name}.azure-mobile.net/api/Expenses 而是请求 https://{service name}.azure-mobile.net/tables/Expenses

这里是初始化

private MobileServiceCollection<Expense, Expense> expenseCollection;
private IMobileServiceTable<Expense> expenseTable = App.MobileService.GetTable<Expense>();

这里是抛出异常的地方

expenseCollection = await expenseTable.ToCollectionAsync();

更新 2

请求了费用控制器类..

public class ExpensesController : ApiController
{
    private mXpenseContext db = new mXpenseContext();

    // GET: api/Expenses
    public IQueryable<Expense> GetExpenses()
    {
        return db.Expenses;
    }

    // GET: api/Expenses/5
    [ResponseType(typeof(Expense))]
    public async Task<IHttpActionResult> GetExpense(string id)
    {
        Expense expense = await db.Expenses.FindAsync(id);
        if (expense == null)
        {
            return NotFound();
        }

        return Ok(expense);
    }

    // PUT: api/Expenses/5
    [ResponseType(typeof(void))]
    public async Task<IHttpActionResult> PutExpense(string id, Expense expense)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        if (id != expense.Id)
        {
            return BadRequest();
        }

        db.Entry(expense).State = EntityState.Modified;

        try
        {
            await db.SaveChangesAsync();
        }
        catch (DbUpdateConcurrencyException)
        {
            if (!ExpenseExists(id))
            {
                return NotFound();
            }
            else
            {
                throw;
            }
        }

        return StatusCode(HttpStatusCode.NoContent);
    }

    // POST: api/Expenses
    [ResponseType(typeof(Expense))]
    public async Task<IHttpActionResult> PostExpense(Expense expense)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        db.Expenses.Add(expense);

        try
        {
            await db.SaveChangesAsync();
        }
        catch (DbUpdateException)
        {
            if (ExpenseExists(expense.Id))
            {
                return Conflict();
            }
            else
            {
                throw;
            }
        }

        return CreatedAtRoute("DefaultApi", new { id = expense.Id }, expense);
    }

    // DELETE: api/Expenses/5
    [ResponseType(typeof(Expense))]
    public async Task<IHttpActionResult> DeleteExpense(string id)
    {
        Expense expense = await db.Expenses.FindAsync(id);
        if (expense == null)
        {
            return NotFound();
        }

        db.Expenses.Remove(expense);
        await db.SaveChangesAsync();

        return Ok(expense);
    }

    protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            db.Dispose();
        }
        base.Dispose(disposing);
    }

    private bool ExpenseExists(string id)
    {
        return db.Expenses.Count(e => e.Id == id) > 0;
    }
}

【问题讨论】:

  • 请查看图片。
  • 你能发布控制器类吗?可能是 ExpenseController.cs

标签: c# entity-framework azure


【解决方案1】:

默认情况下,移动服务 (.Net) 中的控制器从 TableController 继承,如您的错误消息所示,它正在寻找到 /tables/expenses 的路由。由于您的控制器继承自 ApiController(根据 webApi 完全可以),您应该能够添加路由属性以获得所需的结果。

// GET: api/Expenses
[Route("api")]
public IQueryable<Expense> GetExpenses()
{
    return db.Expenses;
}

// GET: api/Expenses/5
[Route("api/{id}"]
[ResponseType(typeof(Expense))]
public async Task<IHttpActionResult> GetExpense(string id)
{
    Expense expense = await db.Expenses.FindAsync(id);
    if (expense == null)
    {
        return NotFound();
    }

    return Ok(expense);
}

【讨论】:

  • 更新了费用控制器,我仍然遇到同样的问题。我怀疑问题出在我的客户端配置上,而不是控制器类。连接到该服务的 Windows Phone 客户端请求的路径错误。
  • 听起来确实可能是问题所在。我通常使用 Fiddler (telerik.com/fiddler) 测试服务并在 GET 方法上添加断点,然后您可以确定服务是否按预期执行。告诉我你的进展情况
【解决方案2】:

似乎问题出在我的控制器上。它是从 ApiController 继承的,但将其切换到 TableController 解决了这个问题。搭建脚手架时,请确保使用“windows mobile services table controller”选项

【讨论】:

    【解决方案3】:

    您的控制器应显示在帮助页面下,您可以在本地和托管时看到该页面。如果托管,则使用您的主密钥作为密码(用户名无关紧要)。

    TableController 连接在 /tables/* 路径下,而所有其他控制器连接在 /api/* 对于 TableControllers,您可以使用客户端表 API,对于所有其他控制器,您可以使用自定义 API。

    谢谢,

    亨里克

    【讨论】:

      猜你喜欢
      • 2014-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-04
      • 2018-12-21
      相关资源
      最近更新 更多