0

我正在尝试将 RESTful 架构添加到现有的 SOAP 服务,以便我同时拥有 Rest 和 SOAP 兼容服务。我正在使用 WebGets 和 WebInvokes 在服务接口中添加现有方法。从方法签名中可以看出,对象作为参数传递,对象从方法中返回。这就是为什么按照http://www.codeproject.com/Articles/327420/WCF-REST-Service-with-JSON我使用 Json 作为消息格式。在 chrome addon Advanced Rest Client 中有一个很好的功能,即在 POST 查询时发送 json 数据。但我无法理解的是,我系统中的客户端在调用此服务时应该如何发送 Json 数据?

这是来自客户的电话:

client.GenerateInvitation(sessionId, email, loggedInstitution);

服务接口:

[OperationContract]
        [WebInvoke( Method = "POST",ResponseFormat = WebMessageFormat.Json,
        RequestFormat = WebMessageFormat.Json,
        UriTemplate = "Rest/GenerateInvitation?ses={sessionId}&em={email}"]
        PartialInvitation GenerateInvitation(string sessionId, string email, Institution  institution);

这是数据合同:

  public partial class Institution
      {

    [DataMember]
        public System.Guid Guid { get; set; }
        [DataMember]
        public string Name { get; set; }
        [DataMember]
        public string Phonenumber { get; set; }
    }

这是一个实现的方法 GenerateInvitation:

public PartialInvitation GenerateInvitation(string sessionId, string email, Institution institution)
        {
            Session session = GetSession(sessionId);
            if (session != null)
            {
                if (((  session.User.Institutions.Where(i => i.Guid ==             institution.Guid).FirstOrDefault() != null &&
                                session.User.AccessLevel == ACCESS_LEVEL_MED
                            ) ||  session.User.AccessLevel == ACCESS_LEVEL_HIGH
                        ) && EmailTools.IsValidEmail(email) )
                {
                    List<User> usersWithMatchingEmail = db.Users.Where(u => u.Email == email).ToList();

                    foreach (User usr in usersWithMatchingEmail)
                    {
                        if (session.User.AccessLevel == ACCESS_LEVEL_MED)
                        {
                            if (usr.AccessLevel != ACCESS_LEVEL_LOW) throw new InvalidOperationException("Cannot invite non trainees");
                        }
                        if (session.User.AccessLevel == ACCESS_LEVEL_HIGH)
                        {
                            if (usr.AccessLevel == ACCESS_LEVEL_HIGH) throw new InvalidOperationException("Admin's cannot invite each other, they have full power without invitations");
                        }
                    }
                    List<Invitation> invitations = db.Invitations.Where(i => i.Email == email).ToList();
                    Invitation invitation = invitations.Where(i => i.Institution.Guid == institution.Guid).FirstOrDefault();
                    if (invitation == null)
                    {
                        invitation = new Invitation
                        {
                            Guid = Guid.NewGuid(),
                            Email = email,
                            Institution = db.Institutions.Where(i => i.Guid == institution.Guid).FirstOrDefault(),
                            InvitationCode = Convert.ToBase64String(Guid.NewGuid().ToByteArray()).Substring(0, 8).ToUpper(),
                            InvitedOn = DateTime.Now,
                            ExpiresOn = DateTime.Now.AddDays(INVITATION_EXPIRY_DAYS),
                            InvitedBy = session.User
                        };

                        while ((db.Invitations.Where(i => i.InvitationCode == invitation.InvitationCode).FirstOrDefault()) != null)
                        {
                            invitation.InvitationCode = Convert.ToBase64String(Guid.NewGuid().ToByteArray()).Substring(0, 8).ToUpper();
                        }

                        if (invitation.Institution != null)
                        {
                            db.Invitations.Add(invitation);
                            db.SaveChanges();
                            SendInvitation(invitation);
                            return PartialInvitation.FromInvitation(invitation);
                        }
                    }
                    SendInvitation(invitation);
                    return PartialInvitation.FromInvitation(invitation);
                }
            }
            return null; }
4

1 回答 1

0

使用 WCF REST 时无法生成客户端代码,因此您的客户端可能仍连接到您的 SOAP 端点。要生成 Rest 客户端,请查看以下博客:http: //blogs.msdn.com/b/pedram/archive/2008/04/21/how-to-consume-rest-services-with-wcf.aspx

于 2013-01-28T13:50:48.940 回答