Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How I can use a TClientDataset (of VCL.NET) in a ASP.NET WebService.

Posted on 2006-07-14
9
Medium Priority
?
1,671 Views
Last Modified: 2008-01-09
Hi Experts,

After days examining different ways to consume a ADO.NET dataset sent
by a webservice method, I found that article of DrBob (http://www.drbob42.com/examines/examin62.htm)
which gave the idea to use the ADONETConnector to take the data to ClientDataset and save it to a
XML string.
The code for the function is below:
 
function ObterXMLDataPacket(
  pADONET_Dataset: DataSet; pNumero_DataTable: Integer): String;
var Conector:TADONETConnector;
    Cds:TclientDataset;
    DSP:TDatasetProvider;
    St: TStringStream;
begin
  If pADONET_Dataset.Tables.Count = 0 then Exit;
  (*Cria os componentes necessários*)
  Cds := TClientDataSet.Create(nil);
  DSP := TDataSetProvider.Create(nil);
  St := TStringStream.Create;
  Conector := TADONETConnector.Create(nil);
  (*Conecta os componentes*)
  DSP.Dataset := Conector;
  Cds.SetProvider(DSP);
  Conector.DataTable := pADONET_Dataset.Tables[pNumero_DataTable];
  (*Obtém os dados*)
  Conector.Open;


  CDS.Open;  // Here the method throws the exception


  (*Transforma os dados em XML Datapacket*)
  Cds.SaveToStream(St,dfXML);
  Result := ST.DataString;
  (*Limpa o ambientes*)
  St.Free;
  Cds.Free;
  Conector.Free;
  DSP.Free;
end;

The function is used in a WebMethod which opens a BDPDataAdapter and calls the function with the Dataset
populated by the Adapter.

Here is the code:

function TCC_DataLayer.RetornaDatasetXMLDPacket: String;
begin
  BdpDataAdapter1.Active := True;
  Result := ObterXMLDataPacket(ds1,0);
end;

With the needed units (ADONetDb, System.Data, etc among a few other ones) - the WebService build without problems. But when the method is called, the execution is aborted with a exception (on the Win32 client, on the browser gave only a internal server error).

Here is the message of the Exception: "Servidor não pôde processar a solicitação. --> Invalid threading model (STAThreadAttribute is required)"

The first part is in Brazilian Portuguese and means: "Server could not process the request"

Here is the code where the WebMethod is called:
 
procedure TForm2.Button2Click(Sender: TObject);
var Comm:THTTPRIO;
begin
  Comm := THTTPRIO.Create(nil);
  WServico := GetTCC_DataLayerSoap(False,'http://localhost/CredCob_DataLayer/uCC_DataLayer.asmx',Comm);
  try
    ClientDataSet1.XMLData := WServico.RetornaDatasetXMLDPacket;
    ClientDataSet1.Open;
  except
  on E: Exception do
    begin
    With TStringList.Create do
      begin
      Add(E.Message);
      SaveToFile('c:\mensagemerro.txt');
      Free;
      end;
    end;
  end;
end;

 
Environment: Delphi 2006 Architect (Borland® Developer Studio for Microsoft® Windows™ Version 10.0.2288.42451 Update 2), IIS + ASP.NET 1.1.4322...
DataBase: MSSQL 2005 (just for the sake of completeness).

Best Regards and thank you very much for the attention,
Fabricio (Delphian)
0
Comment
Question by:Delphian
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
9 Comments
 
LVL 12

Expert Comment

by:Ivanov_G
ID: 17114029
Have you tried this:

[STAThreadAttribute]
function TCC_DataLayer.RetornaDatasetXMLDPacket: String;
....
0
 
LVL 4

Author Comment

by:Delphian
ID: 17121919
You mean apply the attibute to the WebMethod? Does not work.
And even putting in  both (internal function and WebMethod), or
in one of them - does not work, the exception is thrown wherever
the attribute is located...

Any ideas?
0
 
LVL 12

Expert Comment

by:Ivanov_G
ID: 17122930
[STAThread]
static void Main()
{
}

This way you change the Threading Model to Single Threaded.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 4

Author Comment

by:Delphian
ID: 17123337
No luck on this also...
0
 
LVL 12

Expert Comment

by:Ivanov_G
ID: 17123779
No more ideas on my side... Sorry.
0
 
LVL 4

Author Comment

by:Delphian
ID: 17124499
I did some additional testing, the results are:

-> In a desktop Windows Forms: OK (no changes, no additional attributes needed)
-> In a desktop VCL.NET: OK (ditto above)

The great trouble is when ASP.NET Web Services come in scene. Seems a agreement
that someone needs to get a STAThread attrib... But is not the likely ones.

So, the 500 points question: WHERE I put the this 'lovely' attribute? Or is not an attribute
question?

Any comments?

(BTW: Thank you for the good will, Ivanov)
0
 
LVL 4

Author Comment

by:Delphian
ID: 17964054
I discovered later that it's reported on Borland Quality Central at
http://qc.borland.com/wc/qcmain.aspx?d=33679

And after that I forgot the question. It's a real shame.

Since I changed the implementation, there's no use for me the
workaround on the link, but I ask you just to mantain the question
on the DB - it can help others.

But I would like the refund.

0
 
LVL 1

Accepted Solution

by:
Computer101 earned 0 total points
ID: 17983746
PAQed with points refunded (500)

Computer101
EE Admin
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question