You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Xamarin Forms 开发中支持服务端向客户端传多个复杂类型参数的服务选型咨询

Xamarin 跨端服务传参开发问题

开发基于 C#+Xamarin 的移动应用时,需要实现服务端向客户端传输多类复杂结构参数的能力。多日查阅各类技术文档、论坛内容后,仍未解决全端链路打通的问题,具体尝试过程与卡点如下:

已尝试的技术方案与卡点

  • 第一套方案为 WCF RESTful 实现:按常规认知 RESTful 接口基于 URL 传递参数,仅支持字符串类型,无法适配多复杂结构参数的传输需求,因此放弃该方向。
  • 第二套方案为纯 WCF 实现:Android 端功能运行完全正常,但 iOS 端直接抛出运行报错:MonoTouch does not support dynamic proxy code generation. Override this method or its caller to return specific client proxy instance.
  • 针对 iOS 端的报错参照公开的同类问题解决思路修改代码后,又触发 CreateChannel() 相关的执行错误,链路始终无法全端跑通。

核心诉求

  • 探索在 WCF 或 REST 技术框架下,兼容 Xamarin.iOS 运行环境、实现多复杂结构参数稳定传输的可行方案
  • 若上述两类框架无法满足适配需求,寻找可稳定支持 Xamarin.iOS 客户端接收服务端多复杂类型参数的替代服务实现方案

关联代码片段

复杂类型数据结构定义

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);
            
            return base.ToString();
        }
    }
}

注:原提供的代码片段存在实体字符转义、多余注释闭合标记等排版问题,已按C#语法规范还原为可直接阅读的格式。

服务实现层代码(MobileService.cs)

public List<ConcretePayDeskBaseCollection> ListPayDeskBasedCollections(string userName, string password, ConcreteCollectionDetailQueryCriteria collectionDetailQueryCriteria)
{
    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));
    }
}

服务接口定义(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);
}

内容的提问来源于stack exchange,提问作者Veysel Turan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.28 18:06:06