大恶人吉日嘎拉之走火入魔闭门造车之.NET疯狂架构经验分享系列之(七)WCF支持

来源:互联网 发布:google play软件推荐 编辑:程序博客网 时间:2024/04/28 15:53

前后购买了N个WCF的书籍,中国人写书,就怕文字不够数一样,密密麻麻写了一大堆,又难看懂,又浪费时间,其实我们都想快速见效,文字太多看来看去看得头大、容易导致晕书症,建议出书的人多插图,少写字。

 

本文只做入门参考用,WCF服务器端注意事项

1:WCF服务器端配置文件如何写很关键、例如多个服务怎么一同发布?

2:如何用最简单的程序,把WCF服务器发布好?

3:配置文件如何配置,客户端才能正常引用已发布的服务?

其实这3个问题,是服务器端编写程序的核心关键问题,这解决了,就算是好入门了,接着可以深入某个问题了。

 

工程的效果图如下:

 

以下程序是启动WCF服务用的程序参考:

 

            // 读取配置文件
            Configuration configuration = ConfigurationManager.OpenExeConfiguration(Assembly.GetEntryAssembly().Location);
            ServiceModelSectionGroup serviceModelSectionGroup 
= (ServiceModelSectionGroup)configuration.GetSectionGroup("system.serviceModel");
            
// 开启每个服务
            foreach (ServiceElement serviceElement in serviceModelSectionGroup.Services.Services)
            {
                var serviceHost 
= new ServiceHost(Assembly.Load("DotNet.Service").GetType(serviceElement.Name), serviceElement.Endpoints[0].Address);
                serviceHost.Opened 
+= delegate { Console.WriteLine("{0}", serviceHost.BaseAddresses[0]); };
                serviceHost.Open();
            }

 

 

以下文件为 WCF的服务器端配置文件:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  
<system.serviceModel>
    
<services>
      
<service name="DotNet.Service.BusinessCardService" behaviorConfiguration="Internet">
        
<endpoint address="http://localhost:8888/DotNet.Service/BusinessCardService/" binding="basicHttpBinding" contract="DotNet.IService.IBusinessCardService">
        
</endpoint>
      
</service>
      
<service name="DotNet.Service.ExceptionService" behaviorConfiguration="Internet">
        
<endpoint address="http://localhost:8888/DotNet.Service/ExceptionService/" binding="basicHttpBinding" contract="DotNet.IService.IExceptionService">
        
</endpoint>
      
</service>
      
<service name="DotNet.Service.FileService" behaviorConfiguration="Internet">
        
<endpoint address="http://localhost:8888/DotNet.Service/FileService/" binding="basicHttpBinding" contract="DotNet.IService.IFileService">
        
</endpoint>
      
</service>
      
<service name="DotNet.Service.FolderService" behaviorConfiguration="Internet">
        
<endpoint address="http://localhost:8888/DotNet.Service/FolderService/" binding="basicHttpBinding" contract="DotNet.IService.IFolderService">
        
</endpoint>
      
</service>
      
<service name="DotNet.Service.ItemDetailsService" behaviorConfiguration="Internet">
        
<endpoint address="http://localhost:8888/DotNet.Service/ItemDetailsService/" binding="basicHttpBinding" contract="DotNet.IService.IItemDetailsService">
        
</endpoint>
      
</service>
      
<service name="DotNet.Service.ItemsService" behaviorConfiguration="Internet">
        
<endpoint address="http://localhost:8888/DotNet.Service/ItemsService/" binding="basicHttpBinding" contract="DotNet.IService.IItemsService">
        
</endpoint>
      
</service>
      
<service name="DotNet.Service.LoginService" behaviorConfiguration="Internet">
        
<endpoint address="http://localhost:8888/DotNet.Service/LoginService/" binding="basicHttpBinding" contract="DotNet.IService.ILoginService">
        
</endpoint>
      
</service>
      
<service name="DotNet.Service.LogService" behaviorConfiguration="Internet">
        
<endpoint address="http://localhost:8888/DotNet.Service/LogService/" binding="basicHttpBinding" contract="DotNet.IService.ILogService">
        
</endpoint>
      
</service>
      
<service name="DotNet.Service.MessageService" behaviorConfiguration="Internet">
        
<endpoint address="http://localhost:8888/DotNet.Service/MessageService/" binding="basicHttpBinding" contract="DotNet.IService.IMessageService">
        
</endpoint>
      
</service>
      
<service name="DotNet.Service.ModuleService" behaviorConfiguration="Internet">
        
<endpoint address="http://localhost:8888/DotNet.Service/ModuleService/" binding="basicHttpBinding" contract="DotNet.IService.IModuleService">
        
</endpoint>
      
</service>
      
<service name="DotNet.Service.OrganizeService" behaviorConfiguration="Internet">
        
<endpoint address="http://localhost:8888/DotNet.Service/OrganizeService/" binding="basicHttpBinding" contract="DotNet.IService.IOrganizeService">
        
</endpoint>
      
</service>
      
<service name="DotNet.Service.ParameterService" behaviorConfiguration="Internet">
        
<endpoint address="http://localhost:8888/DotNet.Service/ParameterService/" binding="basicHttpBinding" contract="DotNet.IService.IParameterService">
        
</endpoint>
      
</service>
      
<service name="DotNet.Service.PermissionAdminService" behaviorConfiguration="Internet">
        
