0

所以在下面我有一个学生/组服务正在运行,但现在我想让两个服务运行,一个将保存“组”,另一个将保存“学生”,基本上将服务分成两部分。但是当我想将一个学生添加到一个组时,我的困惑就出现了,你是怎么做的cross service

例如,这段代码将一个学生添加到一个组中,所有这些都在一个其余的 web 服务中:

    public void AddStudentToGroup(string group, string studentID, string firstName, string lastName)
    {
        var result = Groups.Where(n => String.Equals(n.GroupName, group)).FirstOrDefault();
        var result1 = students.Where(n => String.Equals(n.StudentID, studentID)).FirstOrDefault();
        if (result != null)
        {
            result.Groupsz.Add(new Student() { StudentID = studentID, FirstName = firstName, LastName = lastName });
        }
        if (result1 != null)
        {
            result1.StudentGroup.Add(new Group() { GroupName = group });
        }

    } 

所以我的问题是,如果我将我的服务分成两个服务,一个包含组,一个包含学生,我如何实现我在上面的代码中所做的(将学生添加到组)但跨两个服务?客户端 POST 在哪里尝试实现这一目标?

我的整个一项服务看起来像下面的代码,但我将寻求取出组,只留下学生信息,并将组变成它自己的宁静 Web 服务。我只是不知道您在尝试实现上述目标时如何从一项服务与另一项服务进行通信?

using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.ServiceModel.Description;
using System.IO;
using System.Net;
using System.Drawing;
using System.Runtime.Serialization;
using System.Linq;

namespace WcfServiceLibrary1
{
    [ServiceContract(Namespace = "http://tempuri.org")]
    [XmlSerializerFormat]
    public interface IReceiveData
    {
        #region DELETE
        [OperationContract]
        [WebInvoke(Method = "DELETE", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml, UriTemplate = "/Student/{studentID}")]
        void removeStudent(string studentID);
        #endregion

        #region POST
        [OperationContract]
        [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml, UriTemplate = "/Group")]
        void AddGroup(Group group);
        [OperationContract]
        [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml, UriTemplate = "/Student")]
        void AddStudent(Student student);
        [OperationContract]
        [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml, UriTemplate = "/AddStudentToGroup/{group}/{studentID}/{firstName}/{lastName}")]
        void AddStudentToGroup(string group, string studentID, string firstName, string lastName);
        #endregion

        #region GET
        [OperationContract]
        [WebInvoke(Method = "GET", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Xml, UriTemplate = "picture/{width}/{height}")]
        Stream GetImage(string width, string height);
        [OperationContract]
        [WebInvoke(Method = "GET", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml, UriTemplate = "/Student")]
        List<Student> GetStudentCollection();
        [OperationContract] 
        [WebInvoke(Method = "GET", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml, UriTemplate = "/Group")]
        List<Group> GetGroups();
        [OperationContract]
        [WebInvoke(Method = "GET", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml, UriTemplate = "/GetStudentCollectionByGroup/{anything}")]
        List<Group> GetStudentCollectionByGroup(string anything);
        [OperationContract]
        [WebInvoke(Method = "GET", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml, UriTemplate = "/GetGroupCollectionByStudent/{groupName}")]
        List<Student> GetGroupCollectionByStudent(string groupName);
        #endregion 
    }
    #region Data Contacts
    [DataContract(Name="Student")]
    public class Student
    {
        public Student()
        {
            StudentGroup = new List<Group>();
        }
        [DataMember(Name = "StudentID")]
        public string StudentID { get; set; }
        [DataMember(Name = "FirstName")]
        public string FirstName { get; set; }
        [DataMember(Name = "LastName")]
        public string LastName { get; set; }
        [DataMember(Name = "TimeAdded")]
        public DateTime TimeAdded;
        public string TimeAddedString
        {
            get
            {
                return this.TimeAdded.ToString("dd/MM/yyyy hh:mm:ss");
            }
        }
        public List<Group> StudentGroup { get; set; }
    }
    [DataContract(Name = "Group")]
    public class Group
    {
        public Group() 
        {
            Groupsz = new List<Student>();
        }
        [DataMember(Name = "GroupName")]
        public string GroupName { get; set; }
        public List<Student> Groupsz { get; set; }
    }
    #endregion 

    #region Service work
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)] 
    public class Service : IService
    {
        #region Lists
        List<Student> students = new List<Student>();
        List<Group> Groups = new List<Group>();
        int eCount = 0;
        #endregion 

        #region Do Work
        public void AddStudent(Student student)
        {
            student.StudentID = (++eCount).ToString();
            student.TimeAdded = DateTime.Now;
            students.Add(student);
        }
        public void UpdateStudent(Student student) 
        {
            var findStudent = students.Where(s => s.StudentID == student.StudentID).FirstOrDefault();

            if (findStudent != null)
            {
                ///findStudent values with student values --- like
                findStudent.FirstName = student.FirstName;
                findStudent.LastName = student.LastName;
            }

        }
        public void AddGroup(Group group)
        {
            Groups.Add(group);
        }
        public void removeStudent(string studentID)
        {
            students.RemoveAll(s => s.StudentID == studentID);
            Groups.ForEach(g => g.Groupsz.RemoveAll(gs => gs.StudentID == studentID));
        }

    public void AddStudentToGroup(string group, string studentID, string firstName, string lastName)
    {
        var result = Groups.Where(n => String.Equals(n.GroupName, group)).FirstOrDefault();
        var result1 = students.Where(n => String.Equals(n.StudentID, studentID)).FirstOrDefault();
        if (result != null)
        {
            result.Groupsz.Add(new Student() { StudentID = studentID, FirstName = firstName, LastName = lastName });
        }
        if (result1 != null)
        {
            result1.StudentGroup.Add(new Group() { GroupName = group });
        }

    } 
        public List<Student> GetStudentCollection()
        {
            return students;
        }
        public List<Group> GetGroups()
        {
            return Groups;
        }
        public List<Student> GetGroupCollectionByStudent(string groupName)
        {
            List<Student> student = (from g in students
                                  where
                                      (from t in g.StudentGroup where t.GroupName == groupName select t).Count() > 0
                                  select g).ToList();
            return student;
        }
        public List<Group> GetStudentCollectionByGroup(string anything)
        {

            List<Group> groups = (from g in Groups
                                  where
                                      (from t in g.Groupsz where
                                              string.Equals(t.StudentID, anything, StringComparison.CurrentCultureIgnoreCase)
                                           || string.Equals(t.FirstName, anything, StringComparison.CurrentCultureIgnoreCase)
                                           || string.Equals(t.LastName, anything, StringComparison.CurrentCultureIgnoreCase)
                                       select t).Count() > 0
                                  select g).ToList();
            return groups;
        }

        #endregion
    }
}
    #endregion
4

0 回答 0