WCF> a runtime environment, a service logic person, and a customer

Source: Internet
Author: User

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.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.