using System.ServiceModel;
namespace Host using System; namespace Host namespace Host namespace Client
{
[ServiceContract]
public interface IHelloWCFService
{
[OperationContract]
string HelloWCF(string message);
}
}
而HelloWCFService.cs代码实现如下
{
public class HelloWCFService : IHelloWCFService
{
public string HelloWCF(string message)
{
return string.Format("你在{0}收到信息:{1}",DateTime.Now,message);
}
}
}
App.config文件原则上可以不用改,但是address太长了
(默认的为baseAddress=http://localhost:8731/Design_Time_Addresses/Host/HelloWCFService/)
缩短为baseAddress=http://localhost:8731/HelloWCFService/
并修改Program.cs文件为
using System;
using System.ServiceModel;
{
class Program
{
static void Main(string[] args)
{
using(ServiceHost host=new ServiceHost(typeof(Host.HelloWCFService)))
{
host.Open();
Console.ReadLine();
host.Close();
}
}
}
}
编译并生成Host.exe文件
接下来创建客户端程序为Console Application项目Client
启动Host.exe文件
右键点击Client项目并选择Add Service Reference...
并且在Address的TextBox里面输入服务器的地址(就是咱们前面设置的baseaddress地址),并点击Go
将得到目标服务器上面的Services,如下图所示
这一步见在客户端间接借助SvcUtil.exe文件创建客户端代理(using Client.HelloWCF;)以及配置文件app.config
修改客户端的程序如下
using System;
using Client.HelloWCF;
{
class Program
{
static void Main(string[] args)
{
HelloWCF.HelloWCFServiceClient proxy=new HelloWCFServiceClient();
string str = proxy.HelloWCF("欢迎来到WCF村!");
Console.WriteLine(str);
Console.ReadLine();
}
}
}
就可以获取得到服务器的对象了
联系客服