<endpoint address="http://localhost:8888/DotNet.Service/PermissionAdminService/" binding="basicHttpBinding" contract="DotNet.IService.IPermissionAdminService">
        
</endpoint>
      
</service>
      
<service name="DotNet.Service.PermissionService" behaviorConfiguration="Internet">
        
<endpoint address="http://localhost:8888/DotNet.Service/PermissionService/" binding="basicHttpBinding" contract="DotNet.IService.IPermissionService">
        
</endpoint>
      
</service>
      
<service name="DotNet.Service.RoleService" behaviorConfiguration="Internet">
        
<endpoint address="http://localhost:8888/DotNet.Service/RoleService/" binding="basicHttpBinding" contract="DotNet.IService.IRoleService">
        
</endpoint>
      
</service>
      
<service name="DotNet.Service.SequenceService" behaviorConfiguration="Internet">
        
<endpoint address="http://localhost:8888/DotNet.Service/SequenceService/" binding="basicHttpBinding" contract="DotNet.IService.ISequenceService">
        
</endpoint>
      
</service>
      
<service name="DotNet.Service.StaffService" behaviorConfiguration="Internet">
        
<endpoint address="http://localhost:8888/DotNet.Service/StaffService/" binding="basicHttpBinding" contract="DotNet.IService.IStaffService">
        
</endpoint>
      
</service>
      
<service name="DotNet.Service.UserService" behaviorConfiguration="Internet">
        
<endpoint address="http://localhost:8888/DotNet.Service/UserService/" binding="basicHttpBinding" contract="DotNet.IService.IUserService">
        
</endpoint>
      
</service>
      
<service name="DotNet.Service.WorkReportService" behaviorConfiguration="Internet">
        
<endpoint address="http://localhost:8888/DotNet.Service/WorkReportService/" binding="basicHttpBinding" contract="DotNet.IService.IWorkReportService">
        
</endpoint>
      
</service>
    
</services>
    
<behaviors>
      
<serviceBehaviors>
        
<behavior name="Internet">
          
<serviceMetadata httpGetEnabled="true" />
        
</behavior>
      
</serviceBehaviors>
    
</behaviors>
  
</system.serviceModel>
</configuration>

 

 

接口文件参考:

//------------------------------------------------------------
// All Rights Reserved , Copyright (C) 2010 , Jirisoft , Ltd. 
//------------------------------------------------------------

using System.Data;
using System.ServiceModel;

namespace DotNet.IService
{
    
using DotNet.Utilities;

    
/// <summary>
    
/// ILoginService
    
/// 
    
/// 修改纪录
    
/// 
    
///        2009.04.15 版本:1.0 JiRiGaLa 添加接口定义。
    
///        
    
/// 版本:1.0
    
///
    
/// <author>
    
///        <name>JiRiGaLa</name>
    
///        <date>2009.04.15</date>
    
/// </author> 
    
/// </summary>
    [ServiceContract]
    
public interface ILoginService
    {
        
/// <summary>
        
/// 获得登录用户列表
        
/// </summary>
        
/// <param name="userInfo">用户</param>
        
/// <returns>数据表</returns>
        [OperationContract]
        DataTable GetUserDT(BaseUserInfo userInfo);

        
/// <summary>
        
/// 获得内部员工列表
        
/// </summary>
        
/// <param name="userInfo"></param>
        
/// <returns></returns>
        [OperationContract]
        DataTable GetStaffDT(BaseUserInfo userInfo);


        
/// <summary>
        
/// 按唯一识别码登录
        
/// </summary>
        
/// <param name="userInfo">用户</param>
        
/// <param name="suid">唯一识别码</param>
        
/// <param name="statusCode">返回状态码</param>
        
/// <param name="statusMessage">返回状消息</param>
        
/// <returns>用户实体</returns>
        [OperationContract]
        BaseUserInfo LoginBySuid(BaseUserInfo userInfo, 
string suid, out string statusCode, out string statusMessage);

        
/// <summary>
        
/// 按用户名登录
        
/// </summary>
        
/// <param name="userInfo">用户</param>
        
/// <param name="userName">用户名</param>
        
/// <param name="statusCode">返回状态码</param>
        
/// <param name="statusMessage">返回状消息</param>
        
/// <returns>用户实体</returns>
        [OperationContract]
        BaseUserInfo LoginByUserName(BaseUserInfo userInfo, 
string userName, out string statusCode, out string statusMessage);

        
/// <summary>
        
/// 登录
        
/// </summary>
        
/// <param name="userInfo">用户</param>
        
/// <param name="userName">用户名</param>
        
/// <param name="password">密码</param>
        
/// <param name="ipAddress">IP地址</param>
        
/// <param name="statusCode">返回状态码</param>
        
/// <param name="statusMessage">返回状消息</param>
        
/// <returns>登录实体类</returns>
        [OperationContract]
        BaseUserInfo UserLogin(BaseUserInfo userInfo, 
string userName, string password, out string statusCode, out string statusMessage);

        
/// <summary>
        
/// 操作员退出应用程序
        
/// </summary>
        
/// <param name="userInfo">用户</param>
        [OperationContract]
        
void OnExit(BaseUserInfo userInfo);

        
/// <summary>
        
/// 检查在线状态(服务器专用)
        
/// </summary>
        
/// <returns>离线人数</returns>
        [OperationContract]
        
int ServerCheckOnLine();

        
/// <summary>
        
/// 检查在线状态
        
/// </summary>
        
/// <param name="userInfo">用户</param>
        
/// <returns>离线人数</returns>
        [OperationContract]
        
int CheckOnLine(BaseUserInfo userInfo);

        
/// <summary>
        
/// 获取在线用户列表
        
/// </summary>
        
/// <param name="userInfo">用户</param>
        
/// <returns>数据表</returns>
        [OperationContract]
        DataTable GetOnLineState(BaseUserInfo userInfo);

        
/// <summary>
        
/// 设置密码
        
/// </summary>
        
/// <param name="userInfo">用户</param>
        
/// <param name="userIds">被设置的用户主键</param>
        
/// <param name="password">新密码</param>
        
/// <param name="statusCode">返回状态码</param>
        
/// <param name="statusMessage">返回状消息</param>
        
/// <returns>影响行数</returns>
        [OperationContract]
        
int SetPassword(BaseUserInfo userInfo, string[] userIds, string password, out string statusCode, out string statusMessage);
        
        
/// <summary>
        
/// 修改密码
        
/// </summary>
        
/// <param name="userInfo">用户</param>
        
/// <param name="oldPassword">原始密码</param>
        
/// <param name="newPassword">新密码</param>
        
/// <param name="statusCode">返回状态码</param>
        
/// <param name="statusMessage">返回状消息</param>
        
/// <returns>影响行数</returns>
        [OperationContract]
        
int ChangePassword(BaseUserInfo userInfo, string oldPassword, string newPassword, out string statusCode, out string statusMessage);
    }
}

 

 

