(Not Found) Error in Azure Mobile Services .NET Backend

假装没事ソ 提交于 2019-12-23 06:07:51

问题


Been stuck with that error till madness phases ... Please help

I have created an Azure Mobile Service .NET backend, and am now trying to call its Post function from a Xamarin Android client

I initialize and call the Insert async function (these are just snippets from my code)

private static IMobileServiceTable<Todo> _todoMobileServiceTable;

public static bool? InitializeAms()
{
    try
    {
        CurrentPlatform.Init();
        _mobileServiceClient = new MobileServiceClient(applicationUrl, applicationKey);

        _todoMobileServiceTable = _mobileServiceClient.GetTable<Todo>();

        return true;
    }
    catch (MalformedURLException malformedUrlException)
    {
        ReportHelper.Report(Tag, "There was an error creating the Mobile Service. Verify the URL", true, malformedUrlException);
    }
    catch (Exception exception)
    {
        ReportHelper.Report(Tag, "Error occurred during initialization of Azure Mobile Services", true, exception);
    }

    return null;
}

_todoMobileServiceTable.InsertAsync(Todo);

I get the following error when calling .InsertAsync(Todo)

The request could not be completed. (Not Found)

N.B:

  • Azure storage client is not available for xamarin yet, and I have no other choice other than to use this dirty fork which is 1 year old and is made for iOS not Android (although it works fine with azure mobile service javascript) https://github.com/zgramana/IOSAzureBlobUploader

  • It works if I use the browser 'try it out' button but it doesn't work when I call it from the xamarin client app.

  • It works from the xamarin client app if I use the javascript mobile service

  • This error occurs both on the local azure mobile service and the published one online

Here is the WebApiConfig class

namespace Service.Ams
{
    public static class WebApiConfig
    {
        public static void Register()
        {
            // Use this class to set configuration options for your mobile service
            ConfigOptions options = new ConfigOptions();

            // Use this class to set WebAPI configuration options
            HttpConfiguration config = ServiceConfig.Initialize(new ConfigBuilder(options));

            // To display errors in the browser during development, uncomment the following
            // line. Comment it out again when you deploy your service for production use.
            config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;

            Database.SetInitializer(new ServiceAmsInitializer());
        }
    }

    public class ServiceAmsInitializer : ClearDatabaseSchemaIfModelChanges<ServiceAmsDbContext>
    {}
}

Here is the TableController class

namespace Service.Ams.Controllers
{
    public class TodoItemController : TableController<TodoItem>
    {
        protected override void Initialize(HttpControllerContext controllerContext)
        {
            base.Initialize(controllerContext);
            ServiceAmsDbContext serviceAmsDbContext = new ServiceAmsDbContext();
            DomainManager = new EntityDomainManager<TodoItem>(serviceAmsDbContext, Request, Services);
        }

        // GET tables/TodoItem
        [AuthorizeLevel(AuthorizationLevel.Admin)]
        public IQueryable<TodoItem> GetAllTodoItems()
        {
            return Query(); 
        }

        // GET tables/TodoItem/55D11C86-6EA6-4C44-AA33-337FC9A27525
        [AuthorizeLevel(AuthorizationLevel.Admin)]
        public SingleResult<TodoItem> GetTodoItem(string id)
        {
            return Lookup(id);
        }

        // PATCH tables/TodoItem/55D11C86-6EA6-4C44-AA33-337FC9A27525
        [AuthorizeLevel(AuthorizationLevel.Admin)]
        public Task<TodoItem> PatchTodoItem(string id, Delta<TodoItem> patch)
        {
             return UpdateAsync(id, patch);
        }

        // POST tables/TodoItem/55D11C86-6EA6-4C44-AA33-337FC9A27525
        [AuthorizeLevel(AuthorizationLevel.Anonymous)]
        public async Task<IHttpActionResult> PostTodoItem(TodoItem item)
        {
            string storageAccountName;
            string storageAccountKey;

            // Try to get the Azure storage account token from app settings.  
            if (
                !(Services.Settings.TryGetValue("STORAGE_ACCOUNT_NAME", out storageAccountName) |
                  Services.Settings.TryGetValue("STORAGE_ACCOUNT_ACCESS_KEY", out storageAccountKey)))
                Services.Log.Error("Could not retrieve storage account settings.");

            // Set the URI for the Blob Storage service.
            Uri blobEndpoint = new Uri(string.Format("http://127.0.0.1:10000/{0}/", storageAccountName));

            // Create the BLOB service client.
            CloudBlobClient blobClient = new CloudBlobClient(blobEndpoint, new StorageCredentials(storageAccountName, storageAccountKey));

            // Create a container, if it doesn't already exist.
            CloudBlobContainer container = blobClient.GetContainerReference(item.ContainerName);
            await container.CreateIfNotExistsAsync();

            // Create a shared access permission policy. 
            BlobContainerPermissions containerPermissions = new BlobContainerPermissions
            {
                PublicAccess = BlobContainerPublicAccessType.Blob
            };

            // Enable anonymous read access to BLOBs.
            container.SetPermissions(containerPermissions);

            // Define a policy that gives write access to the container for 5 minutes.                                   
            SharedAccessBlobPolicy sasPolicy = new SharedAccessBlobPolicy
            {
                SharedAccessStartTime = DateTime.UtcNow,
                SharedAccessExpiryTime = DateTime.UtcNow.AddMinutes(5),
                Permissions = SharedAccessBlobPermissions.Write
            };

            // Get the SAS as a string.
            item.SasQueryString = container.GetSharedAccessSignature(sasPolicy);

            // Set the URL used to store the image.
            item.ImageLqUri = string.Format("{0}{1}/{2}", blobEndpoint, item.ContainerName, item.ResourceNameLq);
            item.ImageHqUri = string.Format("{0}{1}/{2}", blobEndpoint, item.ContainerName, item.ResourceNameHq);

            // Complete the insert operation.
            TodoItem current = await InsertAsync(item);
            return CreatedAtRoute("Tables", new {id = current.Id}, current);
        }

        // DELETE tables/TodoItem/55D11C86-6EA6-4C44-AA33-337FC9A27525
        [AuthorizeLevel(AuthorizationLevel.Admin)]
        public Task DeleteTodoItem(string id)
        {
             return DeleteAsync(id);
        }

    }
}

Here is the EntityData class

namespace Service.Ams.DataObjects
{
    [Table("dbo.TodoItems")]
    public class TodoItem : EntityData
    {
        public string ContainerName { get; set; }
        public string ResourceNameLq { get; set; }
        public string ResourceNameHq { get; set; }
        public string SasQueryString { get; set; }
        public string ImageLqUri { get; set; }
        public string ImageHqUri { get; set; }
    }
}

回答1:


Is there any way you can get a dump of what the HTTP request looks like?

I don't have an android client handy here but we can have a look on Monday.

Henrik




回答2:


TableController and client corresponding class must have the same name for example TodoController and TodoClass. I don't know if there is an attribute that modifies this rule and how to use, if at server side decorating TableController class or at client side decorating data class.



来源:https://stackoverflow.com/questions/24354139/not-found-error-in-azure-mobile-services-net-backend

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!