C# .NET核心GoogleCloudMlV1PredictRequest执行方法返回空响应
在.NETCoreC#中,我使用GooglesMLAPI与引擎交互。我的预测方法的代码在这里C# .NET核心GoogleCloudMlV1PredictRequest执行方法返回空响应,c#,.net,.net-core,google-cloud-ml-engine,C#,.net,.net Core,Google Cloud Ml Engine,在.NETCoreC#中,我使用GooglesMLAPI与引擎交互。我的预测方法的代码在这里 string credPath = @".\appkey.json"; var json = File.ReadAllText(credPath); PersonalServiceAccountCred cr = JsonConvert.DeserializeObject(json); // Create an explicit ServiceAccountCredential credential
string credPath = @".\appkey.json";
var json = File.ReadAllText(credPath);
PersonalServiceAccountCred cr = JsonConvert.DeserializeObject(json);
// Create an explicit ServiceAccountCredential credential
var xCred = new ServiceAccountCredential(new
ServiceAccountCredential.Initializer(cr.ClientEmail)
{
Scopes = new [] {
CloudMachineLearningEngineService.Scope.CloudPlatform
}
}.FromPrivateKey(cr.PrivateKey));
var service = new CloudMachineLearningEngineService(new BaseClientService.Initializer {
HttpClientInitializer = xCred
});
ProjectsResource.PredictRequest req = new ProjectsResource.PredictRequest(service, new GoogleCloudMlV1PredictRequest {
HttpBody = new GoogleApiHttpBody {
Data = "{\"instances\": [{\"age\": 25, \"workclass\": \" Private\", \"education\": \" 11th\", \"education_num\": 7, \"marital_status\": \" Never - married\", \"occupation\": \" Machine - op - inspct\", \"relationship\": \" Own - child\", \"race\": \" Black\", \"gender\": \" Male\", \"capital_gain\": 0, \"capital_loss\": 0, \"hours_per_week\": 40, \"native_country\": \" United - States\"}]}"
}, "projects/{project_name}/models/census/versions/v1");
GoogleApiHttpBody body = req.Execute();
但是,我在GoogleApiHttpBody对象上得到了以下响应:
有人知道发生了什么吗?在检查Google API并看到请求通过后,我认为库可能有问题。我在自己的博客上发布了详细信息: 发生的情况是GoogleApiHttpBody没有序列化:predict端点所期望的“实例”对象。当我读到流并看到这个响应时,我发现:
{"error": "<strong>Missing \"instances\" field in request body</strong>: {\"httpBody\":{\"data\":\"{\\\"instances\\\":[{\\\"age\\\":25,\\\"workclass\\\":\\\" Private\\\",\\\"education\\\":\\\" 11th\\\",\\\"education_num\\\":7,\\\"marital_status\\\":\\\" Never - married\\\",\\\"occupation\\\":\\\" Machine - op - inspct\\\",\\\"relationship\\\":\\\" Own - child\\\",\\\"race\\\":\\\" Black\\\",\\\"gender\\\":\\\" Male\\\",\\\"capital_gain\\\":0,\\\"capital_loss\\\":0,\\\"hours_per_week\\\":40,\\\"native_country\\\":\\\" United - States\\\"}]}\"}}"}
你是对的--你需要发送原始请求。在您的示例中,
toSend
是未绑定的,因此我假设所示代码中存在错误?您是正确的!我试图清理堆栈溢出,但没有重新编译。谢谢@Rhartel80
string credPath = @".\appkey.json";
var json = File.ReadAllText(credPath);
PersonalServiceAccountCred cr = JsonConvert.DeserializeObject(json);
// Create an explicit ServiceAccountCredential credential
var xCred = new ServiceAccountCredential(new ServiceAccountCredential.Initializer(cr.ClientEmail) {
Scopes = new [] {
CloudMachineLearningEngineService.Scope.CloudPlatform
}
}.FromPrivateKey(cr.PrivateKey));
var service = new CloudMachineLearningEngineService(new BaseClientService.Initializer {
HttpClientInitializer = xCred
});
ProjectsResource.PredictRequest req = new ProjectsResource.PredictRequest(service, new GoogleCloudMlV1PredictRequest(), "projects/{project-name}/models/census/versions/v1");
string requestPath = req.Service.BaseUri +
CloudMachineLearningEngineService.Version + "/" + req.Name + ":" + req.MethodName;
Task result = service.HttpClient.PostAsync(requestPath, new StringContent("{\"instances\": [{\"age\": 25, \"workclass\": \" Private\", \"education\": \" 11th\", \"education_num\": 7, \"marital_status\": \" Never - married\", \"occupation\": \" Machine - op - inspct\", \"relationship\": \" Own - child\", \"race\": \" Black\", \"gender\": \" Male\", \"capital_gain\": 0, \"capital_loss\": 0, \"hours_per_week\": 40, \"native_country\": \" United - States\"}]}"));
Task.WaitAll(result);
HttpResponseMessage responseMessage = result.Result;
Task responseStreamTask = responseMessage.Content.ReadAsStringAsync();
Task.WaitAll(responseStreamTask);
string responseText = responseStreamTask.Result;