【问题标题】:C# SVC Server objects have public methods, client objects do notC# SVC 服务器对象有公共方法,客户端对象没有
【发布时间】:2016-09-26 03:29:22
【问题描述】:

我已经声明了一个名为 Authentication 的方法,这个方法接受一个 AuthenticationRequest 作为输入。所以这就是我的问题开始的地方。我已将 AuthenicationRequest 上的用户名和密码变量设置为私有,因此我使用重载的构造函数来设置它们并使用 getter 来返回它们。在我的客户端上,我试图调用Authentication(new AuthenticationRequest("","")) 但是无法识别重载的构造函数。我正在使用 C# WCF 服务。我正在使用 Visual Studio 从网址生成客户端代码。 下面我将发布我的课程的副本。我对 WCF 知之甚少,但据我了解,您在某些事情上需要 [属性]。

身份验证请求

using Classes.General;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Web;

namespace Classes.Authentication
{
    [DataContract]
    public class AuthenicationRequest : Status
    {

        [DataMember]
        private String Email, Password;


        public AuthenicationRequest(String Email, String Password)
        {
            this.Email = Email;
            this.Password = Password;
        }

        public void doWork()
        {

        }

        public String GetEmail()
        {
            return this.Email;
        }

        public String GetPassword()
        {
            return this.Password;
        }
    }
}

Authentication.svc.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using MySql.Data.MySqlClient;
using Classes.General;
using Classes.Users;
using Classes.Authentication;

namespace WebApi_Nepp_Studios.Endpoints
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Authentication" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select Authentication.svc or Authenication.svc.cs at the Solution Explorer and start debugging.
    [ServiceContract]
    public class Authentication
    {
        //Declare the MySQL variable for global databse operations
        MySqlConnection conn = new MySqlConnection(Properties.Resources.Cs);
        MySqlCommand cmd;
        MySqlDataReader reader;

        [OperationContract]
        public AuthenicationResponse Authenicate(AuthenicationRequest input)
        {
            //Blah blah blah
        }
    }
}

【问题讨论】:

  • 我会鼓励你看看这个答案stackoverflow.com/questions/6316118/…
  • 好的,这回答了我的问题,谢谢。不过,我现在遇到了另一个问题。在客户端看不到我的 DoWork 方法。
  • 贝利,我正在输入一个更全面的回复,应该也有助于解决这个问题。

标签: c# .net wcf


【解决方案1】:

你不能做你想做的事。但是,您正在生成客户端代码,它只会带来标有 [DataMember] 的内容。

查看这个答案 -> Constructor in WCF DataContract not reflected on Client

这意味着您不应该使用私有设置器。我不知道你正在做什么的规范,但一般来说,数据合约的所有属性都应该声明为:

[DataMember]
string Email { get; set; }
string Password { get; set; }

当您创建 DataContract 时,所有属性都应该是公开的,因为外部客户端将使用此数据合约来调用您的服务。除了具有公共 getter 和 setter 的公共属性之外,DataContract 不应包含任何内容。

从您的其他问题来看,您似乎不太清楚 WCF 到底应该用于什么。您当前在身份验证请求上有一个 doWork() 方法。 WCF 生成的代码不能像那样传递逻辑,它只能传递属性定义。如果您需要完成逻辑,它应该发生在 WCF 应用程序内部。

您应该将 WCF 视为 Web API,客户端向 WCF 应用程序发送请求,在本例中为身份验证请求,设置电子邮件和密码,在 WCF 应用程序内部完成处理该请求的工作,然后 WCF 应用程序发送响应。

如果以上内容不够清楚,请告诉我,我可以尝试清除它。

编辑:

DoWork() 方法可以从 DataContract 中移出,而是放入 Authentication serviceContract 的 Authenticate() 方法中。该 Authenticate() 方法将是发送请求时调用的实际方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-01
    • 2010-10-07
    • 1970-01-01
    • 2013-11-29
    • 1970-01-01
    • 1970-01-01
    • 2016-11-07
    • 2014-10-08
    相关资源
    最近更新 更多