用wcf实现跨平台通信

本文介绍了如何利用WCF服务在一台Win2003系统、一台Win10系统笔记本和一部华为Mate20 Pro手机之间实现跨平台通信。首先创建了一个名为IQueryFile的WCF服务库,然后在Win2003上作为宿主运行。在Visual Studio 2019中,使用Xamarin构建了Android客户端,通过Nuget包下载Newtonsoft.Json。由于Xamarin的.NET Core与.NET Framework不兼容,作者创建了一个RemoteFile模型,并使用RestService进行通信。最终,在正确配置端口和网络连接后,实现了快速的跨设备通信。
摘要由CSDN通过智能技术生成

一台桌面电脑,系统Win2003,一个笔记本电脑,win10系统,一部华为mate20 pro手机,准备在这三端通信,宿主放在Win2003上,客户端放在笔记本电脑和安卓手机上。

先建一个WCF服务库,将IService改名为IQueryFile,Service改名为QueryFile。

IQueryFile内容是这样

    [ServiceContract]
    public interface IQueryFile
    {
        [OperationContract]
        [WebGet(UriTemplate=@"/mydocs",ResponseFormat =WebMessageFormat.Json)]
        RemoteFile[] GetMydocuments();

        // TODO: 在此添加您的服务操作
        [OperationContract]
        [WebInvoke(UriTemplate=@"/files",RequestFormat =WebMessageFormat.Json, ResponseFormat =WebMessageFormat.Json,Method ="POST")]
        RemoteFile[] GetFiles(string path);
    }

    // 使用下面示例中说明的数据约定将复合类型添加到服务操作。
    public class RemoteFile
    {
        public string Name { get; set; }
        public DateTime FileTime { get; set; }
    }

QueryFile.cs内容

[ServiceBehavior(InstanceContextMode =InstanceContextMode.Single)]
    public class QueryFile : IQueryFile
    {
        public RemoteFile[] GetFiles(string path)
        {
            RemoteFile[] remoteFiles = GetRemoteFiles(path);

            if (remoteFiles.Length > 0)
            {
                WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.OK;
            }
            else
            {
                WebOperationContext.Current.OutgoingResponse.SetStatusAsNotFound();
            }
            return remoteFiles;
        }

        public RemoteFile[] GetMydocuments()
        {
            RemoteFile[] remoteFiles= GetRemoteFiles(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments));

            if (remoteFiles.Length > 0)
            {
                WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.OK;
            }
            else
            {
                WebOperationContext.Current.OutgoingResponse.SetStatusAsNotFound();
            }

            return remoteFiles;
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值