1. Definition Interface
1.1 iduplexcalculator operation interface
PublicInterfaceIcalculatorcallback
{
VoidShowresult (DoubleX,DoubleY,DoubleResult );
}
1.2 define the callback interface icalculatorcallback
PublicInterfaceIcalculatorcallback
{
VoidShowresult (DoubleX,DoubleY,DoubleResult );
}
2. Define server-side operations such as duplexcalculatorremoting, which must inherit albyrefobject,
Iduplexcalculator
Public ClassDuplexcalculatorremoting: marshalbyrefobject, iduplexcalculator
{
Public VoidAdd (DoubleX,DoubleY, icalculatorcallback callback)
{
//Console. writeline ("invoke the method add ({0}, {1}).", x, y );
DoubleResult = x + y;
Callback. showresult (X, Y, result );
}
}
3. Define the host
3.1 class Program
{
Static void main (string [] ARGs)
{
System. runtime. remoting. remotingconfiguration. Configure ("hosting.exe. config", false );
Console. writeline ("Calculator Service has begun to Listen ......");
Console. Read ();
}
}
Modify the remoting service.
3.2 Definition configuration file
View code
<Configuration>
<System. runtime. remoting>
<Application name =" Calculator " >
<Service>
<Wellknown mode = " Singlecall "
Type = " Duplexcalculatorremoting. duplexcalculatorremoting, duplexcalculatorremoting "
Objecturi = " Duplexcalculator. Soap " />
</Service>
<Channels>
<Channel Ref = " HTTP " Port = " 8080 " >
<Serverproviders>
<Provider Ref = " WSDL " />
<Formatter Ref = " Binary " Typefilterlevel = " Full " />
</Serverproviders>
<Clientproviders>
<Formatter Ref = " Binary " />
</Clientproviders>
</Channel>
</Channels>
</Application>
</System. runtime. remoting>
</Configuration>
<Application name = "Calculator"> objecturi = "duplexcalculator. Soap"/>
Http: // localhost: 8080/calculator/duplexcalculator. url used by the producer on the soap Client
Here you need to reference the interface type and operation type.
4. Guest terminal license
View code
Public Partial Class _ Default: system. Web. UI. Page
{
Calculatorcallbackhandler calcul = New Calculatorcallbackhandler ();
Protected Void Page_load ( Object Sender, eventargs E)
{
System. runtime. remoting. remotingconfiguration. Configure (server. mappath ( " Web. config " ), False );
Calcul. myevent + = New Eventhandler (calcul_myevent );
Invocateduplexcalculator ( " Http: // localhost: 8080/calculator/duplexcalculator. Soap " );
// Invocateduplexcalculator (" Http: // localhost/duplexcalculatorremoting/duplexcalculator. Soap ");
}
Void Calcul_myevent ( Object Sender, eventargs E)
{
Textbox1.text = (calculatorcallbackhandler) sender). value;
}
Private Void Invocateduplexcalculator ( String Remoteaddress)
{
Iduplexcalculator proxy = (iduplexcalculator) activator. GetObject ( Typeof (Iduplexcalculator), remoteaddress );
Proxy. Add ( 1.0 , 2.0 , Calcul );
}
}
The client needs to implement the interface of the response data. As follows:
View code
Public Class Calculatorcallbackhandler: marshalbyrefobject, icalculatorcallback
{
Public Event Eventhandler myevent;
# Region Icalculatorcallback members
Public Void Showresult ( Double X,Double Y, Double Result)
{
Value = String . Format ( " X + y = {2} where x = {0} and Y = {1} " , X, Y, result );
If (Myevent! = Null ){
Myevent ( This , Null );
}
}
# Endregion
Public String Value { Get ; Set ;}
}
In the same way, the client needs to configure
As follows:
View code
<System. runtime. remoting>
<Application>
<Channels>
<Channel Ref = " HTTP " Port = " 0 " >
<Clientproviders>
<Formatter Ref = " Binary " />
</Clientproviders>
<Serverproviders>
<Formatter Ref = " Binary " Typefilterlevel = " Full " />
</Serverproviders>
</Channel>
</Channels>
</Application>
</System. runtime. remoting>