应用程序功能的基于文件的工件
File Based Artifacts for App Function
我是 app-functions 的新手。但我已经成功创建了一个自定义应用程序函数,它将接受一些 JSON.
上传我的 JSON 后,我的 app-function 将要根据架构对其进行验证。 JSON 将在 POST 请求的 body 中,以更准确地描述正在发生的事情。
http://www.newtonsoft.com/json/help/html/JsonSchema.htm
这是验证的基本代码:
string schemaJson = @"{
'description': 'A person',
'type': 'object',
'properties':
{
'name': {'type':'string'},
'hobbies': {
'type': 'array',
'items': {'type':'string'}
}
}
}";
JsonSchema schema = JsonSchema.Parse(schemaJson);
JObject person = JObject.Parse(@"{
'name': 'James',
'hobbies': ['.NET', 'Blogging', 'Reading', 'Xbox', 'LOLCATS']
}");
IList<string> messages;
bool valid = person.IsValid(schema, out messages);
以上代码有
的地方
string schemaJson
我想要保存一个我加载并验证的本地文件。
实际上,我会有几个文件,http-request中的一个参数会触发我使用哪个文件json-schema-validate。
假设 "mycustomerid" 在 http-request 的 header 中传递(或 query-string 或其他),mycustomerid 的值将驱动哪个模式我想验证输入 json.
所以我会有几个文件。
customer_1.jsonschema
customer_2.jsonschema
customer_3.jsonschema
什么是best-practice来存储这些文件是我相当简单的app-function所必需的
public static async Task<object> Run(HttpRequestMessage req, TraceWriter log)
{
追加:
这是我使用此处接受的答案以及我在此处找到的答案的最终答案:How to get local file system path in azure websites
string rootDirectory = string.Empty;
if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("HOME")))
{
/* running in azure */
rootDirectory = Environment.GetEnvironmentVariable("HOME") + "\site\wwwroot";
}
else
{
/* in visual studio, local debugging */
rootDirectory = ".";
}
string path = rootDirectory + "\MySubFolder\MyFile.jsonschema";
第 1 步。使用 Kudu 控制台在 'D:\home\site\wwwroot\HttpTriggerCSharp1' 中创建文件 'schema.json'
https://github.com/projectkudu/kudu/wiki/Kudu-console
第 2 步。您的 HttpTrigger 将文件内容读取为字符串的代码:
using System.Net;
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
var path = Environment.GetEnvironmentVariable("HOME").ToString() + "\site\wwwroot\HttpTriggerCSharp1\schema.json";
string readText = File.ReadAllText(path);
log.Info(readText);
return req.CreateResponse(HttpStatusCode.OK, "Hello ");
}
我是 app-functions 的新手。但我已经成功创建了一个自定义应用程序函数,它将接受一些 JSON.
上传我的 JSON 后,我的 app-function 将要根据架构对其进行验证。 JSON 将在 POST 请求的 body 中,以更准确地描述正在发生的事情。
http://www.newtonsoft.com/json/help/html/JsonSchema.htm
这是验证的基本代码:
string schemaJson = @"{
'description': 'A person',
'type': 'object',
'properties':
{
'name': {'type':'string'},
'hobbies': {
'type': 'array',
'items': {'type':'string'}
}
}
}";
JsonSchema schema = JsonSchema.Parse(schemaJson);
JObject person = JObject.Parse(@"{
'name': 'James',
'hobbies': ['.NET', 'Blogging', 'Reading', 'Xbox', 'LOLCATS']
}");
IList<string> messages;
bool valid = person.IsValid(schema, out messages);
以上代码有
的地方string schemaJson
我想要保存一个我加载并验证的本地文件。
实际上,我会有几个文件,http-request中的一个参数会触发我使用哪个文件json-schema-validate。
假设 "mycustomerid" 在 http-request 的 header 中传递(或 query-string 或其他),mycustomerid 的值将驱动哪个模式我想验证输入 json.
所以我会有几个文件。
customer_1.jsonschema
customer_2.jsonschema
customer_3.jsonschema
什么是best-practice来存储这些文件是我相当简单的app-function所必需的
public static async Task<object> Run(HttpRequestMessage req, TraceWriter log)
{
追加:
这是我使用此处接受的答案以及我在此处找到的答案的最终答案:How to get local file system path in azure websites
string rootDirectory = string.Empty;
if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("HOME")))
{
/* running in azure */
rootDirectory = Environment.GetEnvironmentVariable("HOME") + "\site\wwwroot";
}
else
{
/* in visual studio, local debugging */
rootDirectory = ".";
}
string path = rootDirectory + "\MySubFolder\MyFile.jsonschema";
第 1 步。使用 Kudu 控制台在 'D:\home\site\wwwroot\HttpTriggerCSharp1' 中创建文件 'schema.json' https://github.com/projectkudu/kudu/wiki/Kudu-console
第 2 步。您的 HttpTrigger 将文件内容读取为字符串的代码:
using System.Net;
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
var path = Environment.GetEnvironmentVariable("HOME").ToString() + "\site\wwwroot\HttpTriggerCSharp1\schema.json";
string readText = File.ReadAllText(path);
log.Info(readText);
return req.CreateResponse(HttpStatusCode.OK, "Hello ");
}