Reference Cosmos DB nuget package in Azure Functions - c#

I am trying to use Cosmos DB Document client in my Azure Function App. I have followed the steps mentioned here- How can I use NuGet packages in my Azure Functions?
I have the dependecy in project.json-
{
"frameworks": {
"net46":{
"dependencies": {
"Microsoft.Azure.DocumentDB.Core": "2.1.3"
}
}
}
}
The project.json is placed inside the function app and the app service editor path is like this-
https://<functionappname>.scm.azurewebsites.net/dev/wwwroot/<functionname>/project.json
This is my function code-
#r "Newtonsoft.Json"
#r "Microsoft.Azure.Documents.Client"
using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
using System.Net.Http;
using System.Threading.Tasks;
using System.Net.Http.Formatting;
using System.Threading;
using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Client;
public static async Task<IActionResult> Run(HttpRequest req, ILogger log)
{
string endPoint = <ep>;
string databaseId = <dbID>;
string collectionId = <cid>;
string documentId = <did>;
string resourceLink = string.Format("dbs/{0}/colls/{1}/docs/{2}", databaseId, collectionId, documentId);
string primaryKey = <pk>;
IDocumentClient documentClient = new DocumentClient(new Uri(endPoint), primaryKey);
var response = documentClient.ReadDocumentAsync(resourceLink).Result;
return new OkObjectResult(response);
}
When I Save and Run the app, I don't get any error or response. If I remove the CosmosDB reference codes it works. Below is the code-
#r "Newtonsoft.Json"
using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
using System.Net.Http;
using System.Threading.Tasks;
using System.Net.Http.Formatting;
using System.Threading;
public static async Task<IActionResult> Run(HttpRequest req, ILogger log)
{
return new OkObjectResult("response");
}
Am I missing something or doing this incorrectly? The same code works if I use it on a Console application. Any help with this please?

To work in the Portal it's easier if you add a Function with the Cosmos DB Trigger, that will pull the Cosmos DB Extension into the Function App, and then you can delete the created Function and create a new one and pull the DocumentClient:
Once the Extension is added, you can pull the client adding #r "Microsoft.Azure.DocumentDB.Core" on the top of your Function's code:
#r "Newtonsoft.Json"
#r "Microsoft.Azure.DocumentDB.Core"
using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Client;
private static DocumentClient client = new DocumentClient(new Uri("yourendpoint"), "yourkey");
public static async Task<IActionResult> Run(HttpRequest req, ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
string name = req.Query["name"];
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
dynamic data = JsonConvert.DeserializeObject(requestBody);
name = name ?? data?.name;
return name != null
? (ActionResult)new OkObjectResult($"Hello, {name}")
: new BadRequestObjectResult("Please pass a name on the query string or in the request body");
}

Function 2.0 uses function.proj as below.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Azure.DocumentDB.Core" Version="2.1.3"/>
</ItemGroup>
</Project>
And remove extra #r "Microsoft.Azure.Documents.Client" as the assembly has been added.

Related

REST Server connection reset on Routing path

Question is: Did I define the client resource call correctly, or is there something wrong in the server code?
I have a REST API server I am coding in C# / Visual Studio 2019 using the Web API template. I have 2 paths at the moment - a POST and a GET.
POST: /api/account
GET: /api/account/{accountid:long}
POST works great using SoapUI as a test client, but GET gives me a connection reset (message is "Error getting response; java.net.SocketException: Connection reset").
I hope I defined the resource correctly:
Here's my Controller code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Newtonsoft.Json.Linq;
using Coda.Core;
using CodaRESTServer.Models;
using System.IO;
using System.Diagnostics;
namespace MyRESTServer.Controllers
{
[RoutePrefix("api/account")]
public class AccountController : ApiController
{
[HttpGet]
[Route("{accountid:long}")]
// GET api/<controller>/5
public JObject Get(long accountid)
{
var x = new JObject();
x["worked"] = "true";
return (x);
}
[HttpPost]
[Route("")]
// POST api/account
public JObject Post()
{
var x = new JObject();
x["worked"] = "true";
return (x);
}
}
}
I specified it wrong in SoapUI. It needs to be:
/api/account/{accountid}
And then I can click on the Parameters field and enter the value.

How to return multiple Service Bus messages from Azure Function?

Is there a way to output multiple Message (ServiceBusMessage in version 5.0) instances from an Azure Function? The documentation says that one can either return a single message via out Message or multiple entities via ICollector<T>/IAsyncCollector<T>.
I guess the T is a payload type, so using ICollector<Message> would not allow me to configure SessionId and other sending options. Instead, it would simply treat the Message as a payload within an actual message.
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
namespace FunctionApp5
{
public static class Function1
{
[FunctionName("Function1")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
[ServiceBus("queuename1", Connection = "ServiceBusConnectionString1")] IAsyncCollector<dynamic> outputServiceBus1,
[ServiceBus("queuename2", Connection = "ServiceBusConnectionString2")] IAsyncCollector<dynamic> outputServiceBus2,
ILogger log)
{
await outputServiceBus1.AddAsync("xxx");
await outputServiceBus2.AddAsync("xxx");
return new OkObjectResult("This is a test.");
}
}
}
As #user1672994 pointed out, both ICollector<T> and IAsyncCollector<T> accept ServiceBusMessage as the T. Here is an example from Azure WebJobs Service Bus extension.

Microsoft Computer Vision API returning 404 Resource Not Found

I tried to call Azure's Computer Vision API using the C# code below but got the following response:
{"code":"404","message":"Resource not found"}
Any advice to get this work?
using System;
using System.IO;
using System.Threading.Tasks;
using AzureFunctions.Extensions.CognitiveServices.Bindings.Vision.Analysis;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Extensions.Logging;
using Microsoft.WindowsAzure.Storage.Table;
namespace myCognitiveFunction
{
public static class myCognitiveFunction
{
[FunctionName("myCognitiveFunction")]
public static async Task RunAsync(
[BlobTrigger("images/{name}", Connection = "storageAccount")]Stream myBlob,
[VisionAnalysis(VisionKey = "Key", VisionUrl = "Url")]VisionAnalysisClient visionClient,
[Table("VisionAnalysis", Connection = "storageAccount")]IAsyncCollector<VisionResult> results,
string name, ILogger log)
{
var request = new VisionAnalysisRequest(myBlob);
var result = await visionClient.AnalyzeAsync(request);
var visionResult = new VisionResult(Guid.NewGuid().ToString(), "VisionAnalysis") { ResultJson = result.ToString() };
await results.AddAsync(visionResult);
log.LogInformation($"Results: {result.ToString()}");
}
}
public class VisionResult : TableEntity
{
public VisionResult(string id, string partitionKey)
{
this.RowKey = id;
this.PartitionKey = partitionKey;
}
public string ResultJson { get; set; }
}
}
Url: https://mycognitive1000.cognitiveservices.azure.com/
First, Welcome to SO! It seems you've copied the code from the documentation without replacing the placeholders namely storageAccount,Key,Url... which are required for the service to work. For example, in your case, the URL posted under your question should go in the VisionUrl property as follows:
[VisionAnalysis(VisionKey = "YOUR_KEY", VisionUrl = "mycognitive1000.cognitiveservices.azure.com")]
The storage account is an Azure Blob Storage connection string. As for the Vision Key and Url, you can find a good tutorial and documentation on the Computer Vision API in the official docs. The tutorial explains as well how to have those parameters written within a separate configuration file as a best practice.

Microsoft Bot framework assembly references

