代码创建
Service
接口
创建服务组件之前,需要先引用System.ServiceModel,下面的所有代码都需要引用。
[ServiceContract]
public interface IHelloService
{
/// <summary>
/// 服务操作
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
[OperationContract]//仅这样的标识才能将方法放入service
string SayHello(string name);
}
实现类
public class HelloService : IHelloService
{
/// <summary>
/// 打招呼
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public string SayHello(string name)
{
return name+"说:您好!";
}
}
ServiceHost配置
class Program
{
static void Main(string[] args)
{
using (MyHelloHost Host = new MyHelloHost())
{
Host.Open();
Console.Read();
}
}
}
public class MyHelloHost : IDisposable
{
/// <summary>
/// 定义一个服务对象
/// </summary>
private ServiceHost _myhost;
/// <summary>
/// 定义一个基地址
/// </summary>
public const string BaseAddress = "net.pipe://localhost";
/// <summary>
/// 可选地址
/// </summary>
public const string HelloServiceAddress = "Hello";
//服务契约实现类型
public static readonly Type ServiceType = typeof(HelloService.HelloService);//需要先添加引用
//服务契约接口
public static readonly Type ContractType = typeof(HelloService.IHelloService);
/// <summary>
/// 服务定义一个绑定
/// </summary>
public static readonly Binding hellobinding = new NetNamedPipeBinding();
/// <summary>
/// 构造服务对象
/// </summary>
protected void CreateHelloServiceHost()
{
//创建服务对象
_myhost = new ServiceHost(ServiceType, new Uri[] { new Uri(BaseAddress)});
//添加终节点
_myhost.AddServiceEndpoint(ContractType, hellobinding, HelloServiceAddress);
}
//封装服务对象
public ServiceHost Myhost
{
get { return _myhost; }
}
/// <summary>
/// 打开服务方法
/// </summary>
public void Open()
{
Console.WriteLine("开始启动服务……");
_myhost.Open();
Console.WriteLine("服务已经启动……");
}
/// <summary>
/// 构造方法
/// </summary>
public MyHelloHost()
{
CreateHelloServiceHost();
}
/// <summary>
/// 销毁服务宿主对象实例
/// </summary>
public void Dispose()
{
if (_myhost != null)
{
(_myhost as IDisposable).Dispose();
}
}
}
Client客户端
使用之前需要先引入不再同一个项目中的服务
class Program
{
static void Main(string[] args)
{
using (HelloProxy proxy = new HelloProxy())
{
//利用代理调用服务
Console.WriteLine(proxy.Say("张三"));
Console.ReadKey();
}
}
}
//硬编码定义服务契约
[ServiceContract]
interface IService
{
//服务操作
[OperationContract]
String Say(String name);
}
/// <summary>
/// 客户端代理类型 :ClientBase<IHelloService>创建客户端对象
/// </summary>
class HelloProxy :ClientBase<IHelloService>,IService
{
//硬编码定义绑定
public static readonly Binding HelloBinding = new NetNamedPipeBinding();
//硬编码定义地址
public static readonly EndpointAddress HelloAddress = new EndpointAddress(new Uri("net.pipe://localhost/Hello"));//uri必须与服务端中的匹配,才能访问服务端内容,详情见HelloServiceHost/Program.cs
/// <summary>
/// 构造方法
/// </summary>
public HelloProxy() : base(HelloBinding, HelloAddress) { }
public string Say(string name)
{
//使用channel属性对服务进行调用
return Channel.SayHello(name);
}
}
配置文件创建
通过配置文件创建WCF项目,只是将上面的ServiceHost中的代码通过配置文件配置,及使用配置文件替换ServiceHost手动编码。其它没有变动。
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<system.serviceModel>
<services>
<service name="WinformHello.HelloService" behaviorConfiguration="TestBehavior"><!--name不能私自设定,是服务端文件路径;behaviorConfiguration必须与下方的behavior相匹配-->
<host>
<baseAddresses>
<add baseAddress="http://localhost:8080/Hello"/>
</baseAddresses>
</host>
<endpoint address="" binding="basicHttpBinding" contract="WCF_ServiceDemo.IHelloService"></endpoint><!--contract必须是服务协议文件路径-->
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="TestBehavior"><!--名字可随意,但是上方的behaviorConfiguration必须与之相对应-->
<serviceMetadata httpGetEnabled="true"/><!--true为允许我们访问当前WCF里面的服务-->
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>