0

I have declared a method called Authentication, this method takes an AuthenticationRequest as input. So this is where my problem starts. I have set the username and password variables on the AuthenicationRequest to private, so I am using an overloaded constructor to set them and getters to return them. On my client I am trying to call Authentication(new AuthenticationRequest("","")) however the overloaded constructor isn't recognized. I am using C# WCF services. I am using visual studio to generate the client code from a web address. Below I will post copies of my classes. I don't know a whole lot about WCF but from what I understand there are [Attributes] you need on certain things.

AuthenticationRequest

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
        }
    }
}
Bailey Miller
  • 219
  • 2
  • 12
  • I would encourage you to look at this answer http://stackoverflow.com/questions/6316118/constructor-in-wcf-datacontract-not-reflected-on-client – Bill Keller Sep 26 '16 at 03:49
  • Alright that answers my question thank you. I have run into another problem now though. My DoWork method isn't seen on the client side. – Bailey Miller Sep 26 '16 at 03:52
  • Bailey, I'm typing out a more thorough response that should help with that question as well. – Bill Keller Sep 26 '16 at 03:56

1 Answers1

0

You cannot do what you want to do. However you are generating your client-side code, it will only bring over stuff marked with [DataMember].

Check out this answer -> Constructor in WCF DataContract not reflected on Client

This means you shouldn't be using private setters. I'm not aware of the specifications of what you are making, but in general, all properties for data contracts should be declared like:

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

When you are making a DataContract, all of the properties should be public because this data contract is going to be used by external clients to call to your service. A DataContract shouldn't contain anything besides public properties with public getters and setters.

From your other questions, it sounds like you are not quite clear on exactly what WCF should be used for. You have a doWork() method on your authentication request currently. The code generated by WCF cannot pass around logic like that, it can only pass around property definitions. If you need logic done, it should occur inside of the WCF application.

You should think of WCF like a Web API, the client sends a request to the WCF application, in this case an Authentication request, setting the email and password, inside of the WCF application the work is done on processing that request, and then the WCF application sends the response.

Let me know if the above wasn't clear enough and I can try and clear it up.

Edit:

That DoWork() method could be moved out of the DataContract and instead be put into the Authenticate() method on the Authentication serviceContract. That Authenticate() method will be the actual method called when a request is sent.

Community
  • 1
  • 1
Bill Keller
  • 593
  • 1
  • 4
  • 16