Regarding the problem when the web service written in C# is called under Delphi
First of all, I think it is necessary to give a relatively comprehensive explanation of the development of webservices under the .net platform. The initial generation of webservices appeared in the e-commerce environment. Some problems are no longer easy to solve through traditional means, such as in applications. For business communication and data exchange, it is no longer feasible to use binary mechanisms such as dcom and corba. Not to mention that they are limited by their respective platforms. For example, dcom (distribute common objects model) can only be performed under the windows platform, while corba (common objects request broker architecture) is only a product of the java platform. It is difficult for them to achieve data communication between applications.
In this context, web services emerged as the times require. It is a good solution to cross-language, cross-platform, and good and safe penetration of corporate firewalls. Its architecture is generally divided into five levels:
Data communication under http (hypertext transfer protocol) channel
xml (extensable markup language) data expression form
Data encapsulation of soap (simple objects access protocol)
Description language format of wsdl (webservices description language)
uddi (universal description and discovery integration) unified description, discovery and integration
Being cross-language, cross-platform, and good and secure at penetrating corporate firewalls is enough to get us excited. However, there are also areas that need improvement, such as:
HTTP data communication is slow, especially the first connection.
Requires the additional overhead of a web server
After talking about the architecture of webservices and its advantages and disadvantages, let’s get to the point, which is the topic of our discussion this time:
Problems when the web service service is called under Delphi
Without further ado, let’s illustrate with a simple example of testing the attribute of webmethod: it is a simple business process of completing a transfer from customer a to customer b.
First we create a webservice under .net framework1.1. At this time, .net will automatically create a web application for us.
We need to create a class attributetest, which is under the namespace attributetesting. Then this class will automatically inherit its base class system.web.services.webservice. When needed, we can set the web application class Its webserviceattribute, the basic attributes include description and namespace
[webserviceattribute(namespace=http://www.isdoo.com/services,
description=hello ansel,this is a testing web service!)]
public class attributetest : system.web.services.webservice
{
[webmethodattribute(description=Description information: Inherits the count method and overloads the name. The execution is to transfer money from user a to user b...,
//messagename=changing messagename,
bufferresponse=true,
cacheduration=1000,
enablesession=true,
transactionoption=transactionoption.requiresnew)]
public string transmoney(double money)
{
try
{
contextutil.enablecommit();//transaction only used to database operation!
moneyintoa(money);
moneyoutfromb(money);
contextutil.setcomplete();
return transaction successful,total +money.tostring();
}
catch(exception e)
{
contextutil.setabort();
return transaction failed! /n/r +e.message;
}
}
private void moneyintoa(double money)
{
sqlcommand sqlcom = new sqlcommand(update budget set money=money++money.tostring()+ where name='a');
databaseaccess mydatabase = new databaseaccess();
sqlcom.connection=mydatabase.getconnection();
sqlcom.connection.open();
sqlcom.executenonquery();
sqlcom.connection.close();
//throw new exception(operation failed when transfer money into a!);
}
private void moneyoutfromb(double money)
{
sqlcommand sqlcom = new sqlcommand(update budget set money=money-+money.tostring()+ where name='b');
databaseaccess mydatabase = new databaseaccess();
sqlcom.connection=mydatabase.getconnection();
sqlcom.connection.open();
sqlcom.executenonquery();
sqlcom.connection.close();
//throw new exception(operation failed when transfer money from b!);
}
}
What we need to pay special attention to is webmethodattribute, which is also the focus of what we need to talk about this time. As long as you add [webmethodattribute] to a method, even if there are no attributes in it, then the webservice will expose this method to Client segment caller. Let’s talk about its 6 attributes below, including 2 descriptive information attributes and 4 functional attributes.
Descriptive information attributes:
description
messagename
4 functional properties:
bufferresponse
cacheduration
enablesession
transactionoption
Among them, we need to pay attention to the messagename attribute. When Delphi calls the webservice developed under the .net platform, if you set the messagename attribute, an error will be reported when the client calls it. This may be a bug, maybe a later version will solve this problem. As for the specific functions of these attributes, they will not be described in detail here. You can read relevant books.
Below I will list the steps for Delphi to call webservice:
First of all, if you just develop and call the client, then you only need to create an ordinary application. Then what you need to do is to find the soaphttpclient control under the toolbar webservices, and then put it in your client application window body;
Secondly, you need to set the attribute url or wsdl of this soaphttpclient. This content is the service address of your webservice.
For example, the service address of our current example is: http://localhost/attributetesting/attributetesting.asmx
If you want to enter wsdl then it is http://localhost/attributetesting/attributetesting.asmx?wsdl
This completes the control settings;
Then we need to import the server-side wsdl. You can do it manually or use the webservices importer function provided by Delphi.
Finally, you only need to call the imported wsdl interface. Our corresponding code here is:
procedure tform1.bitbtn1click(sender: tobject);
var
aa:attributetestsoap;//This is the class interface object under wsdl
msg:widestring;
bb:double;
begin
//httprio2 is actually the so-called proxy class, which is responsible for data transmission, sending requests and accepting responses.
aa:=httprio2 as attributetestsoap;
bb:=100.00;
msg:=aa.transmoney(bb);//This is the web service method webmethod exposed to us by calling the web service
showmessage(msg);
end;
ok! that's all!
Popular downloads of commercial source code www.html.org.cn