我有一个Azure函数,该函数带有一些参数并创建对另一个API的请求。在本地Visual Studio中,尽管代码相同,但是它可以正常工作,但不能在Azure门户上运行。
我已经使用本地和Azure门户检查了所有代码,但不明白我在做什么错。这是我的代码段。
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; }
}
答案 0 :(得分:2)
似乎您正在尝试在azure portal
上实现本地功能。
就您而言,所有代码似乎都还可以。但是本地Visual Studio功能和azure portal
几乎没有变化。
在迁移到azure portal
#r "Newtonsoft.Json"
您还错过了以下参考资料 :
using System.Text;
使用时
var stringContent = new StringContent(json, UnicodeEncoding.UTF8, "application/json");
需要以上参考。
将本地功能转移到Azure门户时的良好做法
1。不要将完整的代码从本地复制并粘贴到Azure portal
2。传送一小段代码和Save & Run
3。如果每个块都工作正常,则移至下一个块
4。不要先将类文件添加为.csx,请在 功能正常运行后,然后在文件部分添加为.csx
5。注意对Azure portal
6。以上做法肯定会降低您的错误发生率。
注意:
Local visual studio function
和Azure portal
差别不大。您最好先创建新功能。后罐 根据您的需要进行增强,可以减少错误。
如果您仍有任何疑问可以分享评论。谢谢,祝您编程愉快!