.NET疯狂架构经验分享系列之(七)WCF支持(转)

摘要:
本文仅供参考。注意1:如何编写WCF服务器端配置文件至关重要。例如,如何一起发布多个服务?项目的呈现如下:以下程序是启动WCF的程序参考service://Read配置文件Configurationconfiguration=ConfigurationManager。OpenExeConfiguration;ServiceModelSectionGroupserviceModelSectionGroup=配置。GetSectionGroup;//为每个{varserviceHost=newServiceHost;serviceHost;Opened+=delegate{Console.WriteLine;};启动每个服务;serviceHost。打开();}以下文件是WCF的服务器端配置文件:˂?

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

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

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

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

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

工程的效果图如下:

.NET疯狂架构经验分享系列之(七)WCF支持(转)第1张

以下程序是启动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;
        }
    }
}

程序的运行效果如下图:

.NET疯狂架构经验分享系列之(七)WCF支持(转)第2张 

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

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

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

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

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

http://www.cnblogs.com/jirigala/archive/2010/06/09/1754588.html

免责声明:文章转载自《.NET疯狂架构经验分享系列之(七)WCF支持(转)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇各个浏览器内核和前缀【转】VC获取当前程序文件的路径,文件名以及路径+文件名下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

tomcat及Jetty远程调试debug

服务器Tomcat配置 以 Linux 环境为例 Tomcat 安装在: /usr/program/tomcat7 Tomcat 的执行程序:/usr/program/tomcat7/bin/catalina.sh 编辑 Tomcat 执行程序:vim /usr/program/tomcat7/bin/catalina.sh 查找Tomcat的debug...

.Net编程之Web Service 和WCF的历史和特性

Web Service 的工作原理       Web Service也叫XML Web Service WebService是一种可以接收从Internet或者Intranet上的其它系统中传递过来的请求,轻量级的独立的通讯技术。是:通过SOAP在Web上提供的软件服务,使用WSDL文件进行说明,并通过UDDI进行注册。WebService可用基...

storm实时计算实例(socket实时接入)

介绍 实现了一个简单的从实时日志文件监听,写入socket服务器,再接入Storm计算的一个流程。 源码 日志监听实时写入socket服务器   [java] view plain copy  package socket;      import java.io.BufferedReader;   import java.io.File;...

利用JavaCSV API来读写csv文件

http://blog.csdn.net/loongshawn/article/details/53423121 http://javacsv.sourceforge.net/ 转载请注明来源-作者@loongshawn:http://blog.csdn.net/loongshawn/article/details/53423121 1 背景 CSV文件...

Java解析复杂xml文件,使用Xpath

Java解析复杂xml文件,需要使用到xpath,首先使用了Jdom2。代码示例如下: public static Document GetFileFromLocal(String filename){ try { FileInputStream stream=new FileInputStream(String.format("d:/x下载/%s.xm...

arm gdb调试简述

arm gdb调试简述 编译、调试工具和平台 gcc-arm-none-eabigdb-arm-none-eabi,ubantu64位;网上有很多用nfs或者通过串口连接开发机和板子进行调试;这里用qemu的gnuarmeclipse模拟stm32f429的板子。 步骤 先在你的Makeflie的编译加上-g,这样生成的ELF文件里才有调试信息,否则只...