本地调试,第一次调用接口可以识别,后续的调用会卡在BizLicenseOCRResponse resp = await client.BizLicenseOCR(req);这里,没有返回,也没有报错。需要重新启动后台接口应用程序和前端web应用程序才能成功。代码如下
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
using TencentCloud.Common;
using TencentCloud.Common.Profile;
using TencentCloud.Ocr.V20181119;
using TencentCloud.Ocr.V20181119.Models;
using MZSP.API.BLL;
using MZSP.DAL;
using MZSP.Entity;
namespace MZSP.View.BLL.Controls
{
class OCRLicenseControl
{
private async Task<BizLicenseOCRResponse> GetAddress(string pLicenseImgUrl)
{
Credential cred = new Credential
{
SecretId = ConfigurationManager.AppSettings["spSecId"].ToString(),
SecretKey = ConfigurationManager.AppSettings["spSecKey"].ToString()
};
ClientProfile clientProfile = new ClientProfile();
HttpProfile httpProfile = new HttpProfile();
httpProfile.Endpoint = ("ocr.tencentcloudapi.com");
clientProfile.HttpProfile = httpProfile;
OcrClient client = new OcrClient(cred, "ap-guangzhou", clientProfile);
BizLicenseOCRRequest req = new BizLicenseOCRRequest();
string strParams = "{\"ImageUrl\":\"" + pLicenseImgUrl + "\"}";
req = BizLicenseOCRRequest.FromJsonString<BizLicenseOCRRequest>(strParams);
BizLicenseOCRResponse resp = await client.BizLicenseOCR(req);
return resp;
}
private void GetPrivonceCity(string RegNum, ref Tschools school)
{
school.FProvince = TareaBLL.GetInstance().GetOneByID(RegNum.Substring(2, 2)).FAreaName;
school.FCity = TareaBLL.GetInstance().GetOneByID(RegNum.Substring(2, 4)).FAreaName;
school.FCounty = TareaBLL.GetInstance().GetOneByID(RegNum.Substring(2, 6)).FAreaName;
}
public object GetShoolInfoFromLicense(string pLicenseImgUrl)
{
Hashtable hs = new Hashtable();
hs["msg"] = "营业执照识别失败";
hs["flag"] = false;
BizLicenseOCRResponse result = null;
try
{
var t = Task.Run(() =>
{
// 同步方式调用,会卡住直到请求返回
result = GetAddress(pLicenseImgUrl).ConfigureAwait(false).GetAwaiter().GetResult();
});
// 同步等待,会卡住直到线程返回
t.Wait();
Tschools school = new Tschools();
GetPrivonceCity(result.RegNum, ref school);
school.FSchoolName = result.Name;
school.FOwner = result.Person;
school.FAddress = result.Address;
hs["SchoolInfoFromLicense"] = school;
hs["msg"] = "营业执照识别成功";
hs["flag"] = true;
return hs;
}
catch (Exception e)
{
FuncUtility.Utils.WriteErrLogToTxt("营业执照识别失败", e);
hs["msg"] = "营业执照识别失败";
hs["flag"] = false;
return hs;
}
}
}
}
相似问题