最近遇到一个新项目,需要与硬件结合,进行读取信息并保存在数据库中。业务要求也在昨天发布一个问题,当然感谢许多园内的朋友出谋划策,截图有真相!

业务需求那些事,使用WCF如何实现业务需求!-LMLPHP

关于这个问题,我做了如下假设。目前处于测试状态,代码比较简单,具体功能已经实现。

1、首先一张比较丑的图片表名业务逻辑关系。


业务需求那些事,使用WCF如何实现业务需求!-LMLPHP

用户customer 他可以去A  B  C  D 四个办事处去登记信息。这样假设每个办事处有一个登记人员,假设customer 选择了 A ,(可能是离A比较近,他不想跑了)

登记人员说出示的卡。登记人员就把的卡往Arecorder 一放,点击鼠标,用户的信息就可以显示到web界面上了。看到这里是不是感觉很简单,不就是Arecorder 把customer的信息发送给web页面了吗?

你说的没有错,就是这么简单。那简单咱就技术实现吧。

技术实现:需要考虑哪些问题。

      1》  A recorder 只是一台具有连接网络或者局域网的计算机。 web 服务管理也不是部署在 A recorder 计算机上的,当然大家知道是部署在服务器上的。那么问题来了。要实现web服务器与Arecorder之间的通信怎么实现呢?

这也难不倒你,网络传输tcp socket编程。。。不难。这里对于这个问题,我们不做深入探究。我使用的是WCF技术。

2》  好了,web服务与A recorder 通信的问题解决了,那就写程序白。。。。。,半个小时过去了,你的编码速度很牛啊,service端和client的代码都搞定了,现在就测试可不可以呢。说道这里,service端的代码发布到与web服务IIS上面去,client代码当然去监控A recorder了,这里本来没有任何问题,我们知道service 的程序时一直运行的,它们之间的关系可以简单一张图表示,当然只是简单的 request-response模式。

业务需求那些事,使用WCF如何实现业务需求!-LMLPHP

看到这里,你可能发现问题。我应该何时请求呢,我贴卡的时候去Request,这是个好主意,Request 发送到哪里呢,肯定是service呀,不然还能发送到哪了,哈,那么问题来了? web服务管理不是给你一个人使用的,也就是说有好多人都在使用这套管理系统,可能赵四 王五 都在 登记用户信息,好你拿着custmoer的卡一刷,Request 就发送出去了,那么它会发送到你打开的web 页上吗???,或者发送到 赵四、或者王五的打开的页面上呢,那么这样这么多不确定性,确实不好办呢。怎么办???赶紧想呀,经理崔项目呢。。。

3》 时间一点一点过去,你想呀,突然脑袋灵光一现,为什么我非要等到customer刷卡的时,去Request呢,我为什么不能使登记人员去把握Request呢。而 A recorder 成为service呢。这样之间的关系不就清楚了吗。是呀到这里,恭喜你。那还等什么赶紧code呀。。。。,测试下,果然不错。哈哈哈。

4》经理说,我们不是需要一个A recorder,而是有 B C D 。。。,你一个都搞定了,多加几个也是没有问题的吧。瞬间整个人都不好了。。。,经理说了那也上呀,多个录卡器,多个工作登记人员,要想实现信息登录的正确性,怎么办,web服务又是大家共享的,顺着思路想下去,。。。。,多个Recorder,那就要区分每个Recorder,那好办,每个都绑定一个固定的IP不就可以了吗? 然后让web request ,好呀,干嘛,code呀。。。。,不对,我web request 了,那我应该需要的是哪个recorder的信息呢,怎么办? 这可能会造成我把别的customer的信息请求过来的。要是事先能知道Request哪一个就好了,怎么办,那就让登记人员,在请求的时候把选中那个recorder。不就可以了吗。是呀,如图:

业务需求那些事,使用WCF如何实现业务需求!-LMLPHP

5》到这里,通过分析已经找到了,解决方案,那么接下来要看编程技术了,这里只是简单的分享下。

coding: Recorder 端的代码。wcf基本结构四层结构;

业务需求那些事,使用WCF如何实现业务需求!-LMLPHP

Contract:

业务需求那些事,使用WCF如何实现业务需求!-LMLPHP

1---》IOctorpusService.cs

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
namespace DataWorld.Octorpus.Contract
{
[ServiceContract]
[ServiceKnownType(typeof(RecordMintor))]
public interface IOctorpusService
{
[OperationContract]
[FaultContract(typeof(OctorException))]
string GetOctorpusNo();
}
}