服务实现参考:

//------------------------------------------------------------
// All Rights Reserved , Copyright (C) 2010 , Jirisoft , Ltd. 
//------------------------------------------------------------

using System;
using System.Data;
using System.Reflection;

namespace DotNet.Service
{
    
using DotNet.Business;
    
using DotNet.DbUtilities;
    
using DotNet.IService;
    
using DotNet.Model;
    
using DotNet.Utilities;

    
/// <summary>
    
/// ILoginService
    
/// 
    
/// 修改纪录
    
/// 
    
///        2009.04.15 版本:1.0 JiRiGaLa 添加接口定义。
    
///        
    
/// 版本:1.0
    
///
    
/// <author>
    
///        <name>JiRiGaLa</name>
    
///        <date>2009.04.15</date>
    
/// </author> 
    
/// </summary>
    public class LoginService : System.MarshalByRefObject, ILoginService
    {
        
#region public void Load()
        
/// <summary>
        
/// 加载服务层
        
/// </summary>
        public void Load()
        {
        }
        
#endregion

        
#region public DataTable GetUserDT(BaseUserInfo userInfo) 获得用户列表
        
/// <summary>
        
/// 获得用户列表
        
/// </summary>
        
/// <param name="userInfo">用户</param>
        
/// <returns>数据表</returns>
        public DataTable GetUserDT(BaseUserInfo userInfo)
        {
            
// 写入调试信息
            #if (DEBUG)
                
int milliStart = BaseBusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
            
#endif
            DataTable dataTable 
= new DataTable(BaseStaffTable.TableName);

            IDbHelper dbHelper 
= DbHelperFactory.GetHelper();
            
try
            {
                dbHelper.Open();
                
// 检查用户在线状态(服务器专用)
                BaseUserManager userManager = new BaseUserManager(dbHelper, userInfo);
                userManager.CheckOnLine();
                
// 获取允许登录列表
                string[] names = new string[] { BaseUserTable.FieldEnabled, BaseUserTable.FieldDeleteMark};
                Object[] values 
= new Object[] { 10};
                dataTable 
= userManager.GetDT(names, values);
                BaseLogManager.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            
catch (Exception ex)
            {
                BaseExceptionManager.LogException(dbHelper, userInfo, ex);
                
throw ex;
            }
            
finally
            {
                dbHelper.Close();
            }

            
// 写入调试信息
            #if (DEBUG)
                BaseBusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart);
            
#endif
            
return dataTable;
        }
        
#endregion

        
#region public DataTable GetUserDT(BaseUserInfo userInfo) 获得内部职员列表
        
/// <summary>
        
/// 获得内部职员列表
        
/// </summary>
        
/// <param name="userInfo">用户</param>
        
/// <returns>数据表</returns>
        public DataTable GetStaffDT(BaseUserInfo userInfo)
        {
            
// 写入调试信息
            #if (DEBUG)
                
int milliStart = BaseBusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
            
#endif
            DataTable dataTable 
= new DataTable(BaseStaffTable.TableName);

            IDbHelper dbHelper 
= DbHelperFactory.GetHelper();
            
try
            {
                dbHelper.Open();
                
// 检查用户在线状态(服务器专用)
                BaseUserManager userManager = new BaseUserManager(dbHelper);
                userManager.CheckOnLine();
                
// 获取允许登录列表
                string[] names = new string[]{BaseUserTable.FieldEnabled, BaseUserTable.FieldDeleteMark, BaseUserTable.FieldIsStaff};
                Object[] values 
= new Object[] { 101 };
                dataTable 
= userManager.GetDT(names, values);
                BaseLogManager.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            
catch (Exception ex)
            {
                BaseExceptionManager.LogException(dbHelper, userInfo, ex);
                
throw ex;
            }
            
finally
            {
                dbHelper.Close();
            }

            
// 写入调试信息
            #if (DEBUG)
                BaseBusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart);
            
#endif
            
return dataTable;
        }
        
#endregion

        
#region public BaseUserInfo LoginBySuid(BaseUserInfo userInfo, string suid, out string statusCode, out string statusMessage) 按唯一识别码登录
        
/// <summary>
        
/// 按唯一识别码登录
        
/// </summary>
        
/// <param name="userInfo">用户</param>
        
/// <param name="suid">唯一识别码</param>
        
/// <param name="statusCode">返回状态码</param>
        
/// <param name="statusMessage">返回状消息</param>
        
/// <returns>用户实体</returns>
        public BaseUserInfo LoginBySuid(BaseUserInfo userInfo, string suid, out string statusCode, out string statusMessage)
        {
            
// 写入调试信息
            #if (DEBUG)
                
int milliStart = BaseBusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
            
#endif

            BaseUserInfo returnUserInfo 
= null;
            statusCode 
= string.Empty;
            statusMessage 
= string.Empty;
            
            IDbHelper dbHelper 
= DbHelperFactory.GetHelper();
            
try
            {
                dbHelper.Open();
                BaseUserManager userManager 
= new BaseUserManager(dbHelper, userInfo);
                
// 先侦测是否在线
                userManager.CheckOnLine();
                
// 再进行登录
                returnUserInfo = userManager.LoginBySuid(suid, userInfo.IPAddress, userInfo.MACAddress, out statusCode);
                statusMessage 
= userManager.GetStateMessage(statusCode);
                
// 登录时会自动记录进行日志记录,所以不需要进行重复日志记录
                
// BaseLogManager.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            
catch (Exception ex)
            {
                BaseExceptionManager.LogException(dbHelper, userInfo, ex);
                
throw ex;
            }
            
finally
            {
                dbHelper.Close();
            }

            
// 写入调试信息
            #if (DEBUG)
                BaseBusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart);
            
#endif
            
return returnUserInfo;
        }
        
#endregion

        
#region public BaseUserInfo LoginByUserName(BaseUserInfo userInfo, string userName, out string statusCode, out string statusMessage) 按用户名登录
        
/// <summary>
        
/// 按用户名登录
        
/// </summary>
        
/// <param name="userInfo">用户</param>
        
/// <param name="userName">用户名</param>
        
/// <param name="statusCode">返回状态码</param>
        
/// <param name="statusMessage">返回状消息</param>
        
/// <returns>用户实体</returns>
        public BaseUserInfo LoginByUserName(BaseUserInfo userInfo, string userName, out string statusCode, out string statusMessage)
        {
            
// 写入调试信息
            #if (DEBUG)
                
int milliStart = BaseBusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
            
#endif

            BaseUserInfo returnUserInfo 
= null;
            statusCode 
= string.Empty;
            statusMessage 
= string.Empty;
            
            IDbHelper dbHelper 
= DbHelperFactory.GetHelper();
            
try
            {
                dbHelper.Open();
                BaseUserManager userManager 
= new BaseUserManager(dbHelper, userInfo);
                
// 先侦测是否在线
                userManager.CheckOnLine();
                
// 再进行登录
                returnUserInfo = userManager.LoginByUserName(userName, userInfo.IPAddress, userInfo.MACAddress, out statusCode);
                statusMessage 
= userManager.GetStateMessage(statusCode);
                
// 登录时会自动记录进行日志记录,所以不需要进行重复日志记录
                
// BaseLogManager.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            
catch (Exception ex)
            {
                BaseExceptionManager.LogException(dbHelper, userInfo, ex);
                
throw ex;
            }
            
finally
            {
                dbHelper.Close();
            }

            
// 写入调试信息
            #if (DEBUG)
                BaseBusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart);
            
#endif
            
return returnUserInfo;
        }
        
#endregion

        
#region public BaseUserInfo UserLogin(BaseUserInfo userInfo, string userName, string password, out string statusCode, out string statusMessage) 用户登录
        
/// <summary>
        
/// 用户登录
        
/// </summary>
        
/// <param name="userInfo">用户</param>
        
/// <param name="userName">用户名</param>
        
/// <param name="password">密码</param>
        
/// <param name="ipAddress">IP地址</param>
        
/// <param name="statusCode">返回状态码</param>
        
/// <param name="statusMessage">返回状消息</param>
        
/// <returns>用户实体</returns>
        public BaseUserInfo UserLogin(BaseUserInfo userInfo, string userName, string password, out string statusCode, out string statusMessage)
        {
            
// 写入调试信息
            #if (DEBUG)
                
int milliStart = BaseBusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
            
#endif

            BaseUserInfo returnUserInfo 
= null;
            statusCode 
= string.Empty;
            statusMessage 
= string.Empty;
            
            IDbHelper dbHelper 
= DbHelperFactory.GetHelper();
            
try
            {
                dbHelper.Open();
                BaseUserManager userManager 
= new BaseUserManager(dbHelper, userInfo);
                
// 先侦测是否在线
                userManager.CheckOnLine();
                
// 再进行登录
                returnUserInfo = userManager.Login(userName, password, userInfo.IPAddress, userInfo.MACAddress, out statusCode);
                
if (returnUserInfo != null)
                {
                    PermissionService permissionService 
= new PermissionService();
                    returnUserInfo.IsAdministrator 
= permissionService.IsAdministratorByUser(dbHelper, userInfo, returnUserInfo.Id);
                }
                statusMessage 
= userManager.GetStateMessage(statusCode);
                
// 登录时会自动记录进行日志记录,所以不需要进行重复日志记录
                
// BaseLogManager.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            
catch (Exception ex)
            {
                BaseExceptionManager.LogException(dbHelper, userInfo, ex);
                
throw ex;
            }
            
finally
            {
                dbHelper.Close();
            }

            
// 写入调试信息
            #if (DEBUG)
                BaseBusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart);
            
#endif
            
return returnUserInfo;
        }
        
#endregion

        
#region public void OnExit(BaseUserInfo userInfo) 用户退出应用程序
        
/// <summary>
        
/// 用户退出应用程序
        
/// </summary>
        
/// <param name="userInfo">用户</param>
        public void OnExit(BaseUserInfo userInfo)
        {
            
// 写入调试信息
            #if (DEBUG)
                
int milliStart = BaseBusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
            
#endif

            IDbHelper dbHelper 
= DbHelperFactory.GetHelper();
            
try
            {
                dbHelper.Open();
                BaseLogManager.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
                BaseUserManager userManager 
= new BaseUserManager(dbHelper, userInfo);
                userManager.OnExit(userInfo.Id);
            }
            
catch (Exception ex)
            {
                BaseExceptionManager.LogException(dbHelper, userInfo, ex);
                
throw ex;
            }
            
finally
            {
                dbHelper.Close();
            }

            
// 写入调试信息
            #if (DEBUG)
                BaseBusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart);
            
#endif
        }
        
#endregion

        
#region public int ServerCheckOnLine() 服务器端检查在线状态
        
/// <summary>
        
/// 服务器端检查在线状态
        
/// </summary>
        
/// <returns>离线人数</returns>
        public int ServerCheckOnLine()
        {
            
int returnValue = 0;
            IDbHelper dbHelper 
= DbHelperFactory.GetHelper();
            
try
            {
                dbHelper.Open();
                BaseUserManager userManager 
= new BaseUserManager(dbHelper);
                returnValue 
= userManager.CheckOnLine();
            }
            
catch (Exception ex)
            {
                BaseInterfaceLogic.WriteException(ex);
                
throw ex;
            }
            
finally
            {
                dbHelper.Close();
            }
            
return returnValue;
        }
        
#endregion

        
#region public int CheckOnLine(BaseUserInfo userInfo) 检查在线状态
        
/// <summary>
        
/// 检查在线状态
        
/// </summary>
        
/// <param name="userInfo">用户</param>
        
/// <returns>离线人数</returns>
        public int CheckOnLine(BaseUserInfo userInfo)
        {
            
// 写入调试信息
            #if (DEBUG)
                
int milliStart = BaseBusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
            
#endif
            
int returnValue = 0;

            IDbHelper dbHelper 
= DbHelperFactory.GetHelper();
            
try
            {
                dbHelper.Open();
                BaseUserManager userManager 
= new BaseUserManager(dbHelper);
                
// 设置为在线状态
                userManager.OnLine(userInfo.Id);
                returnValue 
= userManager.CheckOnLine();
            }
            
catch (Exception ex)
            {
                BaseInterfaceLogic.WriteException(ex);
                
throw ex;
            }
            
finally
            {
                dbHelper.Close();
            }

            
// 写入调试信息
            #if (DEBUG)
                BaseBusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart);
            
#endif
            
return returnValue;
        }
        
