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;
}
}
WCF 服务、托管、客户端 简单案例
原创
©著作权归作者所有:来自51CTO博客作者wx636b6259489d3的原创作品,请联系作者获取转载授权,否则将追究法律责任
上一篇:目标必须要符合SMART原则
下一篇:执行能力
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
WCF初探-12:WCF客户端异常处理
前言: 当我们打开WCF基础客户端通道(无论是通过显式打开还是通过调用操 提供了一个错误消息处理的类FaultExcepti
WCF 客户端 ide 异常信息 应用程序