2----》OctorException.cs

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
namespace DataWorld.Octorpus.Contract
{
[DataContract]
public class OctorException
{
#region 私有类
private string _operation;
private string _errorMessage;
#endregion
#region 构造函数
public OctorException(string operation, string errorMessage)
{
this._operation = operation;
this._errorMessage = errorMessage;
}
#endregion
#region 公共方法
[DataMember]
public string Operation
{
get { return _operation; }
set { _operation = value; }
}
[DataMember]
public string ErrorMessage
{
get { return _errorMessage; }
set { _errorMessage = value; }
}
#endregion }
}

3----》RecordMintor.cs

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
namespace DataWorld.Octorpus.Contract
{
[DataContract]
public class RecordMintor
{
#region 私有字段
private string _CarNo;
private string _OctorpusNo;
#endregion
#region 构造函数
public RecordMintor(string carno, string octorpusno)
{
this._CarNo = carno;
this._OctorpusNo = octorpusno;
}
#endregion
#region 公共属性
[DataMember]
public string CarNo
{
get { return _CarNo; }
set { _CarNo = value; }
}
[DataMember]
public string OctorpusNo
{
get { return _OctorpusNo; }
set { _OctorpusNo = value; }
}
#endregion
}
}

Service:

业务需求那些事,使用WCF如何实现业务需求!-LMLPHP

1--》OctorpusService.cs

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataWorld.Octorpus.Contract;
namespace DataWorld.Octorpus.Service
{
public class OctorpusService:IOctorpusService
{ public string GetOctorpusNo()
{
// OctorException oe = new OctorException("Server exception", "Restart server"); RecordMintor rm = new RecordMintor(Guid.NewGuid().ToString(), Guid.NewGuid().ToString());
//throw new NotImplementedException();
Console.WriteLine(rm.CarNo);
return rm.OctorpusNo;
}
}
}

hosting:

业务需求那些事,使用WCF如何实现业务需求!-LMLPHP

1--》 program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataWorld.Octorpus.Service;
using System.ServiceModel;
namespace DataWorld.Octorpus.Hosting
{
class Program
{
static void Main(string[] args)
{
using (ServiceHost host = new ServiceHost(typeof(OctorpusService)))
{
host.Opened += delegate { Console.WriteLine("listening..."); };
host.Open();
Console.Read();
}
}
}
}

2--》App.config

 <?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="wsHttpBinding">
<security mode="None">
</security>
</binding>
</wsHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors> <behavior name="OctorpusServiceBehavior">
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="Company.Octorpus.Service.OctorpusService" behaviorConfiguration="OctorpusServiceBehavior">
<endpoint address="" binding="wsHttpBinding" contract="Company.Octorpus.Contract.IOctorpusService" bindingConfiguration="wsHttpBinding"/>
<host>
<baseAddresses>
<add baseAddress="http://192.168.19.56:9999/Service"/>
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
</configuration>

client:我写在了web中;

AutoGetCarNO.ashx

 using System;
using System.Collections.Generic;
using System.Web; namespace fairviewweb.Management
{
/// <summary>
/// Summary description for AutoGetCarNO
/// </summary>
public class AutoGetCarNO : IHttpHandler
{
private DateTime _endTime;
private bool _IsEndFlag = false;
private string _msg=null;
private Maticsoft.BLL.t_Recorder tbll = new Maticsoft.BLL.t_Recorder();
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
string CarNodroplist = context.Request.QueryString["CarNodroplist"] == null ? string.Empty : context.Request.QueryString["CarNodroplist"].ToString();
Maticsoft.Model.t_Recorder tmodel= tbll.GetModel(CarNodroplist.Trim());
this._endTime=DateTime.Now.AddSeconds();
switch (tmodel.IPADDRESS)
{
case "ServiceReferenceA": try
{
ServiceReference1.OctorpusServiceClient _octorpusNO = new ServiceReference1.OctorpusServiceClient();
this._msg = _octorpusNO.GetOctorpusNo();
while (!_IsEndFlag)
{ if (!string.IsNullOrEmpty(this._msg))
{
_IsEndFlag = true;
}
if (this._endTime <= DateTime.Now)
{
_IsEndFlag = true;
}
}
if (string.IsNullOrEmpty(this._msg))
{
context.Response.Write("timeout");
}
else
{
context.Response.Write(this._msg);
}
}
catch (Exception ex)
{
string msg = ex.Message;
context.Response.Write("timeout");
}
break;
case "ServiceReferenceB": try
{
ServiceReference2.OctorpusServiceClient _octorpusNO = new ServiceReference2.OctorpusServiceClient();
this._msg = _octorpusNO.GetOctorpusNo();
while (!_IsEndFlag)
{ if (!string.IsNullOrEmpty(this._msg))
{
_IsEndFlag = true;
}
if (this._endTime <= DateTime.Now)
{
_IsEndFlag = true;
}
}
if (string.IsNullOrEmpty(this._msg))
{
context.Response.Write("timeout");
}
else
{
context.Response.Write(this._msg);
}
}
catch (Exception ex)
{
string msg = ex.Message;
context.Response.Write("timeout");
}
break;
case "ServiceReferenceC": try
{
ServiceReference1.OctorpusServiceClient _octorpusNO = new ServiceReference1.OctorpusServiceClient();
this._msg = _octorpusNO.GetOctorpusNo();
while (!_IsEndFlag)
{ if (!string.IsNullOrEmpty(this._msg))
{
_IsEndFlag = true;
}
if (this._endTime <= DateTime.Now)
{
_IsEndFlag = true;
}
}
if (string.IsNullOrEmpty(this._msg))
{
context.Response.Write("timeout");
}
else
{
context.Response.Write(this._msg);
}
}
catch (Exception ex)
{
//string msg = ex.Message;
context.Response.Write("timeout");
}
break;
} } public bool IsReusable
{
get
{
return false;
}
}
}
}

最后还给大家来个小贴图把:

前段的;

业务需求那些事,使用WCF如何实现业务需求!-LMLPHP

简单说明下,这个B代表是个Recorder ,然后去请求客户端。页面的实现是采用Ajax异步传输的,这里就不在累赘了。我只是采用这种办法,解决了,这个问题,如果你有更好的办法,可以继续交流。谢谢!

04-13 19:39