#endregion

        
#region public DataTable GetOnLineState(BaseUserInfo userInfo) 获取在线用户列表
        
/// <summary>
        
/// 获取在线用户列表
        
/// </summary>
        
/// <param name="userInfo">用户</param>
        
/// <returns>数据表</returns>
        public DataTable GetOnLineState(BaseUserInfo userInfo)
        {
            
// 写入调试信息
            #if (DEBUG)
                
int milliStart = BaseBusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
            
#endif
            DataTable dataTable 
= new DataTable();

            IDbHelper dbHelper 
= DbHelperFactory.GetHelper();
            
try
            {
                dbHelper.Open();
                BaseUserManager userManager 
= new BaseUserManager(dbHelper, userInfo);
                
if (MessageService.LaseCheck == DateTime.MinValue)
                {
                    
// 设置为在线状态
                    userManager.OnLine(userInfo.Id);
                    
// 检查用户在线状态(服务器专用)
                    userManager.CheckOnLine();
                    MessageService.LaseCheck 
= DateTime.Now;
                }
                
else
                {
                    
// 2008.01.23 JiRiGaLa 修正错误
                    TimeSpan timeSpan = DateTime.Now - MessageService.LaseCheck;
                    
if ((timeSpan.Minutes * 60 + timeSpan.Seconds) >= BaseSystemInfo.OnLineCheck)
                    {
                        
// 设置为在线状态
                        userManager.OnLine(userInfo.Id);
                        
// 检查用户在线状态(服务器专用)
                        userManager.CheckOnLine();
                        MessageService.LaseCheck 
= DateTime.Now;
                    }
                }
                
// 获取在线状态列表
                dataTable = userManager.GetOnLineStateDT();
                
// BaseLogManager.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            
catch (Exception ex)
            {
                BaseExceptionManager.LogException(dbHelper, userInfo, ex);
                
throw ex;
            }
            
finally
            {
                dbHelper.Close();
            }

            
// 写入调试信息
            #if (DEBUG)
                BaseBusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart);
            
#endif
            
return dataTable;
        }
        