I have the following code but from the Microsoft bot creation tutorial https://learn.microsoft.com/en-us/azure/bot-service/dotnet/bot-builder-dotnet-quickstart
When I copy and paste it my using statements don't seem to be being used when they should be in the example? I've tried adding the using statements it suggest but I don't think that is required. I have errors on [BotAuthentication] and Activity "Type or namespace name 'Activity' could not be found' etc
I have the nugget packages installed as well.
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Connector;
using System;
using System.Linq;
using System.Configuration;
using Microsoft.Bot.Builder.CognitiveServices.QnAMaker;
using System.Web.Services.Description;
using Microsoft.Bot.Builder.PersonalityChat;
using Microsoft.Bot.Builder.PersonalityChat.Core;
namespace BenTestBot
{
[BotAuthentication]
public class MessagesController : ApiController
{
public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
{
if (activity.GetActivityType() == ActivityTypes.Message)
{
//await Conversation.SendAsync(activity, () => new Qna_Rich_Cards.Dialogs.QnaDialog().DefaultIfException());
await Conversation.SendAsync(activity, () => new Dialogs.BasicPersonalityChatBotDialog().DefaultIfException());
}
else
{
await HandleSystemMessageAsync(activity);
}
var response = Request.CreateResponse(HttpStatusCode.OK);
return response;
}
If you have already installed the nuget packages then this should not be there.
Maybe you should check the version of the packages and try updating the packages.
For [BotAuthentication] and activity to work you need Microsoft.Bot.Connector; that is already there in your case so just try updating.

System.Web.Http assembly not allways visible in same c# project

I have a C# project where in my controller class the System.Web.Http library can be referenced but in the another class it cannot. The reference has been added to the overall project and both classes have all the necessary using directives.
The Request method of System.Web.Http cannot be resolved in some instances?
Here are code snippets of the two classes:
Controllers/FormsController.cs
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http;
using System.Web.Http.Description;
using FormsImport.Models;
namespace FormsImport.Controllers
{
public class TCSTasksController : ApiController
{
[Route("api/TCSUploadFile")]
[AllowAnonymous]
public async Task<HttpResponseMessage> UploadCSVFile()
{
try
{
var httpRequest = HttpContext.Current.Request;
foreach (string file in httpRequest.Files)
{
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created); // <-- the name Request does exists
.
.
.
}
CSVmanager.cs
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http; //<-- Compiler claims this directive is unneccesary
using System.Web.Http.Description;
using FormsImport.Models;
namespace FormsImport
{
public class CSVmgr
{
public async Task<HttpResponseMessage> UploadCSVFile()
{
try
{
var httpRequest = HttpContext.Current.Request;
foreach (string file in httpRequest.Files)
{
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created); // <-- the name Request Does not exist in the current context
.
.
.
}
I think you should agree with your compiler/IDE (Visual Studio?) in this case - it's simply pointing out that the referenced assembly is not in use in this class.
Should you decide to use any functionality from this assembly, the warning in question will go away.
EDIT based on comment: If the functionality you need is part of a protected interface of another class, such as ApiController, you MUST extend that class to access such functionality in your own class. If such extension subsequently uses methods in the assembly you referenced (and for which the compiler shows an unnecessary using directive), the compiler warning regarding the using directive will likewise resolve.
in your api controller, Request is a HttpRequestMessage type, and the CreateResponse is the extension method of HttpRequestMessage. why not create a HttpResponseMessage new instance in your CSVmgr class. like:
HttpResponseMessage response = new HttpResponseMessage();
response.StatusCode = HttpStatusCode.Created;
codran 17 explained it the best.
My new class is not derived from the APIController Class and the Request method of that class is not a static function that I can call like System.IO.Path() for instance. So I must use another static function from a class that returns the same results or pass a reference to the APIController to my new class.
Like so:
public async Task<HttpResponseMessage> UploadCSVFile(ApiController controller)
{
Dictionary<string, object> dict = new Dictionary<string, object>();
try
{
var httpRequest = HttpContext.Current.Request;
foreach (string file in httpRequest.Files)
{
HttpResponseMessage response = controller.Request.CreateResponse(HttpStatusCode.Created);

Categories

Resources