您的位置:首页 > 其它

如何使用WCF服务实现分布式处理数据?

2014-03-13 10:11 856 查看
前面-我已经讲到如何 创建 Windows Service 服务 ,那么今天我将介绍如何创建WCF 服务(采用TCP通信)

一、创建一个WIndow Service 服务

具体步骤参考之前的文章 创建 Windows Service 服务

二、 定义接口层

1) 创建一个 Window 类库



2) 创建一个接口类



注: 创建好类库后 右键 类库-->>添加引用 (Add Reference)-->>选项卡中选择.net -->>找到 System.ServiceModel-->>选中-->>确定。

创建一个接口类: ITestInterface.cs

编写好自己需要的方法 (接口中都是未实现的方法)

在接口上标注 [ServiceContract] (代表此接口及实现此接口的类都是对外发布的Service类)

在每个方法上标注 [OperationContract] (代表此 方法 外部可以访问到)

具体代码如下:

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

namespace MyTest.Contracts
{
//[ServiceContract]和[OperationContract]这两个标签需要导入using System.ServiceModel 命名空间。
//代表此接口及实现此接口的类都是对外发布的Service类
[ServiceContract(Name = "MyTest.Contracts", Namespace = "测试接口")]
public interface ITestInterface
{
/// <summary>
/// 获取 姓名
/// </summary>
/// <returns></returns>
[OperationContract]  //使 外部可以访问到此方法。
// 注: 每个方法必须都加  [OperationContract]标识,否则外部无法调用。
string GetMyName();

//这里测试就定义这么一个简单的方法
//实际工作中根据自己的需要去定义...
//

//由于这里没有用到 Model 层, 如果需要使用到Model,应该增加如下配置:
// [DataContract]标签,在每个需要序列化的成员变量上加入[DataMember]标签。
// 这两个标签在使用的进候需要导入using System.Runtime.Serialization命名空间。
// [DataContract] // WCF调用中类能够被序列化
// public class Model
// {
//     [DataMember] // WCF调用中属性能够被序列化
//     public string Name;
// }
}
}


三、创建服务方法层 (相当于 项目中的BLL业务逻辑层)

1) 创建一个Window 类库



2) 创建 实现接口方法的类



注: 创建好类库后 右键 类库-->>添加引用 (Add Reference)-->>选项卡中选择.net -->>找到 System.ServiceModel-->>选中-->>确定。

右键 类库-->>添加引用 (Add Reference)-->>选项卡中选择Projects-->>找到 MyTest.Contracts-->>选中-->>确定。

创建一个类: Test.cs 并且继承 : ITestInterface 接口实现接口方法

在类上标注:[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]

(此标签代表这个类采用SingleTone(单类模式)来生成 对象。)

四、配置 WCF

1) 添加一个 app.config 配置文件



2) 编辑配置文件



具体配置如下:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="TESTBehavior">
<serviceDebug includeExceptionDetailInFaults="true" />
<serviceMetadata httpGetEnabled="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<netTcpBinding>
<binding name="TcpTEST" maxReceivedMessageSize="2147483647" maxBufferPoolSize="2147483647" maxBufferSize="2147483647" transferMode="Streamed">
<security mode="None" />
</binding>
</netTcpBinding>
</bindings>
<services>
<!-- name="MyTest.Service.Test" 指定实现接口的方法 即:之前创建的类 Test.cs  -->
<service name="MyTest.Service.Test" behaviorConfiguration="TESTBehavior">
<host>
<baseAddresses>
<!-- 配置调用 的端口号 (建议配置端口 都大于 6000,因为 6000以下 因为大部分端口都被系统占用,如 80 端口 ) -->
<add baseAddress="net.tcp://localhost:8888/service"/>
</baseAddresses>
</host>
<!-- contract="MyTest.Contracts.ITestInterface" 指定 定义的接口 即:之前创建的接口 ITestInterface.cs  -->
<endpoint
address="test"
binding="netTcpBinding"
contract="MyTest.Contracts.ITestInterface"
bindingConfiguration="TcpTEST" />
<endpoint
address="mex"
binding="mexTcpBinding"
contract="IMetadataExchange"   />
</service>
</services>
</system.serviceModel>
</configuration>


3) 设置启动服务类



具体源码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.ServiceModel;

namespace MyTest.Service
{
public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
}
private ServiceHost host;
//启动 服务 调用
protected override void OnStart(string[] args)
{
//Test 是提供调用实现功能的类 即:之前创建的类  MyTest.Service.Test.cs
host = new ServiceHost(typeof(Test));
host.Opened += delegate
{
// " 服务启动!" 这里可以定义一个 委托 记录下 服务启动日志
};
try
{
host.Open();
}
catch (Exception ex)
{
//"服务启动异常!"  //服务启动异常  可以记录下日志
host.Abort();
}
}
//关闭 服务 调用
protected override void OnStop()
{
//停止 服务 可以记录下日志
}
}
}


---恭喜,到这一步你已经全部完成了。WCF服务 。

现在 我们就将它 部署到 测试环境上。--具体部署 还是去参考: 创建 Windows Service 服务
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: