我有网络频道聊天机器人通过C#创建使用bot框架SDK V4。它有多个瀑布对话框,根据主对话框中选择的选项执行一组操作。
在其中一个对话框中,我的要求是用户输入一些数据,然后使用它在AZURE DEvOps项目中创建一个Type任务的工作项,以便进行跟踪。我能够成功地从用户那里获取数据,但是当在devops中创建工作项时,我面临着问题。我已经尝试了两件事,但如果通过创建单个C#控制台应用程序来执行,它们就会工作,但是如果我试图通过安装相关的NuGET包或添加引用程序集来使用相同的代码,则会收到错误或警告。
Try 1:在BOTCode:中使用与TFS相关的nuget包
如果我试图通过安装与TFS扩展客户端相关的Nuget包来使用代码,那么在安装期间,它会说兼容性警告和我的引用程序集部分有一个警告符号。通过我没有尝试在我的对话框类中执行这段代码,因为虽然它可能有工作,但我不确定在发布到AZURE之后,它可能会引起问题。
现在来试试2 2:在BOT代码中使用AZURE DEVOPS REST:
我编写了调用REST的代码,我在Dialog类中使用了以下代码:
string token = "toekn";
string type = "Task";
string organization = org
string project = "Project";
int workitemid = 0;
string url = $"https://dev.azure.com/{organization}/{project}/_apis/wit/workitems/${type}?api-version=5.0";
JavaScriptSerializer serializer = new JavaScriptSerializer();
string json = serializer.Serialize(new object[]{new
{
op = "add",
path = "/fields/System.Title",
value = "Testing Workitem creation through API"
},
new {
op = "add",
path = "/fields/System.Description",
value = "Model Request ID#" + requestid + " from: "+ name + " requested from ChatBot"
},
new {
op = "add",
path = "/fields/Priority",
value = 1
}
});
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}", "", token))));
var method = new HttpMethod("POST");
var request = new HttpRequestMessage(method, url)
{
Content = new StringContent(json, Encoding.UTF8,
"application/json-patch+json")
};
var sendresult = client.SendAsync(request).Result;
var result = sendresult.Content.ReadAsStringAsync().Result;
Console.WriteLine("Completed!");
dynamic workitemdata = JsonConvert.DeserializeObject(result);
workitemid = workitemdata.id;
};如果你在上面的代码中观察到有一个方法-
JavaScriptSerializer序列化程序=新的JavaScriptSerializer();
这需要一个名为: system.web.extensions.dll的程序集引用,我通过浏览这个DLL添加了这个引用,即使用System.Web.Script.Serialization;
现在,当我执行此操作时,我得到了如下异常:捕获的异常:无法加载文件或程序集'System.Web.Extensions,Version=4.0.0.0,Culture=neutral,PublicKeyToken=31bf3856ad364e35‘。不应加载引用程序集以供执行。它们只能在只反射加载器上下文中加载。(HRESULT例外: 0x80131058)
当我在this博客中搜索这个错误时,他们说要从csproj文件中删除与tags框架相关的标记,这应该可以工作,但是这给出了构建错误,说明没有找到tags框架。
这就是我被困的地方。我还附上未经修改的csproj文件,以供参考。
请注意,我已经在Azure中创建了一个基本的回显机器人,然后下载了这个基本的bot,并根据我的需求从零开始构建了我自己的水对话框。
请帮助我解开这个问题,因为我尝试了几件事情,但没有工作。如果不能做到这一点,请让我知道,这样我就可以与我的团队进行同样的交流。在这里,谢谢您的帮助。
我还尝试使用this博客中的以下代码,这也不起作用。当我被封锁时,我会发布这个查询以寻求帮助:
static void Main(string[] args)
{
CreateWorkItem();
}
public static void CreateWorkItem()
{
string _tokenAccess = "************"; //Click in security and get Token and give full access https://azure.microsoft.com/en-us/services/devops/
string type = "Bug";
string organization = "type your organization";
string proyect = "type your proyect";
string _UrlServiceCreate = $"https://dev.azure.com/{organization}/{proyect}/_apis/wit/workitems/${type}?api-version=5.0";
dynamic WorkItem = new List<dynamic>() {
new
{
op = "add",
path = "/fields/System.Title",
value = "Sample Bug test"
}
};
var WorkItemValue = new StringContent(JsonConvert.SerializeObject(WorkItem), Encoding.UTF8, "application/json-patch+json");
var JsonResultWorkItemCreated = HttpPost(_UrlServiceCreate, _tokenAccess, WorkItemValue);
}
public static string HttpPost(string urlService, string token, StringContent postValue)
{
try
{
string request = string.Empty;
using (HttpClient httpClient = new HttpClient())
{
httpClient.DefaultRequestHeaders.Accept.Clear();
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}", "", token))));
using (HttpRequestMessage httpRequestMessage = new HttpRequestMessage(new HttpMethod("POST"), urlService) { Content = postValue })
{
var httpResponseMessage = httpClient.SendAsync(httpRequestMessage).Result;
if (httpResponseMessage.IsSuccessStatusCode)
request = httpResponseMessage.Content.ReadAsStringAsync().Result;
}
}
return request;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}下面是csproj文件中的数据供参考:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.Bot.Builder.AI.QnA" Version="4.6.0" />
<PackageReference Include="Microsoft.Bot.Builder.Dialogs" Version="4.6.0" />
<PackageReference Include="Microsoft.Bot.Builder.Integration.AspNet.Core" Version="4.6.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
<PackageReference Include="System.Data.SqlClient" Version="4.7.0" />
</ItemGroup>
<ItemGroup>
<Reference Include="System.Web.Extensions">
<HintPath>..\..\..\..\..\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.7.2\System.Web.Extensions.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Content Update="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
<Import Project="PostDeployScripts\IncludeSources.targets" Condition="Exists('PostDeployScripts\IncludeSources.targets')" />
<Import Project="..\PostDeployScripts\IncludeSources.targets" Condition="Exists('..\PostDeployScripts\IncludeSources.targets')" />
</Project>发布于 2019-11-01 07:14:22
当我从post尝试这个想法时,我将结束这个查询。而且起作用了。
因此,正如我在Try 2代码中解释的那样,将数据转换为JavascriptSerializer对象的字符串作为给定代码从行开始转换为JSON:
JavaScriptSerializer serializer = new JavaScriptSerializer();我所做的不是这样做,而是使用上面给出的博客创建了一个类,它已经设置了;并获得了变量为
OP路径值
public class WorkItemData
{
public string op { get; set; }
public string path { get; set; }
public string value { get; set; }
}在我实际的bot代码中,创建了一个列表变量:
List<WorkItemData> wiarray= new List<WorkItemData>;如上面给出的博客链接所示,将数据添加到类和数组中,然后使用下面的代码行将其转换为Json并存储到变量中:
字符串wijsondata = JsonConvert.SerializeObject(wiarray);
并且使用给出的代码的其余部分-- try 2块--在我最初的问题中称为post方法,因为它现在不是Json变量,而是传递了wijsondata。
Content =新的StringContent(wijsondata,Encoding.UTF8,“application/json-补丁+json”)
而且起作用了。
谢谢你在博客里,在这篇文章里,或者在外面的文章中提供的帮助和想法,并对它给任何人造成的任何不便表示歉意。
https://stackoverflow.com/questions/58646265
复制相似问题