segunda-feira, 17 de janeiro de 2011

Memory Stream

Bom para resolver o problema do Memory Stream, acabamos fazendo pesquisas menos na base de dados, e retornando os dados em partes. Assim eram serializados dados menores.
No cliente juntavamos todo os dados e exibíamos na tela.

Assim para trafegar 1gb de dados, ao inves de aumentar o pacote SOAP, quebramos em pacotes menores de 50 MB, juntando os pedaçinhos no clientes e exibindo.

Agora no nosso problema de memoria ficou na maquina do usuário, que insistia em trabalhar com 60.000 mil registros sem paginar.

Bom, vamos tentar agora propor uma mudança na interface e trazer sobre demanda.

Aplicações velhas e usuários difícil são assim mesmo.

Missão cumprida por hora...

Abaixo o codigo da remocao do Memory Stream, não resolvi o problema de deletar os lixos das stream...

////////////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Web.Services;
using System.Xml;
using ICSharpCode.SharpZipLib;
using System.Web.Services.Protocols;
using ICSharpCode.SharpZipLib.Zip;

namespace Lapuinka.WebServices
{
[AttributeUsage(AttributeTargets.Method)]
public class CompressionExtensionAttribute : SoapExtensionAttribute
{
// Fields
private int priority;

// Properties
public override Type ExtensionType
{
get
{
return typeof(CompressionExtension);
}
}

public override int Priority
{
get
{
return this.priority;
}
set
{
this.priority = value;
}
}
}



public class CompressionExtension : SoapExtension
{
private Stream newdiskStream;
private Stream entradaDiskStream;
private Stream saidaDiskStream;

private Stream oldStream;
private Stream newStream;



public string saidaFile = string.Empty;
public string entradaFile = string.Empty;

public string currentFile = string.Empty;

// Methods
public override Stream ChainStream(Stream stream)
{
this.oldStream = stream;
this.newStream = new FileStream(setFileName(@"c:\soap\EntradaHttpInputStream"), FileMode.Create);
return this.newStream;

}

private void Copy(Stream from, Stream to)
{
StreamReader reader = new StreamReader(from);
StreamWriter writer = new StreamWriter(to);
if (to.CanSeek)
to.Position = 0;
if (from.CanSeek)
from.Position = 0;
writer.Write(reader.ReadToEnd());
writer.Flush();
if (to.CanSeek)
to.Position = 0;
if (from.CanSeek)
from.Position = 0;

}


public override void ProcessMessage(SoapMessage message)
{
switch (message.Stage)
{
case SoapMessageStage.BeforeSerialize:
case SoapMessageStage.AfterDeserialize:
return;

case SoapMessageStage.AfterSerialize:
TratamentoSaida(message);
return;

case SoapMessageStage.BeforeDeserialize:
TratamentoEntrada(message);
return;
}
throw new Exception("invalid stage");
}

public void TratamentoEntrada(SoapMessage message)
{
this.Copy(this.oldStream, this.newStream);
this.newStream.Position = 0L;
}

public void TratamentoSaida(SoapMessage message)
{
this.newStream.Position = 0L;
this.Copy(this.newStream, this.oldStream);
this.newStream.Position = 0L;
}


public override object GetInitializer(Type t)
{
return typeof(CompressionExtension);
}

public override object GetInitializer(LogicalMethodInfo methodInfo, SoapExtensionAttribute attribute)
{
return attribute;
}

public override void Initialize(object initializer)
{
CompressionExtensionAttribute attribute = (CompressionExtensionAttribute)initializer;
}

private string setFileName(string preFixo)
{
string action = string.Empty;
int startIndex = action.LastIndexOf("/") + 1;
action = action.Substring(startIndex, action.Length - startIndex).Replace(":", "_").Replace("/", "_").Replace("?", "_").Replace(" ", "_");
return (preFixo + "_" + action + "_" + Guid.NewGuid().ToString() + ".xml");
}

}



}
////////////////////////////////////////////////////////////////////////////

Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.ComponentModel
Imports Lapuinka.WebServices
Imports System.Data
Imports System.Data.SqlClient

Imports Microsoft.Practices.EnterpriseLibrary.Data
Imports Microsoft.Practices.EnterpriseLibrary.Data.Sql
Imports System.IO

_
_
_
Public Class teste
Inherits System.Web.Services.WebService

_
Public Function HelloWorld() As DataSet
Dim logdb As Database = DatabaseFactory.CreateDatabase
Dim sqlCommand As String = "sproc_DPP_StressTest"
Dim dbCommandWrapper As DBCommandWrapper = logdb.GetStoredProcCommandWrapper(sqlCommand)
Dim feedDB As Database = DatabaseFactory.CreateDatabase

Dim ds As DataSet = feedDB.ExecuteDataSet(dbCommandWrapper)
If (ds.Tables(0).Rows.Count > 0) Then
Return ds
End If
HelloWorld = ds
End Function
_
Public Function SemCompress() As DataSet
Dim mem As MemoryStream = New MemoryStream(252661760 + 50)
mem.Dispose()
Dim logdb As Database = DatabaseFactory.CreateDatabase
Dim sqlCommand As String = "sproc_DPP_StressTest"
Dim dbCommandWrapper As DBCommandWrapper = logdb.GetStoredProcCommandWrapper(sqlCommand)
Dim feedDB As Database = DatabaseFactory.CreateDatabase

Dim ds As DataSet = feedDB.ExecuteDataSet(dbCommandWrapper)
If (ds.Tables(0).Rows.Count > 0) Then
Return ds
End If
SemCompress = ds
End Function

End Class

///////////////////////

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace LapuinkaConsole
{
class Program
{
static void Main(string[] args)
{
ServiceReference1.testeSoapClient t = new LapuinkaConsole.ServiceReference1.testeSoapClient();
Console.WriteLine("Start");
Console.ReadLine();
DataSet ds = t.HelloWorld();
if (ds.Tables[0].Rows.Count > 0)
{
Console.WriteLine("Compression OK " + ds.Tables[0].Rows.Count.ToString());

}
Console.ReadLine();
}
}
}
Postar um comentário