Reputation: 91
I have an Azure function which take some parameters and creates a request to another API. In the local Visual Studio, it works fine but it does not work on Azure portal, though the code is the same.
I have inspected all the code with local and Azure portal but don't understand what I am doing wrong. Here is my code snippet.
using System.Net;
using Newtonsoft.Json;
using System.Net.Http;
using System.Linq;
using System.Collections.Generic;
public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)]HttpRequestMessage req, ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
try
{
// Convert all request perameter into Json object
var content = req.Content;
string jsonContent = content.ReadAsStringAsync().Result;
dynamic requestPram = JsonConvert.DeserializeObject<CaseRequestModel>(jsonContent);
// Validate required param
if (string.IsNullOrEmpty(requestPram.email.Trim()))
{
return req.CreateResponse(HttpStatusCode.OK, "email required!");
}
//Create API request
HttpClient client = new HttpClient();
//Partner contact class
Contact objContactModel = new Contact();
//Call Contact API by Email
HttpRequestMessage newRequest = new HttpRequestMessage(HttpMethod.Get, string.Format("https://APIURL/api/ConnectUs/GetPartnerContacts?email={0}", requestPram.email.Trim()));
HttpResponseMessage response = await client.SendAsync(newRequest);
List<Contact> listPartnerContact = new List<Contact>();
//Read Server Response
listPartnerContact = await response.Content.ReadAsAsync<List<Contact>>();
if (listPartnerContact == null)
{
return req.CreateResponse(HttpStatusCode.OK, "No Partner found!");
}
//Filter one record for binding class for Request Case
var filterCasePartnerClass = listPartnerContact.FirstOrDefault();
if (filterCasePartnerClass == null)
{
return req.CreateResponse(HttpStatusCode.OK, "No Partner found!");
}
// Instancify PartnerCaseApiRequestModel class for submitting Case Request
CaseApiRequestModel caseRequestModel = new CaseApiRequestModel();
caseRequestModel.serviceCatalog = Guid.NewGuid();
caseRequestModel.dispatchToTeam = Guid.NewGuid();
caseRequestModel.title = requestPram.title;
caseRequestModel.description = requestPram.description;
caseRequestModel.contact = filterCasePartnerClass;
// Post data to Partner Case Request API
// Convert json
string routingActivityId = null;
var json = JsonConvert.SerializeObject(caseRequestModel);
var stringContent = new StringContent(json, UnicodeEncoding.UTF8, "application/json");
try
{
HttpResponseMessage responseFromCaseApi = await client.PostAsync("MyURL", stringContent);
if (responseFromCaseApi.IsSuccessStatusCode)
{
routingActivityId = responseFromCaseApi.Content.ReadAsStringAsync().Result;
}
}
catch (Exception ex)
{
Console.Error.WriteLine(ex.Message);
}
return req.CreateResponse(HttpStatusCode.OK, new CaseResponseModel { CaseRequestId = routingActivityId });
}
catch (Exception ex)
{
Console.Error.WriteLine(ex.Message);
return req.CreateResponse(HttpStatusCode.OK, "");
}
}
public class CaseResponseModel
{
public string CaseRequestId { get; set; }
}
public class Contact
{
public string contactId { get; set; }
public string mpnID { get; set; }
public string partnerName { get; set; }
public string contract { get; set; }
public DateTime enrollmentEndDate { get; set; }
public string advisoryHours { get; set; }
public string fieldMotion { get; set; }
public string firstName { get; set; }
public string lastName { get; set; }
public string phoneNumber { get; set; }
public string email { get; set; }
public int contactLanguageCode { get; set; }
}
public class CaseRequestModel
{
public string email { get; set; }
public string title { get; set; }
public string description { get; set; }
}
public class CaseApiRequestModel
{
public Guid serviceCatalog { get; set; }
public Guid dispatchToTeam { get; set; }
public string title { get; set; }
public string description { get; set; }
public Contact contact { get; set; }
}
Upvotes: 2
Views: 433
Reputation: 22419
Its seems you are trying to implement your local function on azure portal
.
In your case , all the code seems okay. But local visual studio function and azure portal
has little change.
Remember to add this reference below while migrate to azure portal
#r "Newtonsoft.Json"
You have also missed following reference :
using System.Text;
As you are using
var stringContent = new StringContent(json, UnicodeEncoding.UTF8, "application/json");
Which requires above reference.
Good Practice While Transfer Local Function to Azure Portal
1. Do not copy and paste the full code from local to Azure portal
2. Transfer small chunk of code and Save & Run
3. If each chunk works well then move to next chunk
4. Do not add the class file as .csx first, add class below the function once it works fine then add as .csx on file section
5. Be care about local reference a to Azure portal
6. Above practice definitely reduced your error occurrence rate.
Note:
Local visual studio function
andAzure portal
has little difference.You should better create new function first. Latter can enhance according to your needs which may reduce error.
If you still have any query feel to share in comment. Thanks and happy coding!
Upvotes: 2