The process in this example is: The Service Logic person has the ability to establish a connection with the service contact person. The customer specifies the Service Logic person, assigns the customer contact person to contact the service contact person, and uses the skill of the Service Logic person.
First teach Service Logic people-oriented. Note: first create a class library and then create a class in the class library.
Service Logic person Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using system. servicemodel; // Add reference system. servicemodel to the class library
Namespace helloindigo // Service Logic person
{
[Servicecontract (namespace = "http://www.thatindigogirl.com/samples/2006/06")] // This capability is called by the customer by the Service Logic person (term: Service Contract)
Public interface iheloindigoservice
{
[Operationcontract] // specifies the Service Logic person. The specific behavior of this skill can be called by the customer (term: operation contract)
String helloindigo ();
}
Public class helloindigoservice: iheloindigoservice // logic personnel skill
{
Public String helloindigo () // The specific behavior of this skill
{
Return "Hello indigo ";
}
}
}
Set up the runtime environment, which includes the service contact person. This is a console application.
Running Environment Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using system. servicemodel; // Add reference>. Net> system. servicemodel
// Add reference> Project> logic person helloindigo
Namespace host // Runtime Environment
{
Class Program
{
Static void main (string [] ARGs)
{
Using (servicehost host = new servicehost (typeof (helloindigo. helloindigoservice), new uri ("net. TCP: // localhost: 8000/helloindigo ") // net. TCP, logical person address localhost: 8000/helloindigo
{
Host. addserviceendpoint (typeof (helloindigo. iheloindigoservice), new nettcpbinding (), "helloindigoservice"); // The contact person joins the runtime environment to specify the logic person's skills
Host. open (); // generate the runtime environment
Console. writeline ("press enter to close the running environment (host )");
Console. Readline ();
}
}
}
}
The customer spoke. This is a console application.
Customer Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using system. servicemodel; // Add reference system. servicemodel
Namespace client // customer
{
[Servicecontract (namespace = "http://www.thatindigogirl.com/samples/2006/06")]
Public interface iheloindigoservice // specify the logic person of the customer
{
[Operationcontract] // The helloindigo () capability of the customer who calls the Service Logic
String helloindigo ();
}
Class Program
{
Static void main (string [] ARGs)
{
Console. writeline ("call the service by 1 and exit by 0 ");
String n = console. Readline ();
If (n = "1 ")
{
// The customer assigns a contact person to contact the service contact person and agrees with the customer's transport, address, and logic personnel.
Endpointaddress Ep = new endpointaddress ("net. TCP: // localhost: 8000/helloindigo/helloindigoservice ");
// Obtain the data obtained by the contact
Iheloindigoservice proxy = channelfactory <iheloindigoservice>. createchannel (New nettcpbinding (), EP );
// Use the data obtained by the contact person
String S = proxy. helloindigo ();
Console. writeline (s );
Console. Readline ();
}
Else if (n = "0 ")
{
}
}
}
}
After compilation, start the motion environment and then the customer.