Search code examples
c#wcfxamarin.formsxamarin.ioswcf-rest

Which Service can allow me to pass Multiple(ComplexType) Parameters from Service to Client For Xamarin Forms?


I know this is kind of old topic but I read all pages and forms and I've been struggling to solve my problem for days. I'm using C#-Xamarin platforms to create a mobile app. I need to pass multiple parameters from service to client. I tried WCF Resftul but As far as I know Resftul only allows to pass string type because it's based URL. Therefore I couldn't pass my multiple(complex type) parameters with Restful. And then I tried only WCF, I successed for Android, My android side works perfectly but on iOS side I got error which is "MonoTouch does not support dynamic proxy code generation. Override this method or its caller to return specific client proxy instance." , I found 2 solution for it, one of them is https://forums.xamarin.com/discussion/15148/how-to-access-wcf-service-in-ios-platform-using-xamarin ,and the second one is Monotouch/WCF: How to consume the wcf service without svcutil but then I got error about CreateChannel(). Is there any way to solve that problem in WCF or Rest? if no, Is there any service which allows me to pass multiple parameters from service to client, especially xamarin.ios?

My complexType class :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;

namespace Com.BS.AccrumentAndCollectionDefinition
{
    [DataContract]
    public class ConcreteCollectionDetailQueryCriteria
    {
        
        private long payDeskOid;
        [DataMember(IsRequired = true)]
        public long PayDeskOid
        {
            get { return payDeskOid; }
            set { payDeskOid = value; }
        }

        private DateTime collectionDateStart;
        [DataMember(IsRequired = true)]
        public DateTime CollectionDateStart
        {
            get { return collectionDateStart; }
            set { collectionDateStart = value; }
        }

        private DateTime collectionDateFinish;
        [DataMember(IsRequired = true)]
        public DateTime CollectionDateFinish
        {
            get { return collectionDateFinish; }
            set { collectionDateFinish = value; }
        }

        private string receiptSerial;
        [DataMember(IsRequired = true)]
        public string ReceiptSerial
        {
            get { return receiptSerial; }
            set { receiptSerial = value; }
        }

        private long? receiptNoStart;
        [DataMember(IsRequired = true)]
        public long? ReceiptNoStart
        {
            get { return receiptNoStart; }
            set { receiptNoStart = value; }
        }

        private long? receiptNoFinish;
        [DataMember(IsRequired = true)]
        public long? ReceiptNoFinish
        {
            get { return receiptNoFinish; }
            set { receiptNoFinish = value; }
        }

        private List<string> collectionTypeList;

        [DataMember(IsRequired = true)]
        public List<string> CollectionTypeList
        {
            get { return collectionTypeList; }
            set { collectionTypeList = value; }
        }
        }*/       
        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("PayDeskOid:").Append(payDeskOid).Append(Environment.NewLine);
            sb.Append("CollectionDateStart:").Append(collectionDateStart).Append(Environment.NewLine);
            sb.Append("CollectionDateFinish:").Append(collectionDateFinish).Append(Environment.NewLine);
            sb.Append("ReceiptSerial:").Append(receiptSerial).Append(Environment.NewLine);
            sb.Append("ReceiptNoStart:").Append(receiptNoStart).Append(Environment.NewLine);
            sb.Append("ReceiptNoFinish:").Append(receiptNoFinish).Append(Environment.NewLine);
            //sb.Append("CollectionTypeCode:").Append(collectionTypeCode).Append(Environment.NewLine);
            
            return base.ToString();
        }
    }

    
}

My MobileService.cs

public List<ConcretePayDeskBaseCollection> ListPayDeskBasedCollections(string userName, string password, ConcreteCollectionDetailQueryCriteria collectionDetailQueryCriteria)
{
    //ConcreteCollectionDetailQueryCriteria collectionDetailQueryCriteria = new ConcreteCollectionDetailQueryCriteria();
    try
    {
        ReportingOperations reportingOperations = new ReportingOperations();
        return reportingOperations.ListPayDeskBasedCollections(collectionDetailQueryCriteria);
    }
    catch (BSException e)
    {
        FileLogger.Error(CLASS_NAME, "ListPayDeskBasedCollections", e.Message, e.StackTrace, collectionDetailQueryCriteria);
        BSCommunicationException commException = new BSCommunicationException();
        commException.Id = e.Id;
        commException.ExceptionMessage = e.ExceptionMessage;
        throw new FaultException<BSCommunicationException>(commException, new FaultReason(commException.ExceptionMessage));
    }
    catch (Exception e)
    {
        FileLogger.Error(CLASS_NAME, "ListPayDeskBasedCollections", e.Message, e.StackTrace, collectionDetailQueryCriteria);
        BSCommunicationException commException = PrepareCommunicationException(e);
        throw new FaultException<BSCommunicationException>(commException, new FaultReason(commException.ExceptionMessage));
    }
}

And My Interface(IMobileService):

[ServiceContract]
public interface IMobileService
{
    [OperationContract]
    [FaultContract(typeof(BSCommunicationException))]
    [WebInvoke(Method = "POST", UriTemplate = "/ListPayDeskBasedCollections/{userName}/{password}/{collectionDetailQueryCriteria}", BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
    List<ConcretePayDeskBaseCollection> ListPayDeskBasedCollections(string userName, string password, ConcreteCollectionDetailQueryCriteria collectionDetailQueryCriteria);
}

Solution

  • Finally I got a solution!! I canceled my WCF Service because it has some limitation and also not work well enough with iOS. I came back to Web ApI thanks to https://stackoverflow.com/a/20226220/12448483 .Here the solution for me. It worked!! And now I pass complex and multi type parameters easily. By the way Passing parameters from client to service you need to use PostAsync, if you are confused to use it the you can check this https://carldesouza.com/httpclient-getasync-postasync-sendasync-c/