Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Dialogflow V2 api的不工作示例_C#_Dialogflow Es - Fatal编程技术网

C# Dialogflow V2 api的不工作示例

C# Dialogflow V2 api的不工作示例,c#,dialogflow-es,C#,Dialogflow Es,在C#SDK文档中遇到的问题可在此处找到: 没有对方法ToChannelCredentials()的引用。 我们无法将SDK连接到dialogflow,即使使用空白项目也是如此。此方法是否仍然存在或已弃用 using Google.Cloud.Dialogflow.V2; using Google.Apis.Auth.OAuth2; using Grpc.Auth; using Grpc.Core; ... GoogleCredential cred = GoogleCredential.Fr

在C#SDK文档中遇到的问题可在此处找到:

没有对方法ToChannelCredentials()的引用。 我们无法将SDK连接到dialogflow,即使使用空白项目也是如此。此方法是否仍然存在或已弃用

using Google.Cloud.Dialogflow.V2;
using Google.Apis.Auth.OAuth2;
using Grpc.Auth;
using Grpc.Core;
...
GoogleCredential cred = GoogleCredential.FromFile("/path/to/credentials.json");
Channel channel = new Channel(
    SessionsClient.DefaultEndpoint.Host, SessionsClient.DefaultEndpoint.Port, cred.ToChannelCredentials());
SessionsClient client = SessionsClient.Create(channel);
...
// Shutdown the channel when it is no longer required.
channel.ShutdownAsync().Wait();

您是否尝试过使用服务帐户私钥连接?(Json文件

遵循以下步骤(C#中的工作示例)

  • 创建Dialogflow agent后,转到该agent的设置-->常规-->单击服务帐户链接
  • 您将被发送到谷歌云平台,在那里您可以创建一个服务帐户
  • 创建服务帐户后,将有一个选项来创建密钥,创建它并下载它的(JSON)格式
  • 此键将用于从C#项目连接到Dialogflow代理
  • 在项目中安装Google.Cloud.Dialogflow.V2
  • 例如,创建一个Dialogflow管理器类(查看下面的示例)


  • 在#7:如何/在何处实例化
    \u hostingEnvironment
    ?@Shiasu sama下的示例中,它被注入到构造函数(DI)中,我在该构造函数中调用DialogflowManagerWhat
    \u contentRootPath
    应设置为?这些用于获取json密钥文件的实际位置,我刚刚注意到管理器中没有使用webRoothPath,因此您可以忽略它,但是_contentRootPath返回包含应用程序内容文件的目录的绝对路径
        public class DialogflowManager {
        private string _userID;
        private string _webRootPath;
        private string _contentRootPath;
        private string _projectId;
        private SessionsClient _sessionsClient;
        private SessionName _sessionName;
    
        public DialogflowManager(string userID, string webRootPath, string contentRootPath, string projectId) {
    
            _userID = userID;
            _webRootPath = webRootPath;
            _contentRootPath = contentRootPath;
            _projectId = projectId;
            SetEnvironmentVariable();
    
        }
    
        private void SetEnvironmentVariable() {
            try {
                Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", _contentRootPath + "\\Keys\\{THE_DOWNLOADED_JSON_FILE_HERE}.json");
            } catch (ArgumentNullException) {
                throw;
            } catch (ArgumentException) {
                throw;
            } catch (SecurityException) {
                throw;
            }
        }
    
        private async Task CreateSession() {
            // Create client
            _sessionsClient = await SessionsClient.CreateAsync();
            // Initialize request argument(s)
            _sessionName = new SessionName(_projectId, _userID);
    
        }
    
        public async Task < QueryResult > CheckIntent(string userInput, string LanguageCode = "en") {
            await CreateSession();
            QueryInput queryInput = new QueryInput();
            var queryText = new TextInput();
            queryText.Text = userInput;
            queryText.LanguageCode = LanguageCode;
            queryInput.Text = queryText;
    
            // Make the request
            DetectIntentResponse response = await _sessionsClient.DetectIntentAsync(_sessionName, queryInput);
            return response.QueryResult;
        }
    }
    
         DialogflowManager dialogflow = new DialogflowManager("{INSERT_USER_ID}",
        _hostingEnvironment.WebRootPath,
        _hostingEnvironment.ContentRootPath,
        "{INSERT_AGENT_ID");
    
    var dialogflowQueryResult = await dialogflow.CheckIntent("{INSERT_USER_INPUT}");