如何使用c#使用IBM Watson对话服务?

如何使用c#使用IBM Watson对话服务?,c#,ibm-watson,watson-dialog,C#,Ibm Watson,Watson Dialog,一个客户正在使用IBM的Watson Dialog服务,我找不到任何人使用.Net(特别是c#)做最基本的事情的例子 IBM仅展示了使用Curl、Node和Java的示例 我的第一个目标是能够向watson服务添加一个新的xml文件(对话框树)。看起来很简单,但我已经绞尽脑汁了一段时间。所以我最终通过拼凑十几个谷歌搜索相关主题的帮助实现了这一点。我想我会在这里发布工作版本 下面是我的代码,它将使用MVC控制器中的C#将xml文件上载到Watson Dialog服务 前端是一个采用友好名称(我将其

一个客户正在使用IBM的Watson Dialog服务,我找不到任何人使用.Net(特别是c#)做最基本的事情的例子

IBM仅展示了使用Curl、Node和Java的示例


我的第一个目标是能够向watson服务添加一个新的xml文件(对话框树)。看起来很简单,但我已经绞尽脑汁了一段时间。

所以我最终通过拼凑十几个谷歌搜索相关主题的帮助实现了这一点。我想我会在这里发布工作版本

下面是我的代码,它将使用MVC控制器中的C#将xml文件上载到Watson Dialog服务

前端是一个采用友好名称(我将其转换为.xml文件名)和文件本身上传(使用dropzone)的表单

我相信可能会有优化,但我希望这对某些人有所帮助。好消息是,这可以作为任何Watson对话服务调用(添加、更新、删除)的基础。
公共内容结果保存(FormCollection表单)
{
尝试
{
变量名称=形式[“txtDialogName”];
var filename=name+“.xml”;
字节[]字节=null;
foreach(Request.Files中的字符串s)
{
var file=Request.Files[s];
使用(Stream inputStream=file.inputStream)
{
MemoryStream MemoryStream=作为MemoryStream的输入流;
if(memoryStream==null)
{
memoryStream=新的memoryStream();
inputStream.CopyTo(memoryStream);
}
bytes=memoryStream.ToArray();
}
打破
}
如果(字节==null)
{
var contentResult=新contentResult
{
ContentType=“application/json”,
内容=空
};
返回contentResult;
}
var basePath=ConfigurationManager.AppSettings[“WatsonPath”];
var username=ConfigurationManager.AppSettings[“WatsonUsername”];
var password=ConfigurationManager.AppSettings[“WatsonPassword”];
字符串凭据=Convert.ToBase64String(Encoding.ASCII.GetBytes(string.Format(“{0}:{1}”,用户名,密码));
var值=新[]
{新的KeyValuePair(“名称”,文件名)};
使用(var client=new HttpClient())
使用(var formData=new MultipartFormDataContent())
{
client.DefaultRequestHeaders.Authorization=新的AuthenticationHeaderValue(“基本”,凭证);
foreach(值中的var keyValuePair)
{
Add(新的StringContent(keyValuePair.Value)、string.Format(“\{0}\”、keyValuePair.Key));
}
添加(新的ByteArrayContent(字节),
“+”文件“+”,
“'+filename+”);
var response=client.PostAsync(basePath+“/v1/dialogs”,formData);
var result=response.Content.ReadAsStringAsync().result;
如果(!response.issucessStatusCode)
{
var contentResult=新contentResult
{
ContentType=“application/json”,
Content=response.reason短语
};
返回contentResult;
}
var successResult=new ContentResult
{
ContentType=“application/json”,
内容=结果
};
返回成功结果;
}
}
捕获(例外情况除外)
{
HandleError(ex);
var contentResult=新contentResult
{
ContentType=“application/json”,
Content=“false”
};
返回contentResult;
}
}
    public ContentResult Save(FormCollection form)
    {
        try
        {
            var name = form["txtDialogName"];

            var filename = name + ".xml";
            byte[] bytes = null;

            foreach (string s in Request.Files)
            {
                var file = Request.Files[s];

                using (Stream inputStream = file.InputStream)
                {
                    MemoryStream memoryStream = inputStream as MemoryStream;
                    if (memoryStream == null)
                    {
                        memoryStream = new MemoryStream();
                        inputStream.CopyTo(memoryStream);
                    }

                    bytes = memoryStream.ToArray();
                }

                break;
            }

            if (bytes == null)
            {
                var contentResult = new ContentResult
                {
                    ContentType = "application/json",
                    Content = null
                };
                return contentResult;
            }


            var basePath = ConfigurationManager.AppSettings["WatsonPath"];

            var username = ConfigurationManager.AppSettings["WatsonUsername"];
            var password = ConfigurationManager.AppSettings["WatsonPassword"];
            string credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes(string.Format("{0}:{1}", username, password)));

            var values = new[]
                         { new KeyValuePair<string, string>("name", filename) };

            using (var client = new HttpClient())
            using (var formData = new MultipartFormDataContent())
            {
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", credentials);

                foreach (var keyValuePair in values)
                {
                    formData.Add(new StringContent(keyValuePair.Value), string.Format("\"{0}\"", keyValuePair.Key));
                }

                formData.Add(new ByteArrayContent(bytes),
                               '"' + "file" + '"',
                               '"' + filename + '"');

                var response = client.PostAsync(basePath + "/v1/dialogs", formData).Result;
                var result = response.Content.ReadAsStringAsync().Result;
                if (!response.IsSuccessStatusCode)
                {
                    var contentResult = new ContentResult
                    {
                        ContentType = "application/json",
                        Content = response.ReasonPhrase
                    };
                    return contentResult;
                }

                var successResult = new ContentResult
                {
                    ContentType = "application/json",
                    Content = result
                };

                return successResult;
            }
        }
        catch (Exception ex)
        {
            HandleError(ex);
            var contentResult = new ContentResult
            {
                ContentType = "application/json",
                Content = "false"
            };
            return contentResult;
        }
    }