#endregion

        
#region public int SetPassword(BaseUserInfo userInfo, string[] userIds, string password, out string statusCode, out string statusMessage) 设置密码
        
/// <summary>
        
/// 设置密码
        
/// </summary>
        
/// <param name="userInfo">用户</param>
        
/// <param name="userId">被设置的职员主键</param>
        
/// <param name="password">新密码</param>
        
/// <param name="statusCode">返回状态码</param>
        
/// <param name="statusMessage">返回状消息</param>
        
/// <returns>影响行数</returns>
        public int SetPassword(BaseUserInfo userInfo, string[] userIds, string password, out string statusCode, out string statusMessage)
        {
            
// 写入调试信息
            #if (DEBUG)
                
int milliStart = BaseBusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
            
#endif
            statusCode 
= string.Empty;
            statusMessage 
= string.Empty;
            
int returnValue = 0;

            IDbHelper dbHelper 
= DbHelperFactory.GetHelper();
            
try
            {
                dbHelper.Open();
                BaseLogManager.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
                BaseUserManager userManager 
= new BaseUserManager(dbHelper, userInfo);
                returnValue 
= userManager.BatchSetPassword(userIds, password, out statusCode);
                
// 获得状态消息
                statusMessage = userManager.GetStateMessage(statusCode);
            }
            
catch (Exception ex)
            {
                BaseExceptionManager.LogException(dbHelper, userInfo, ex);
                
throw ex;
            }
            
finally
            {
                dbHelper.Close();
            }

            
// 写入调试信息
            #if (DEBUG)
                BaseBusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart);
            
