您的位置:首页 > 其它

WCF入门教程2——创建第一个WCF程序

2015-01-28 23:03 393 查看
本节目标

定义服务契约

创建宿主程序

创建客户端程序访问服务

定义服务契约

ServiceContract特性:该特性可被用来作用于子类或者借口之上,并允许重复声明。

OperationContract:只有定义了该特性的方法才会被放入服务之中。

1、新建服务程序

新建项目——类库,这里我们先不直接新建一个WCF服务,而是新建一个类库,命名为HelloService



添加引用





删除Class1.cs,然后新建一个接口IHelloService.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel; //添加命名空间,这是WCF的核心库

namespace HelloService

{
[ServiceContract]
public interface IHelloService
{
[OperationContract]
string SayHello(string name);
}
}

添加HelloService类:

public classHelloService:IHelloService
{
public string SayHello(string name)
{
return "你好,我是:" + name;
}
}

ServiceHost类型:当IIS活WAS作为宿主程序时,IIS和WAS会自动创建ServiceHost类型。

手动创建的基本语法:public ServiceHost(TypeserviceType,params Uri[] baseAddresses);

2、新建宿主

新建项目——控制台应用程序



然后添加System.ServiceModel引用,和项目引用HelloService,引用之前的类库项目。



HelloServiceHost 项目中Program.cs代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.ServiceModel;
using System.ServiceModel.Channels; //使用到了绑定

namespace HelloServiceHost
{
class Program
{
static void Main(string[] args)
{
using (MyHelloHost host=newMyHelloHost())
{
host.Open();
Console. Console.ReadLine();
}
}
}

public class MyHelloHost:IDisposable
{
/// <summary>
/// 定义一个服务对象
/// </summary>
private ServiceHost _myHelloHost;
public const string BaseAddress = "net.pipe://localhost"; //基地址
public const string HelloServiceAddress = "Hello"; //可选地址
public static readonly Type ServiceType =typeof(HelloService.HelloService);  //服务契约实现类型
public static readonly Type ContractType =typeof(HelloService.IHelloService);  //服务契约接口
public static readonly Binding HelloBinding = new NetNamedPipeBinding(); //服务定义一个绑定

/// <summary>
/// 构造方法
/// </summary>
public MyHelloHost()
{
CreateHelloServiceHost();
}

/// <summary>
/// 构造服务对象
/// </summary>
protected void CreateHelloServiceHost()
{
_myHelloHost = newServiceHost(ServiceType, new Uri[] { new Uri(BaseAddress) });//创建服务对象
_myHelloHost.AddServiceEndpoint(ContractType, HelloBinding,HelloServiceAddress); //添加终结点
}

/// <summary>
/// 打开服务方法
/// </summary>
public void Open()
{
Console.WriteLine("开始启动服务...");
_myHelloHost.Open();
Console.WriteLine("服务已启动");
}

/// <summary>
/// 销毁服务宿主对象实例
/// </summary>
public void Dispose()
{
if (_myHelloHost != null)
(_myHelloHost asIDisposable).Dispose();
}
}

}

3、新建客户端调用程序

新建项目——控制台应用程序





HelloClient项目中Program.cs代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.ServiceModel;
using System.ServiceModel.Channels;
using HelloService;

namespace HelloClient
{
class Program
{
static void Main(string[] args)
{
using(HelloProxy proxy=new HelloProxy())
{
//利用代理调用方法
Console.WriteLine(proxy.Say("郑少秋"));
Console.ReadLine();
}
}
}

[ServiceContract]
interface IService
{
[OperationContract]
string Say(string name);
}

class HelloProxy:ClientBase<IHelloService>,IService
{
public static readonly Binding HelloBinding = newNetNamedPipeBinding();  //硬编码定义绑定
//硬编码定义地址 注意:这里要和之前服务定义的地址保持一直
public static readonly EndpointAddress HelloAddress =newEndpointAddress(new Uri("net.pipe://localhost/Hello"));
public HelloProxy() : base(HelloBinding,HelloAddress) { } //构造方法

public string Say(string name)
{
//使用Channel属性对服务进行调用
return Channel.SayHello(name);
}
}
}




先运行HelloServiceHost



然后运行HelloClient

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: