Create a simple WCF Service)

Source: Internet
Author: User
1: Basic knowledge:

Like the traditional distributed communication framework, WCF essentially provides a cross-process, cross-machine, and cross-network service call. It mainly includes contract, binding, and address ).

The WCF Service cannot exist in isolation and must be hosted in a running process. We call the process that carries the WCF Service as the host, the process of specifying a host for a service is called service hosting ). In our computing service applications, two service hosting methods are adopted:Self-hostingCreate a host (hosting.exe boarding process) for the application as a Service. Use IIS to host the service in IIS (w3wp.exe for the job where the boarding process is IIS ). The customer uses another console application simulation (the process is client.exe ). The following describes how to create a service host through self-hosting.

2: create the first standard WCFProgram.

First, create an empty solution: wcfcalculator

Right-click solution, select Add-> new project, select console application template, and select project name host (server side)

Similarly, right-click solution, select Add-> new project, select console application template, and select project name client)

Right-click the project host and add the WCF Service named calculator.

R

After being added, generate three files: calculator. CS, icalculator. CS, and App. config.

Write a service contract in icalculator. CS:

Namespace Host
{
// Note: If you change the Interface Name "icalculator" here, you must also update the reference to "icalculator" in APP. config.
[Servicecontract]
Public   Interface Icalculator
{
[Operationcontract]
Double Add ( Double X, Double Y ); // Indicates externally accessible services
}
}

Then calculator. CS:

  Namespace  Host
{
// Note: If you change the class name "Calculator" here, you must also update the reference to "Calculator" in APP. config.
Public   Class Calculator: icalculator
{
Public   Double Add ( Double X, Double Y)
{
Return X + Y;
}
}
}
 
In app. config, change the default baseaddress to a briefBaseaddress = "http: // localhost: 8732/calculator/"
 
ThenBoarding services through self-boarding
Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. text;
Using System. servicemodel;

Namespace Host
{
Class Program
{
Static   Void Main ( String [] ARGs)
{
Using (Servicehost host =   New Servicehost ( Typeof (Host. calculator )))
{
Host. open ();
Console. Write ( " Service started " );
Console. readkey ();
Host. Close ();
}
}
}

 

Compile and generate the host.exe file. In this way, the service is successfully hosted on the host.
Start the host.exe file in the wcfcalculator \ host \ bin \ debugdirectory.[Host. EXE cannot be closed during service refenrence addition]
In the created client project, right-click the client project and choose add service reference...
Enter the server address http: // localhost: 8732/calculator/In the textbox of address, and click go to create a calculatorwcf service reference;

 In this way, you can access the WCF Service on the client.

Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. text;

Namespace Client
{
Class Program
{
Static   Void Main ( String [] ARGs)
{
Calculatorwcf. calculatorclient Client =   New Client. calculatorwcf. calculatorclient ();
Double Result = Client. Add ( 1.0 , 3.0 );

Console. writeline ( " The result after calling the WCF Service is: " + Result. tostring ());
Console. readkey ();
}
}
}

In this way, the client can access the services on the server. Running result:

 

From: http://www.cnblogs.com/ttltry-air/archive/2010/09/03/1817327.html

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.