#endif
            
return returnValue;
        }
        
#endregion
        
        
#region public int ChangePassword(BaseUserInfo userInfo, string oldPassword, string newPassword, out string statusCode, out string statusMessage) 修改密码
        
/// <summary>
        
/// 修改密码
        
/// </summary>
        
/// <param name="userInfo">用户</param>
        
/// <param name="oldPassword">原始密码</param>
        
/// <param name="newPassword">新密码</param>
        
/// <param name="statusCode">返回状态码</param>
        
/// <param name="statusMessage">返回状消息</param>
        
/// <returns>影响行数</returns>
        public int ChangePassword(BaseUserInfo userInfo, string oldPassword, string newPassword, out string statusCode, out string statusMessage)
        {
            
// 写入调试信息
            #if (DEBUG)
                
int milliStart = BaseBusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
            
#endif
            statusCode 
= string.Empty;
            statusMessage 
= string.Empty;
            
int returnValue = 0;

            IDbHelper dbHelper 
= DbHelperFactory.GetHelper();
            
try
            {
                dbHelper.Open();
                
// 事务开始
                
// dbHelper.BeginTransaction();
                BaseLogManager.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
                BaseUserManager userManager 
= new BaseUserManager(dbHelper, userInfo);
                returnValue 
= userManager.ChangePassword(oldPassword, newPassword, out statusCode);
                
// 获得状态消息
                statusMessage = userManager.GetStateMessage(statusCode);
                
// 事务递交
                
// dbHelper.CommitTransaction();
            }
            
catch (Exception ex)
            {
                
// 事务回滚
                
// dbHelper.RollbackTransaction();
                BaseExceptionManager.LogException(dbHelper, userInfo, ex);
                
throw ex;
            }
            
finally
            {
                dbHelper.Close();
            }

            
// 写入调试信息
            #if (DEBUG)
                BaseBusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart);
            
#endif
            
return returnValue;
        }
        
#endregion
    }
}

 

 

WCF 客户端程序注意事项

1:如何不用一个个引用,用程序方式,实现各服务的灵活调用?

2:返回值的长度限制问题解决、如何进行设置?

3:客户端多个endpoint的定义如何设置?最少的配置文件写法?

 

以下是客户端的配置文件参考

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  
<system.serviceModel>
    
<client>
      
<endpoint address="http://localhost:8888/DotNet.Service/BusinessCardService/" binding="basicHttpBinding" bindingConfiguration="HTTP" contract="DotNet.IService.IBusinessCardService"  name="DotNet.Service.BusinessCardService"></endpoint>
      
<endpoint address="http://localhost:8888/DotNet.Service/ExceptionService/" binding="basicHttpBinding" bindingConfiguration="HTTP" contract="DotNet.IService.IExceptionService" name="DotNet.Service.ExceptionService"></endpoint>
      
<endpoint address="http://localhost:8888/DotNet.Service/FileService/" binding="basicHttpBinding" bindingConfiguration="HTTP" contract="DotNet.IService.IFileService" name="DotNet.Service.FileService"></endpoint>
      
<endpoint address="http://localhost:8888/DotNet.Service/FolderService/" binding="basicHttpBinding" bindingConfiguration="HTTP" contract="DotNet.IService.IFolderService" name="DotNet.Service.FolderService"></endpoint>
      
<endpoint address="http://localhost:8888/DotNet.Service/ItemDetailsService/" binding="basicHttpBinding" bindingConfiguration="HTTP" contract="DotNet.IService.IItemDetailsService" name="DotNet.Service.ItemDetailsService"></endpoint>
      
<endpoint address="http://localhost:8888/DotNet.Service/ItemsService/" binding="basicHttpBinding" bindingConfiguration="HTTP" contract="DotNet.IService.IItemsService" name="DotNet.Service.ItemsService"></endpoint>
      
