Search code examples
c#.netvisual-studiowcfdatacontract

WCF service - trying to return a class


This is my Program (client) side, and the function that tries to get a Transaction return looks like this:

private void MakeTransition_Click(object sender, RoutedEventArgs e) {
        //string CardHoldName = _CardHoldName.Text;
        //string CardBrand = _CardBrand.Text;
        //string Password = _Password.Text;
        int CardNumber = int.Parse(_CardNumber.Text);
        //int ExpirationDateM = int.Parse(_ExpirationDateM.Text);
        //int ExperiationDateY = int.Parse(_ExpirationDate_Y.Text);
        int TransationValue = int.Parse(_TransitionValue.Text);

        Transition t = new Transition();

        t.CardNumber = CardNumber;
        t.TransitionValue = TransationValue;

        HttpClient client = new HttpClient();
        string serializedContent = JsonConvert.SerializeObject(t);
        StringContent content = new StringContent(serializedContent);

        /*Transaction teste = JsonConvert.DeserializeObject<Transaction>(content.ReadAsStringAsync().Result);
        MessageBox.Show(teste.TransitionValue + "");
        MessageBox.Show(content.ReadAsStringAsync().Result);*/

        HttpResponseMessage response = client.PostAsync("http://localhost:8733/Design_Time_Addresses/HireMePleasee_Server/Service1/mex", content).Result;

        MessageBox.Show(response.Content.ReadAsStringAsync().Result);

        TransactionResponse r = JsonConvert.DeserializeObject<TransactionResponse>(response.Content.ReadAsStringAsync().Result);

        if (r.Status == "Approved") {
            MessageBox.Show("APPROVED!!!");
        } else {
            MessageBox.Show("YOU GET NOTHING! GOOD DAY, SIR!");
        }
    }
}

My Transaction class on client:

[DataContract]
public class Transaction{
    [DataMember]
    public int CardNumber { get ; set; }
    [DataMember]
    public int TransitionValue { get; set; }
}

My TransactionResponse class on client:

[DataContract]
public class TransactionResponse {
    [DataMember]
    public string Status { get; set; }
}

@@@@@@@@@@@

This is my Service with the function implemented:

public class Service1 : IService1 {

    public Transaction checkTransaction(Transaction t) {
        if (t.TransactionValue > 1500) {
            t.Status = "Denied";
        } else {
            t.Status = "Approved";
        }

        return t;
    }
}

And the ServiceContract:

[ServiceContract]
public interface IService1 {
    [OperationContract]
    Transaction checkTransaction(Transaction t);
}

[DataContract]
public class Transaction {
    [DataMember]
    public int CardNumber { get; set; }
    [DataMember]
    public int ValidationDate { get; set; }
    [DataMember]
    public int TransactionValue { get; set; }
    [DataMember]
    public string Status { get; set; }
}

################

Somehow MessageBox.Show(response.Content.ReadAsStringAsync().Result); prints nothing, which means my server-side is not processing data given from the client and returning nothing at all. I can't find anything that really helps and that's why I'm here asking for the community. What am I doing wrong?


Solution

  • First of all you can not call WCF SOAP services directly by HttpClient() class because HttpClient() is used where some data is exposed directly on url. If you want to use HttpClient to access WCF services then make your WCF service to Restful services.(Please check out what is WCF restful services).

    However if you don't want to use Proxy classes to call your WCF Soap service then then one alternative of HttpClient is to use ChannelFactory Class. The class is different from HttpClient but this is made to consume your services without making proxy for classes on WCF services.

    Please check this link to know more how to create restful services and call with HttpClient. http://sylvester-lee.blogspot.in/2013/04/wcf-rest-service-httpclient-vs-wcf.html