我的基于WCF的SOA架构项目实战,博客园首次发文!
本人才疏学浅,在现在的项目中即是程序员又是架构设计组组长。在使用WCF技术前看了无数号称WCF实战博客,没有一篇是我想要的。不过也算学到些皮毛。在此谢过,对你们的善意分享和辛勤劳动表示万分的感谢。由于以前是在csdn写博客,到博客园发文算是第一次。还请各位高手跳过,但愿不会浪费各位的表情。这里先上几张图,后面再讲过程。
- 解决方案截图
架构过程遇到的问题和解决方法。
1、要不要使用dto对象,要不要建立DTO层?
解决办法是巧妙的将实体对象和dto对象合并,这样少了转换的过程。感觉还行。
样本如下:
namespace RTLS.Entities
{
[Serializable, DataContract(IsReference = true)]
[KnownType(typeof(SysRolefunc))]
public class SysRole
{
[DataMember]
public virtual int Roleid { get; set; }
[DataMember]
public virtual string Rolename { get; set; }
[DataMember]
public virtual bool Active { get; set; }
[DataMember]
public virtual string Desc { get; set; }
[DataMember]
public virtual DateTime? Timestamp { get; set; }
[DataMember]
public virtual string Remark { get; set; }
private IList<SysRolefunc> list;
//一对多关系:角色(Role)有一个或多个角色功能(SysRolefunc)
[DataMember]
public virtual IList<SysRolefunc> RoleFuncs
{
get
{
if (list == null)
{
list = new List<SysRolefunc>();
}
return list;
}
set
{
list = value;
}
}
}
}
using System;
using System.ServiceModel;
namespace RTLS.IServices
{
[ServiceContract]
public interface ISysRoleService
{
[OperationContract]
bool AddSysRole(RTLS.Entities.SysRole role);
[OperationContract]
bool DeleteSysRole(RTLS.Entities.SysRole role);
[OperationContract]
System.Collections.Generic.IList<RTLS.Entities.SysRole> GetAll(bool isActive);
[OperationContract]
System.Collections.Generic.IList<RTLS.Entities.SysRole> GetPageResults(int curPageNo, int limit,
string name, bool isActive,
ref int totalCount);
[OperationContract]
System.Collections.Generic.IList<RTLS.Entities.SysRole> GetRoleByUserId(int userID);
[OperationContract]
bool ModifySysRole(RTLS.Entities.SysRole role);
}
}
namespace RTLS.Services
{
/// <summary>
/// 系统角色服务类
/// </summary>
public class SysRoleService : ISysRoleService
{
private SysRoleBiz biz = new SysRoleBiz();
public bool AddSysRole(Entities.SysRole role)
{
var result = biz.AddSysRole(role);
return result.Success();
}
public bool DeleteSysRole(Entities.SysRole role)
{
return biz.DeleteSysRole(role);
}
public IList<Entities.SysRole> GetAll(bool isActive)
{
return biz.GetAll(isActive);
}
public IList<Entities.SysRole> GetPageResults(int curPageNo, int limit, string name, bool isActive, ref int totalCount)
{
return biz.GetPageResults(curPageNo,limit,name,isActive,ref totalCount);
}
public IList<Entities.SysRole> GetRoleByUserId(int userID)
{
return biz.GetRoleByUserId(userID);
}
public bool ModifySysRole(Entities.SysRole role)
{
return biz.ModifySysRole(role);
}
}
}
虽然说不推荐这样用,但是工期有限啊。没有使用Linq,所以要真是dto-->entity,entity-->entity转换来转换去的要死人的。因为这个原因曾经打算放弃使用WCF,让客户端直接引用BLL和Model层。后来发现这样合并了之后可以继续下去了,就这样做了。对错先暂且不论。
2、怎么调试和设置WCF参数?
当然是摸索了,跌倒,跌倒...爬起来,再来!上几张图:
<service behaviorConfiguration="RTLS.Services.ServiceBehavior"
name="RTLS.Services.SystemFunctionService">
<endpoint address="net.tcp://192.168.1.123:9998/RTLS_SysFun_Svr" binding="netTcpBinding" bindingConfiguration="myNetTcpBinding"
name="net" contract="RTLS.IServices.ISystemFunctionService" />
<endpoint address="mex" binding="mexTcpBinding" bindingConfiguration=""
name="mex" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="net.tcp://192.168.1.123:9998/RTLS_SysFun_Svr" />
</baseAddresses>
</host>
</service>
</services>
3.一个Winnt服务如何host多个wcf服务?
protected override void OnStart(string[] args)
{
if (serviceHostes.Count > 0) serviceHostes.Clear();
var configuration = ConfigurationManager.OpenExeConfiguration(Assembly.GetEntryAssembly().Location);
ServiceModelSectionGroup serviceModelSectionGroup = (ServiceModelSectionGroup)configuration.GetSectionGroup("system.serviceModel");
// 开启每个服务
foreach (ServiceElement serviceElement in serviceModelSectionGroup.Services.Services)
{
var wcfServiceType = Assembly.Load("RTLS.Services").GetType(serviceElement.Name);
var serviceHost = new ServiceHost(wcfServiceType);
serviceHostes.Add(serviceHost);
serviceHost.Opened += delegate
{
LogManager.WriteLog("Log", string.Format("{0}开始监听Uri为:{1}",
serviceElement.Name, serviceElement.Endpoints[0].Address.ToString()));
};
serviceHost.Open();
}
}
这个办法是在吉日大哥的通用权限组件里面看到的,这里对吉日大哥表示衷心的感谢!希望大伙尊重一下扎扎实实搞技术的人,我不是为了跟吉日大哥打广告。做人要懂得感恩!你技术牛,你鄙视我可以,我不羡慕你,也不一定要屌你。
4、怎样使用泛型工厂类来简化和重构代码?
这个正在尝试中,毕竟先完成了。
5、使用winnt服务的方式安装、发布容易吗?好用吗?
/// <summary>
/// 应用程序的主入口点。
/// </summary>
static void Main(string[] args)
{
if (args.Length > 0)
{
AssemblyInstaller myAssemblyInstaller;
myAssemblyInstaller = new AssemblyInstaller();
myAssemblyInstaller.UseNewContext = true;
myAssemblyInstaller.Path = System.AppDomain.CurrentDomain.BaseDirectory + "\\" + System.AppDomain.CurrentDomain.FriendlyName;
System.Collections.Hashtable mySavedState = new System.Collections.Hashtable();
switch (args[0].ToLower())
{
case "-i":
myAssemblyInstaller.Install(mySavedState);
myAssemblyInstaller.Commit(mySavedState);
myAssemblyInstaller.Dispose();
return;
case "-u":
myAssemblyInstaller.CommandLine = new string[1] { "/u" };
myAssemblyInstaller.Uninstall(null);
myAssemblyInstaller.Dispose();
return;
default:
System.Console.WriteLine("------参数说明------");
System.Console.WriteLine("- i 安装服务!");
System.Console.WriteLine("- u 卸载服务!");
System.Console.ReadKey();
return;
}
}
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new RtlsHostSvr()
};
ServiceBase.Run(ServicesToRun);
想知道怎样调试服务程序像调试控制台程序一样吗? 把服务的OnStart方法标记为public new .默认是protected override.然后像下面那样取消那些注释,并注释上面 的代码就ok了。
//var sHostSvr = new RtlsHostSvr();
//sHostSvr.OnStart(null);
//System.Threading.Thread.Sleep(10000);
//sHostSvr.OnStop();
}
作者:数据酷软件
出处:https://www.cnblogs.com/datacool/archive/2011/07/13/WCFSOA.html
关于作者:20年编程从业经验,持续关注MES/ERP/POS/WMS/工业自动化
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明。
联系方式: qq:71008973;wx:6857740733
基于人脸识别的考勤系统 地址: https://gitee.com/afeng124/viewface_attendance_ext
自己开发安卓应用框架 地址: https://gitee.com/afeng124/android-app-frame
WPOS(warehouse+pos) 后台演示地址: http://47.239.106.75:8080/
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构