<endpoint address="http://localhost:8888/DotNet.Service/LoginService/" binding="basicHttpBinding" bindingConfiguration="HTTP" contract="DotNet.IService.ILoginService" name="DotNet.Service.LoginService"></endpoint>
      
<endpoint address="http://localhost:8888/DotNet.Service/LogService/" binding="basicHttpBinding" bindingConfiguration="HTTP" contract="DotNet.IService.ILogService" name="DotNet.Service.LogService"></endpoint>
      
<endpoint address="http://localhost:8888/DotNet.Service/MessageService/" binding="basicHttpBinding" bindingConfiguration="HTTP" contract="DotNet.IService.IMessageService" name="DotNet.Service.MessageService"></endpoint>
      
<endpoint address="http://localhost:8888/DotNet.Service/ModuleService/" binding="basicHttpBinding" bindingConfiguration="HTTP" contract="DotNet.IService.IModuleService" name="DotNet.Service.ModuleService"></endpoint>
      
<endpoint address="http://localhost:8888/DotNet.Service/OrganizeService/" binding="basicHttpBinding" bindingConfiguration="HTTP" contract="DotNet.IService.IOrganizeService" name="DotNet.Service.OrganizeService"></endpoint>
      
<endpoint address="http://localhost:8888/DotNet.Service/ParameterService/" binding="basicHttpBinding" bindingConfiguration="HTTP" contract="DotNet.IService.IParameterService" name="DotNet.Service.ParameterService"></endpoint>
      
<endpoint address="http://localhost:8888/DotNet.Service/PermissionAdminService/" binding="basicHttpBinding" bindingConfiguration="HTTP" contract="DotNet.IService.IPermissionAdminService" name="DotNet.Service.PermissionAdminService"></endpoint>
      
<endpoint address="http://localhost:8888/DotNet.Service/PermissionService/" binding="basicHttpBinding" bindingConfiguration="HTTP" contract="DotNet.IService.IPermissionService" name="DotNet.Service.PermissionService"></endpoint>
      
<endpoint address="http://localhost:8888/DotNet.Service/RoleService/" binding="basicHttpBinding" bindingConfiguration="HTTP" contract="DotNet.IService.IRoleService" name="DotNet.Service.RoleService"></endpoint>
      
<endpoint address="http://localhost:8888/DotNet.Service/SequenceService/" binding="basicHttpBinding" bindingConfiguration="HTTP" contract="DotNet.IService.ISequenceService" name="DotNet.Service.SequenceService"></endpoint>
      
<endpoint address="http://localhost:8888/DotNet.Service/StaffService/" binding="basicHttpBinding" bindingConfiguration="HTTP" contract="DotNet.IService.IStaffService" name="DotNet.Service.StaffService"></endpoint>
      
<endpoint address="http://localhost:8888/DotNet.Service/UserService/" binding="basicHttpBinding" bindingConfiguration="HTTP" contract="DotNet.IService.IUserService" name="DotNet.Service.UserService"></endpoint>
      
<endpoint address="http://localhost:8888/DotNet.Service/WorkReportService/" binding="basicHttpBinding" bindingConfiguration="HTTP" contract="DotNet.IService.IWorkReportService" name="DotNet.Service.WorkReportService"></endpoint>
    
</client>
    
<bindings>
      
<basicHttpBinding>
        
<binding name="HTTP" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647">
        
</binding>
      
</basicHttpBinding>
    
</bindings>
  
</system.serviceModel>
</configuration>

 

 

以下是客户端调用服务器端代码的实现参考

//------------------------------------------------------------
// All Rights Reserved , Copyright (C) 2009 , Jirisoft , Ltd. 
//------------------------------------------------------------

using System.ServiceModel;

