1、定义服务
[ServiceContract]
public interface IServiceClass
{
[OperationContract]
string GetText();

[OperationContract]
int MultiplyNumbers(int firstvalue, int secondvalue);
}

public class ServiceClass : IServiceClass
{
string IServiceClass.GetText()
{
StreamReader sw = new StreamReader(@"c:\WCFServiceTest.txt");
return sw.ReadLine();
//return "Hello World";
}

int IServiceClass.MultiplyNumbers(int firstvalue, int secondvalue)
{
return firstvalue * secondvalue;
}

}2、托管
private void Form1_Load(object sender, EventArgs e)
{

Uri bpa = new Uri("net.pipe://localhost/NetNamedPipeBinding");
Uri tcpa = new Uri("net.tcp://localhost:8000/TcpBinding");

sh = new ServiceHost(typeof(ServiceClass), bpa, tcpa);
//sh = new ServiceHost(typeof(ServiceClass), tcpa);

NetNamedPipeBinding pb = new NetNamedPipeBinding();
NetTcpBinding tcpb = new NetTcpBinding();

ServiceMetadataBehavior mBehave = new ServiceMetadataBehavior();
sh.Description.Behaviors.Add(mBehave);
sh.AddServiceEndpoint(typeof(IMetadataExchange),
MetadataExchangeBindings.CreateMexTcpBinding(), "mex");

sh.AddServiceEndpoint(typeof(IMetadataExchange),
MetadataExchangeBindings.CreateMexNamedPipeBinding(), "mex");

sh.AddServiceEndpoint(typeof(IServiceClass), pb, bpa);
sh.AddServiceEndpoint(typeof(IServiceClass), tcpb, tcpa);

sh.Open();

label1.Text = "Service Running";
}3、客户端调用
private void button2_Click(object sender, EventArgs e)
{
switch (_Selection)
{
case 0:
TCP.ServiceClassClient client = new
WCFClientApp.TCP.ServiceClassClient("NetTcpBinding_IServiceClass");
textBox2.Text = client.MultiplyNumbers(5, 5).ToString();
break;

case 1:
NamedPipe.ServiceClassClient client1 = new
WCFClientApp.NamedPipe.ServiceClassClient("NetNamedPipeBinding_IServiceClass");
textBox2.Text = client1.MultiplyNumbers(5, 5).ToString();
break;

case 2:
break;
}
}