[ Foro de C# ]
Hola, necesito alguna pista o ayuda, tengo un código que funciona correctamente como programa pero que al meterlo dentro de un controller no me funciona. Seguramente es algo básico pero desconozco por completo.
Esto funciona perfectamente:
/////////////////////////////
static async Task Main(string[] args)
{
var result = await searchReviewsByIdProductAsync("hhcbaaczbzj");
System.Environment.Exit(1);
}
private static async Task<string> searchReviewsByIdProductAsync(string idProduct)
{
var result = "";
Console.WriteLine("searchReviewByIdProduct:" + idProduct);
//define connections
var conn1Config = new Dictionary<string, object>();
//config connection 1
conn1Config.Add("baseUrl", "https://test.ipdb.io");
BlockchainConnection conn1 = new BlockchainConnection(conn1Config);
//add connections
IList<BlockchainConnection> connections = new List<BlockchainConnection>();
connections.Add(conn1);
//multiple connections
var builderWithConnections = BigchainDbConfigBuilder
.addConnections(connections)
.setTimeout(20000); //override default timeout of 20000 milliseconds
// single connection
var builder = BigchainDbConfigBuilder
.baseUrl("https://test.ipdb.io");
if (!AsyncContext.Run(() => builder.setup()))
{
Console.WriteLine("Failed to setup");
};
// prepare your key
var algorithm = SignatureAlgorithm.Ed25519;
var privateKey = Key.Import(algorithm, Utils.StringToByteArray(privateKeyString), KeyBlobFormat.PkixPrivateKey);
var publicKey = PublicKey.Import(algorithm, Utils.StringToByteArray(publicKeyString), KeyBlobFormat.PkixPublicKey);
var assets = await AssetsApi<object>.getAssetsAsync(idProduct);
try
{
for (int i = 0; i < assets.Count; i++)
{
JsonSerializer serializer = new JsonSerializer();
//ReviewsAsset review = (ReviewsAsset)serializer.Deserialize((Newtonsoft.Json.Linq.JObject)assets[i].Data, typeof(ReviewsAsset));
var json = assets[i].Data.ToString().Substring(0, assets[i].Data.ToString().Length);
string jsonCliente = JsonConvert.SerializeObject(assets[i].Data);
//var dictionary = serializer.Deserialize<Dictionary<string, object>>(assets[i].Data);
result += assets[i].Data.ToString() + "@|@";
}
}
catch (Exception e)
{
Console.WriteLine("Error: " + e.Message);
}
return result;
}
///////////////
pero si lo pongo en un Controller tal que así:
//////////////////////////////////////////
[System.Web.Http.HttpGet]
public async Task<string> searchReviewsByIdProductAsync(string id)
{
var result = "";
try
{
//define connections
var conn1Config = new Dictionary<string, object>();
//config connection 1
conn1Config.Add("baseUrl", "https://test.ipdb.io");
BlockchainConnection conn1 = new BlockchainConnection(conn1Config);
//add connections
IList<BlockchainConnection> connections = new List<BlockchainConnection>();
connections.Add(conn1);
//multiple connections
var builderWithConnections = BigchainDbConfigBuilder
.addConnections(connections)
.setTimeout(20000); //override default timeout of 20000 milliseconds
var builder = BigchainDbConfigBuilder
.baseUrl("https://test.ipdb.io")
.webSocketMonitor(new ValidTransactionMessageHandler())
.setup();
//if (!AsyncContext.Run(() => builder.setup()))
//{
// Console.WriteLine("Failed to setup");
//};
// prepare your key
var algorithm = SignatureAlgorithm.Ed25519;
var privateKey = Key.Import(algorithm, Utils.StringToByteArray(privateKeyString), KeyBlobFormat.PkixPrivateKey);
var publicKey = PublicKey.Import(algorithm, Utils.StringToByteArray(publicKeyString), KeyBlobFormat.PkixPublicKey);
var assets = await AssetsApi<object>.getAssetsAsync(id);
if (assets != null)
{
if (assets.Count > 0)
{
for (int i = 0; i < assets.Count; i++)
{
JsonSerializer serializer = new JsonSerializer();
//ReviewsAsset review = (ReviewsAsset)serializer.Deserialize((Newtonsoft.Json.Linq.JObject)assets[i].Data, typeof(ReviewsAsset));
var json = assets[i].Data.ToString().Substring(0, assets[i].Data.ToString().Length);
string jsonCliente = JsonConvert.SerializeObject(assets[i].Data);
//var dictionary = serializer.Deserialize<Dictionary<string, object>>(assets[i].Data);
result += assets[i].Data.ToString() + "@|@";
}
}
else
{
result = "No Assets";
}
}
else
{
result = "No Assets";
}
}
catch (Exception e)
{
Console.WriteLine("Error: " + e.Message);
result = e.Message;
}
//sw.Stop();
return result;
}
/////////////////////////////////
Esto ya no me funciona, concretamente falla en:
if (!AsyncContext.Run(() => builder.setup()))
{
Console.WriteLine("Failed to setup");
};
En esta línea no la resuelve y salta por timeout devolviendo un null.
Agradezco cualquier pista o manual que leer. Muchas gracias !
(No se puede continuar esta discusión porque tiene más de dos meses de antigüedad. Si tienes dudas parecidas, abre un nuevo hilo.)