最简单的一个WCF实现客户端和服务端
2011-03-23 13:51
549 查看
信不信由你,无需配置,两个console就好了
service side
1.定义ServiceContract:
2.new a ServiceHost
3. add endpoint
using System.ServiceModel;
namespace Service
{
class Program
{
static void Main(string[] args)
{
Uri address = new Uri("http://localhost:8000/myservice");
using (ServiceHost host = new ServiceHost(typeof(MyService), address))
{
host.AddServiceEndpoint(typeof(IMyService), new BasicHttpBinding(), address);
host.Open();
Console.ReadLine();
}
}
}
[ServiceContract]
public interface IMyService
{
[OperationContract]
string GetService();
}
public class MyService : IMyService
{
public string GetService()
{
return "Got Service!";
}
}
}
client side: get proxy, service 调用
using System.ServiceModel;
namespace Client
{
class Program
{
static void Main(string[] args)
{
IMyService proxy = ChannelFactory<IMyService>.CreateChannel(new BasicHttpBinding(),
new EndpointAddress("http://localhost:8000/myservice"));
Console.WriteLine(proxy.GetService());
Console.ReadLine();
}
}
[ServiceContract]
public interface IMyService
{
[OperationContract]
string GetService();
}
}
service side
1.定义ServiceContract:
2.new a ServiceHost
3. add endpoint
using System.ServiceModel;
namespace Service
{
class Program
{
static void Main(string[] args)
{
Uri address = new Uri("http://localhost:8000/myservice");
using (ServiceHost host = new ServiceHost(typeof(MyService), address))
{
host.AddServiceEndpoint(typeof(IMyService), new BasicHttpBinding(), address);
host.Open();
Console.ReadLine();
}
}
}
[ServiceContract]
public interface IMyService
{
[OperationContract]
string GetService();
}
public class MyService : IMyService
{
public string GetService()
{
return "Got Service!";
}
}
}
client side: get proxy, service 调用
using System.ServiceModel;
namespace Client
{
class Program
{
static void Main(string[] args)
{
IMyService proxy = ChannelFactory<IMyService>.CreateChannel(new BasicHttpBinding(),
new EndpointAddress("http://localhost:8000/myservice"));
Console.WriteLine(proxy.GetService());
Console.ReadLine();
}
}
[ServiceContract]
public interface IMyService
{
[OperationContract]
string GetService();
}
}
相关文章推荐
- golang使用net库实现一个简单的服务端和客户端通信
- 无配置WCF客户端的一个简单到令人发指的实现方法
- Go实战--实现一个简单的tcp服务端和客户端(The way to go)
- 01 TCP Socket 编程_一个简单的服务端程序和客户端程序实现
- 传智播客-一个服务端和多个客户端间的通信的简单实现
- 创建一个简单的restful wcf, 并且用silverlight做为客户端实现get, post, put,delete
- java中自己实现一个服务端对应多个客户端的简单代码
- WCF 第一章 基础 为一个ASMX服务实现一个WCF客户端
- 【socket编程】 一个简单的基于TCP连接的客户端、服务端用例
- 转:实现一个简单的服务端推送方案
- 基于TCP实现简单的客户端服务端的消息传递(二)
- 写一个Tomcat+Okhttp实现的聊天websocket聊天框架(一)-- 完成客户端和服务端的通信
- WCF基础 (续 为一个ASMX服务实现一个WCF客户端) 完结
- 随手写了一个简单的socket服务端和客户端
- 简单的socket连接,实现服务端可以获得多个客户端连接(备份,方便以后查询)
- 使用 flask 实现一个简单的可以返回 json 的 RESTful API 服务端
- 分享一个PHP写的简单webservice服务端+客户端
- WCF心跳判断服务端及客户端是否掉线并实现重连接
- java 实现简单http服务端,客户端DEMO
- 基于mina实现一个简单数据采集中间件的多客户端在线测试程序