Home > OS >  WCF Service and Windows Application Client. why client receives null
WCF Service and Windows Application Client. why client receives null

Time:11-10

I have mapped the service with a linq to Sql classes and I am using wcf library for vs 2019 and in client a win form app. I am trying sending the class created for linq to sql the next way

    public List<Trades> GetAllTradings()
    {
        context = new DCStockTradingDataContext();

        List<Trades> tradings = (from t in context.Trades
                        select t).ToList();
        
        return tradings;
    }

and the client

   private void btnRun_Click(object sender, EventArgs e)
    {
        
        Service1Client client = new Service1Client();
        var trades = client.GetAllTradings();
        
        dgViewStocks.DataSource = trades;
        //string ret = client.GetData("Hello");
        //Console.WriteLine(ret);
    }

I din´t know what is happening and I don´t know what is wrong

The service

enter image description here

and the client receives all null

enter image description here

I would appreciate any help about this and thank you in advance

CodePudding user response:

If you get null response in WCF client, some advices:

  • try to call the service in SoapUi with Validation of request and response turned on. It may be useful in detecting problems.
  • debug Reference.cs file to see more closely what is going on
  • use MessageInspector to see what is received in AfterReceiveReply method
  • examine namespaces attentively, response often cannot be deserialized because of the difference in namespaces that are in Reference.cs and in real service

CodePudding user response:

Thanks for your response and everything if this I did.

I found how I have to work with linq to sql classes and wcf. We need to take into account that you need to convert the List of linq classes to List<string. What I did

enter code here

    public List<string[]> GetAllStocks()
    {
        context = new DCStockTradingDataContext();

        System.Data.Linq.Table<Stocks> stocks = context.GetTable<Stocks>();
        var allStock = (from st in stocks
                        select new
                        {
                            stockId = (string) st.CodeID,
                            currency = (char) st.Currency,
                            stName = st.Name,
                            price = (decimal) st.Price,
                            volument = (decimal) st.Volumen
                        }).ToList();
        List<string[]> listRetruned = new List<string[]>();

        foreach (var tr in allStock)
        {
            string[] t = new string[5];
            t[0] =  tr.stockId;
            t[1] = tr.currency.ToString();
            t[2] = tr.stName;
            t[3] = tr.price.ToString();
            t[4] = tr.volument.ToString();
             listRetruned.Add(t);
        }
        return listRetruned;
    }

and the client

I have created a model class with the expected data

public class TradingModel
{
    public string TradeID { get; set; }
    public string StockId { get; set; }
    public decimal Bid { get; set; }
    public int BidQty { get; set; }
    public decimal Ask{get;set;}
    public int AskQty { get; set; }
    public decimal Last { get; set; }
}

and finally the method

         List<TradingModel> models = new List<TradingModel>();
       
        for(int i = 0; i < trades.Length; i  )
        {
            TradingModel model = new TradingModel()
            {
                Ask = Convert.ToDecimal(trades[i][0]),
                Bid = Convert.ToDecimal(trades[i][1]),
                BidQty = Convert.ToInt32(trades[i][2]),
                AskQty = Convert.ToInt32(trades[i][3]),
                Last = Convert.ToDecimal(trades[i][4]),
                StockId = trades[i][5],
                TradeID = trades[i][6],
            };
            models.Add(model);
    }
    dgViewStocks.DataSource = models;

I am not sure if this is the best way to jump to next step, but it worked for me. If someone is looking for this info as I did, wasting several days chasing the solution, I leave what I did.

There is off course changing the service and generate the proxy again

  • Related