namespace DotNet.WCFClient
{
    
using DotNet.IService;

    
/// <summary>
    
/// ServiceFactory
    
/// 本地服务的具体实现接口
    
/// 
    
/// 修改纪录
    
/// 
    
///        2009.09.20 版本:1.0 JiRiGaLa 创建。
    
///        
    
/// 版本:1.0
    
///
    
/// <author>
    
///        <name>JiRiGaLa</name>
    
///        <date>2009.09.20</date>
    
/// </author> 
    
/// </summary>
    public class ServiceFactory : IServiceFactory
    {
        
public ISequenceService CreateSequenceService()
        {
            ChannelFactory
<ISequenceService> channelFactory = new ChannelFactory<ISequenceService>("DotNet.Service.SequenceService");
            ISequenceService proxy 
= channelFactory.CreateChannel();
            
return proxy;
        }

        
public IUserService CreateUserService()
        {
            ChannelFactory
<IUserService> channelFactory = new ChannelFactory<IUserService>("DotNet.Service.UserService");
            IUserService proxy 
= channelFactory.CreateChannel();
            
return proxy;
        }

        
public ILoginService CreateLoginService()
        {
            ChannelFactory
<ILoginService> channelFactory = new ChannelFactory<ILoginService>("DotNet.Service.LoginService");
            ILoginService proxy 
= channelFactory.CreateChannel();
            
return proxy;
        }

        
public ILogService CreateLogService()
        {
            ChannelFactory
<ILogService> channelFactory = new ChannelFactory<ILogService>("DotNet.Service.LogService");
            ILogService proxy 
= channelFactory.CreateChannel();
            
return proxy;
        }

        
public IExceptionService CreateExceptionService()
        {
            ChannelFactory
<IExceptionService> channelFactory = new ChannelFactory<IExceptionService>("DotNet.Service.ExceptionService");
            IExceptionService proxy 
= channelFactory.CreateChannel();
            
return proxy;
        }

        
public IPermissionAdminService CreatePermissionAdminService()
        {
            ChannelFactory
<IPermissionAdminService> channelFactory = new ChannelFactory<IPermissionAdminService>("DotNet.Service.PermissionAdminService");
            IPermissionAdminService proxy 
= channelFactory.CreateChannel();
            
return proxy;
        }

        
public IOrganizeService CreateOrganizeService()
        {
            ChannelFactory
<IOrganizeService> channelFactory = new ChannelFactory<IOrganizeService>("DotNet.Service.OrganizeService");
            IOrganizeService proxy 
= channelFactory.CreateChannel();
            
return proxy;
        }

        
public IItemsService CreateItemsService()
        {
            ChannelFactory
<IItemsService> channelFactory = new ChannelFactory<IItemsService>("DotNet.Service.ItemsService");
            IItemsService proxy 
= channelFactory.CreateChannel();
            
return proxy;
        }

        
public IItemDetailsService CreateItemDetailsService()
        {
            ChannelFactory
<IItemDetailsService> channelFactory = new ChannelFactory<IItemDetailsService>("DotNet.Service.ItemDetailsService");
            IItemDetailsService proxy 
= channelFactory.CreateChannel();
            
return proxy;
        }

        
public IModuleService CreateModuleService()
        {
            ChannelFactory
<IModuleService> channelFactory = new ChannelFactory<IModuleService>("DotNet.Service.ModuleService");
            IModuleService proxy 
= channelFactory.CreateChannel();
            
return proxy;
        }

        
public IStaffService CreateStaffService()
        {
            ChannelFactory
<IStaffService> channelFactory = new ChannelFactory<IStaffService>("DotNet.Service.StaffService");
            IStaffService proxy 
= channelFactory.CreateChannel();
            
return proxy;
        }

        
public IRoleService CreateRoleService()
        {
            ChannelFactory
<IRoleService> channelFactory = new ChannelFactory<IRoleService>("DotNet.Service.RoleService");
            IRoleService proxy 
= channelFactory.CreateChannel();
            
return proxy;
        }

        
public IMessageService CreateMessageService()
        {
            ChannelFactory
<IMessageService> channelFactory = new ChannelFactory<IMessageService>("DotNet.Service.MessageService");
            IMessageService proxy 
= channelFactory.CreateChannel();
            
return proxy;
        }

        
public IFileService CreateFileService()
        {
            ChannelFactory
<IFileService> channelFactory = new ChannelFactory<IFileService>("DotNet.Service.FileService");
            IFileService proxy 
= channelFactory.CreateChannel();
            
return proxy;
        }

        
public IFolderService CreateFolderService()
        {
            ChannelFactory
<IFolderService> channelFactory = new ChannelFactory<IFolderService>("DotNet.Service.FolderService");
            IFolderService proxy 
= channelFactory.CreateChannel();
            
return proxy;
        }

        
public IParameterService CreateParameterService()
        {
            ChannelFactory
<IParameterService> channelFactory = new ChannelFactory<IParameterService>("DotNet.Service.ParameterService");
            IParameterService proxy 
= channelFactory.CreateChannel();
            
return proxy;
        }

        
public IPermissionService CreatePermissionService()
        {
            ChannelFactory
<IPermissionService> channelFactory = new ChannelFactory<IPermissionService>("DotNet.Service.PermissionService");
            IPermissionService proxy 
= channelFactory.CreateChannel();
            
return proxy;
        }

        
public IBusinessCardService CreateBusinessCardService()
        {
            ChannelFactory
<IBusinessCardService> channelFactory = new ChannelFactory<IBusinessCardService>("DotNet.Service.BusinessCardService");
            IBusinessCardService proxy 
= channelFactory.CreateChannel();
            
return proxy;
        }
    }
}

 

程序的运行效果如下图:

 

 

以上的写法核心解决的问题有:

WCF服务器端、WCF客户端配置文件的正确写法。

多个服务的定义及调用方法。

用程序、接口的方式调用WCF服务,减少命名空间不同,无法正常调用远程WCF服务的问题,这样程序用最少的改动量,支持WCF技术了。

只要入门了,接下就是肯投入多少时间,在于研究什么细节问题了,铺平了入门的道路了,进入了轨道了,发展就快了。

 

将权限管理、工作流管理做到我能力的极致,一个人只能做好那么很少的几件事情。

posted on 2010-06-09 11:03 吉日嘎拉 不仅权通用权限 阅读(460) 评论(6) 编辑 收藏

评论

#1楼  回复 引用 查看    大牛,用MarshalByRefObject的用意是??

2010-06-09 11:15 | a bird      

#2楼[楼主]  回复 引用 查看   

@a bird

这个程序,也支持 Remoting 的服务,
System.MarshalByRefObject 是 Remoting 里需要的东西。
2010-06-09 11:21 | 吉日嘎拉 不仅权限管理      

#3楼  回复 引用 查看   

代码太多了
2010-06-09 16:03 | 伊牛娃      

#4楼[楼主]  回复 引用 查看   

@伊牛娃

别人都说我,代码太少了,这次就多贴了一些出来了。
2010-06-09 16:08 | 吉日嘎拉 不仅权限管理      

#5楼  回复 引用 查看   

推荐博客里面好像没有你的名字,不知道为什么?
原创粉丝点击