当前位置:网站首页>My practice of SOA architecture project based on WCF

My practice of SOA architecture project based on WCF

2022-06-13 02:49:00 afeng124

I'm not very talented , In the current project, I am both a programmer and the head of the architecture design team . In the use of WCF Before the technology, I have seen countless claims WCF Practical blog , None of them is what I want . But I've learned something . In this thanked , Thank you very much for your kind sharing and hard work . I haven't blogged for a long time , Recent research WCF I have some tips to share with you . Please also skip , I hope I won't waste your expressions . Here are some pictures , We will talk about the process later .

  • Solution screenshot

Problems encountered in the architecture process and solutions .

1、 Do you want to use dto object , Do you want to establish DTO layer ?

The solution is to skillfully combine entity objects with dto Object to merge , This eliminates the conversion process . Feeling ok .

Samples are as follows :

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;

        // One-to-many relation : role (Role) Have one or more role functions (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>
    /// System role service class
    /// </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);
        }
    }
}

Although it is not recommended to use this way , But the construction period is limited . Not used Linq, So if it is dto-->entity,entity-->entity It's going to be a dead man . For this reason, I have planned to give up using WCF, Let the client directly reference BLL and Model layer . Later, it was found that the merger could continue , That's it . Right or wrong, for the time being .

2、 How to debug and set WCF Parameters ?

Of course, it was groping , Fall down , Fall down ... Get up , Come again ! Last few pictures :

 <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. One Winnt How about the service host Multiple wcf service ?

        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");
            // Start each service
            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} Start listening Uri by :{1}",
                    serviceElement.Name, serviceElement.Endpoints[0].Address.ToString()));
                };
                serviceHost.Open();               
            }           
        }

This method is seen in brother Jiri's general permission component , I would like to express my heartfelt thanks to brother Jiri ! I hope you will respect those who are down-to-earth in technology , I'm not trying to advertise with brother Jiri . Be grateful ! You know how to do it , You despise me can , I don't envy you , I don't have to fuck you .

4、 How to use generic factory classes to simplify and refactor code ?

This is being tried , After all, I finished it first .

5、 Use winnt Service installation 、 Is it easy to publish ? Is it easy to use? ?


 /// <summary>
        /// The main entry point for the application .
        /// </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("------ Parameter description ------");
                        System.Console.WriteLine("- i Installation services !");
                        System.Console.WriteLine("- u Uninstall service !");
                        System.Console.ReadKey();
                        return;
                }
            }

            ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[]
            {
                new RtlsHostSvr()
            };
            ServiceBase.Run(ServicesToRun);

            Want to know how to debug a service program like a console program ? Put the service OnStart The method is marked public new . The default is protected override. Then remove those comments as follows , And note the above   The code of ok 了 .
            //var sHostSvr = new RtlsHostSvr();
            //sHostSvr.OnStart(null);
            //System.Threading.Thread.Sleep(10000);
            //sHostSvr.OnStop();
        }

原网站

版权声明
本文为[afeng124]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202280537409437.html