http://msdn.microsoft.com/zh-cn/library/ms734712.aspx
照着说明测试了一下:
一、创建解决方案WCFtest
二、创建服务端
1、在WCFtest下添加一个“控制台应用程序”,命名为
Service
2、修改默认的命名空间为
Microsoft.ServiceModel.Samples
3、添加引用System.ServiceModel.dll
4、编写Program.cs代码:


using
System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Description;
namespace Microsoft.ServiceModel.Samples
{
[ServiceContract(Namespace = " http://Microsoft.ServiceModel.Samples " )]
public interface ICalculator
{
[OperationContract]
double Add( double n1, double n2);
[OperationContract]
double Subtract( double n1, double n2);
[OperationContract]
double Multiply( double n1, double n2);
[OperationContract]
double Divide( double n1, double n2);
}
public class CalculatorService : ICalculator
{
public double Add( double n1, double n2)
{
double result = n1 + n2;
Console.WriteLine( " Received Add({0},{1}) " , n1, n2);
// Code added to write output to the console window.
Console.WriteLine( " Return: {0} " , result);
return result;
}
public double Subtract( double n1, double n2)
{
double result = n1 - n2;
Console.WriteLine( " Received Subtract({0},{1}) " , n1, n2);
Console.WriteLine( " Return: {0} " , result);
return result;
}
public double Multiply( double n1, double n2)
{
double result = n1 * n2;
Console.WriteLine( " Received Multiply({0},{1}) " , n1, n2);
Console.WriteLine( " Return: {0} " , result);
return result;
}
public double Divide( double n1, double n2)
{
double result = n1 / n2;
Console.WriteLine( " Received Divide({0},{1}) " , n1, n2);
Console.WriteLine( " Return: {0} " , result);
return result;
}
}
class Program
{
static void Main( string [] args)
{
// Step 1 of the address configuration procedure: Create a URI to serve as the base address.
Uri baseAddress = new Uri( " http://localhost:8000/ServiceModelSamples/Service " );
// Step 2 of the hosting procedure: Create ServiceHost
ServiceHost selfHost = new ServiceHost( typeof (CalculatorService), baseAddress);
try
{
// Step 3 of the hosting procedure: Add a service endpoint.
selfHost.AddServiceEndpoint(
typeof (ICalculator),
new WSHttpBinding(),
" CalculatorService " );
// Step 4 of the hosting procedure: Enable metadata exchange.
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true ;
selfHost.Description.Behaviors.Add(smb);
// Step 5 of the hosting procedure: Start (and then stop) the service.
selfHost.Open();
Console.WriteLine( " The service is ready. " );
Console.WriteLine( " Press <ENTER> to terminate service. " );
Console.WriteLine();
Console.ReadLine();
// Close the ServiceHostBase to shutdown the service.
selfHost.Close();
}
catch (CommunicationException ce)
{
Console.WriteLine( " An exception occurred: {0} " , ce.Message);
selfHost.Abort();
}
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Description;
namespace Microsoft.ServiceModel.Samples
{
[ServiceContract(Namespace = " http://Microsoft.ServiceModel.Samples " )]
public interface ICalculator
{
[OperationContract]
double Add( double n1, double n2);
[OperationContract]
double Subtract( double n1, double n2);
[OperationContract]
double Multiply( double n1, double n2);
[OperationContract]
double Divide( double n1, double n2);
}
public class CalculatorService : ICalculator
{
public double Add( double n1, double n2)
{
double result = n1 + n2;
Console.WriteLine( " Received Add({0},{1}) " , n1, n2);
// Code added to write output to the console window.
Console.WriteLine( " Return: {0} " , result);
return result;
}
public double Subtract( double n1, double n2)
{
double result = n1 - n2;
Console.WriteLine( " Received Subtract({0},{1}) " , n1, n2);
Console.WriteLine( " Return: {0} " , result);
return result;
}
public double Multiply( double n1, double n2)
{
double result = n1 * n2;
Console.WriteLine( " Received Multiply({0},{1}) " , n1, n2);
Console.WriteLine( " Return: {0} " , result);
return result;
}
public double Divide( double n1, double n2)
{
double result = n1 / n2;
Console.WriteLine( " Received Divide({0},{1}) " , n1, n2);
Console.WriteLine( " Return: {0} " , result);
return result;
}
}
class Program
{
static void Main( string [] args)
{
// Step 1 of the address configuration procedure: Create a URI to serve as the base address.
Uri baseAddress = new Uri( " http://localhost:8000/ServiceModelSamples/Service " );
// Step 2 of the hosting procedure: Create ServiceHost
ServiceHost selfHost = new ServiceHost( typeof (CalculatorService), baseAddress);
try
{
// Step 3 of the hosting procedure: Add a service endpoint.
selfHost.AddServiceEndpoint(
typeof (ICalculator),
new WSHttpBinding(),
" CalculatorService " );
// Step 4 of the hosting procedure: Enable metadata exchange.
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true ;
selfHost.Description.Behaviors.Add(smb);
// Step 5 of the hosting procedure: Start (and then stop) the service.
selfHost.Open();
Console.WriteLine( " The service is ready. " );
Console.WriteLine( " Press <ENTER> to terminate service. " );
Console.WriteLine();
Console.ReadLine();
// Close the ServiceHostBase to shutdown the service.
selfHost.Close();
}
catch (CommunicationException ce)
{
Console.WriteLine( " An exception occurred: {0} " , ce.Message);
selfHost.Abort();
}
}
}
}
验证服务是否正常运行:
1、按F5启动或者在项目目录下的bin\Debug目录下打开service.exe
2、打开浏览器并输入 http://localhost:8000/ServiceModelSamples/Service
三、创建客户端
1、在WCFtest下添加一个“控制台应用程序”,命名为
Client
2、
添加引用System.ServiceModel.dll
3、打开“Visual Studio 2010 命令提示”,运行ServiceModel 元数据实用工具 (Svcutil.exe) ,在项目Client目录下创建客户端代码和配置文件:
C:\Documents and Settings\lc\My Documents\Visual Studio 2010\Projects\WCFte
st\Client>svcutil.exe /language:cs /out:generatedProxy.cs /config:app.config htt
p://localhost:8000/ServiceModelSamples/service
st\Client>svcutil.exe /language:cs /out:generatedProxy.cs /config:app.config htt
p://localhost:8000/ServiceModelSamples/service
4、在解决方案资源管理器中点击“显示所有文件”,把隐藏的app.config、generatedProxy.cs右键包括在项目Client中
5、编写Program.cs代码:


using
System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Description;
namespace Client
{
class Program
{
static void Main( string [] args)
{
// Step 1: Create an endpoint address and an instance of the WCF Client.
CalculatorClient client = new CalculatorClient();
// Step 2: Call the service operations.
// Call the Add service operation.
double value1 = 100.00D ;
double value2 = 15.99D ;
double result = client.Add(value1, value2);
Console.WriteLine( " Add({0},{1}) = {2} " , value1, value2, result);
// Call the Subtract service operation.
value1 = 145.00D ;
value2 = 76.54D ;
result = client.Subtract(value1, value2);
Console.WriteLine( " Subtract({0},{1}) = {2} " , value1, value2, result);
// Call the Multiply service operation.
value1 = 9.00D ;
value2 = 81.25D ;
result = client.Multiply(value1, value2);
Console.WriteLine( " Multiply({0},{1}) = {2} " , value1, value2, result);
// Call the Divide service operation.
value1 = 22.00D ;
value2 = 7.00D ;
result = client.Divide(value1, value2);
Console.WriteLine( " Divide({0},{1}) = {2} " , value1, value2, result);
// Step 3: Closing the client gracefully closes the connection and cleans up resources.
client.Close();
Console.WriteLine();
Console.WriteLine( " Press <ENTER> to terminate client. " );
Console.ReadLine();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Description;
namespace Client
{
class Program
{
static void Main( string [] args)
{
// Step 1: Create an endpoint address and an instance of the WCF Client.
CalculatorClient client = new CalculatorClient();
// Step 2: Call the service operations.
// Call the Add service operation.
double value1 = 100.00D ;
double value2 = 15.99D ;
double result = client.Add(value1, value2);
Console.WriteLine( " Add({0},{1}) = {2} " , value1, value2, result);
// Call the Subtract service operation.
value1 = 145.00D ;
value2 = 76.54D ;
result = client.Subtract(value1, value2);
Console.WriteLine( " Subtract({0},{1}) = {2} " , value1, value2, result);
// Call the Multiply service operation.
value1 = 9.00D ;
value2 = 81.25D ;
result = client.Multiply(value1, value2);
Console.WriteLine( " Multiply({0},{1}) = {2} " , value1, value2, result);
// Call the Divide service operation.
value1 = 22.00D ;
value2 = 7.00D ;
result = client.Divide(value1, value2);
Console.WriteLine( " Divide({0},{1}) = {2} " , value1, value2, result);
// Step 3: Closing the client gracefully closes the connection and cleans up resources.
client.Close();
Console.WriteLine();
Console.WriteLine( " Press <ENTER> to terminate client. " );
Console.ReadLine();
}
}
}
最后,先打开Server\bin\Debug下的Service.exe运行服务端,再打开Client\bin\Debug下的Client.exe运行客户端。
显示如下:
