热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

WCF开发(9)Unity连接WCF

参考:https:www.cnblogs.comJLZT1223p6062613.html和一般连接WCF的不同之处:1.要手动添加必要的dll2

参考:https://www.cnblogs.com/JLZT1223/p/6062613.html

和一般连接WCF的不同之处:

1.要手动添加必要的dll

2.要手动创建客户端代码(用svcutil.exe)



我用D盘的绝对路径是因为不加路径会导致无法创建文件,以管理员方式启动cmd的话应该就没问题。

System.Runtime.Serialization.dll不用添加,添加了会说已经存在。我的Unity是5.6.03f

生成文件内容:

//------------------------------------------------------------------------------
//
// 此代码由工具生成。
// 运行时版本:2.0.50727.8825
//
// 对此文件的更改可能会导致不正确的行为,并且如果
// 重新生成代码,这些更改将会丢失。
//

//------------------------------------------------------------------------------[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
[System.ServiceModel.ServiceContractAttribute(Namespace="WCFServiceLib", ConfigurationName="ICommonService")]
public interface ICommonService {[System.ServiceModel.OperationContractAttribute(Action="WCFServiceLib/ICommonService/Hello", ReplyAction="WCFServiceLib/ICommonService/HelloResponse")]string Hello(string name);[System.ServiceModel.OperationContractAttribute(Action="WCFServiceLib/ICommonService/GetString", ReplyAction="WCFServiceLib/ICommonService/GetStringResponse")]string GetString(int count, string item);[System.ServiceModel.OperationContractAttribute(Action="WCFServiceLib/ICommonService/SendString", ReplyAction="WCFServiceLib/ICommonService/SendStringResponse")]string SendString(string msg);[System.ServiceModel.OperationContractAttribute(Action="WCFServiceLib/ICommonService/GetData", ReplyAction="WCFServiceLib/ICommonService/GetDataResponse")]WCFModels.DataInfo GetData(string id, string name);[System.ServiceModel.OperationContractAttribute(Action="WCFServiceLib/ICommonService/SendData", ReplyAction="WCFServiceLib/ICommonService/SendDataResponse")]string SendData(WCFModels.DataInfo data);
}[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
public interface ICommonServiceChannel : ICommonService, System.ServiceModel.IClientChannel {
}[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
public partial class CommonServiceClient : System.ServiceModel.ClientBase, ICommonService {public CommonServiceClient() {}public CommonServiceClient(string endpointConfigurationName) : base(endpointConfigurationName) {}public CommonServiceClient(string endpointConfigurationName, string remoteAddress) : base(endpointConfigurationName, remoteAddress) {}public CommonServiceClient(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) : base(endpointConfigurationName, remoteAddress) {}public CommonServiceClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) : base(binding, remoteAddress) {}public string Hello(string name) {return base.Channel.Hello(name);}public string GetString(int count, string item) {return base.Channel.GetString(count, item);}public string SendString(string msg) {return base.Channel.SendString(msg);}public WCFModels.DataInfo GetData(string id, string name) {return base.Channel.GetData(id, name);}public string SendData(WCFModels.DataInfo data) {return base.Channel.SendData(data);}
}
namespace WCFModels {using System.Runtime.Serialization;[System.Diagnostics.DebuggerStepThroughAttribute()][System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "3.0.0.0")][System.Runtime.Serialization.DataContractAttribute(Name="DataInfo", Namespace="http://schemas.datacontract.org/2004/07/WCFModels")]public partial class DataInfo : object, System.Runtime.Serialization.IExtensibleDataObject {private System.Runtime.Serialization.ExtensionDataObject extensionDataField;private string IdField;private string NameField;private string TextField;public System.Runtime.Serialization.ExtensionDataObject ExtensionData {get {return this.extensionDataField;}set {this.extensionDataField = value;}}[System.Runtime.Serialization.DataMemberAttribute()]public string Id {get {return this.IdField;}set {this.IdField = value;}}[System.Runtime.Serialization.DataMemberAttribute()]public string Name {get {return this.NameField;}set {this.NameField = value;}}[System.Runtime.Serialization.DataMemberAttribute()]public string Text {get {return this.TextField;}set {this.TextField = value;}}}
}
脚本中测试

private void TestCommonServiceClient(){CommonServiceClient client = GetCommonServiceClient(WCFClient.Instance.Ip);client.SendString("CommonServiceClient Start");WCFModels.DataInfo data = client.GetData("1", "2");Write(string.Format("{0},{1}", data.Id, data.Name));}public static CommonServiceClient GetCommonServiceClient(string ip){NetTcpBinding binding = new NetTcpBinding(SecurityMode.None);binding.MaxReceivedMessageSize = int.MaxValue;EndpointAddress endpointAddress = new EndpointAddress("net.tcp://" + ip + ":8001/CommonService");CommonServiceClient currentClient = new CommonServiceClient(binding, endpointAddress);return currentClient;}可以

接下来试试这部分代码在Hololens中能不能正常运行。

当初的最终目标就是在Hololens中使用wcf的,因为Hololens的运行环境是UWP,所以先研究了在UWP中使用WCF,然后再通过接口的方式在脚本中使用。

这么看来可能不需要这样。

等待后续测试吧,说不定在Hololens中不能使用呢。

=======================================================

svcutil生成的代码中的 数据类型 和 Unity引用的dll中的数据类型重复了,必须手动删除那部分代码。想让svcutil只生成客户代码,添加-reference:参数,没用,还是会生成。

用VS生成的话,只要客户端项目引用了数据类型所在的dll就不会重新创建这些类型的代码了了。

但是用VS添加服务引用生成的代码中包含异步调用的代码...

将客户端项目修改为3.5的(原本是4.5的),异步调用代码就没有了。

不使用svcutil.exe,还是用VS 项目生成代码吧,再移动到Unity中好了。
==========================================================

考虑到异步处理,还是用回调函数的方式处理返回结果,做一个Unity版本的WCFClient

将UWP中的代码移动到Unity中,相应的修改代码(基本上去掉async和await就行了),语法没问题,测试。

问题1:

获取列表数据时数量没问题,但是无法获取具体数据内容。发现是因为数据类中没有添加DataContract和DataMember,但是用UWP的话可以,之前有测试过的,没加相当于全加上的,但是这样看来在这里不适用。加上合适的DataContract和DataMember就可以了,加[Serializable]没用(《WCF服务编程》里面是说可以的)。

问题2:

MainTest:System.Xml.XmlException: Text content buffer exceeds the quota limitation at 5339. 12220 bytes and should be less than 8192 bytesat System.Xml.XmlBinaryDictionaryReader.Alloc (Int32 size) [0x00000] in :0 at System.Xml.XmlBinaryDictionaryReader.ReadTextOrValue (Byte ident, System.Xml.NodeInfo node, Boolean canSkip) [0x00000] in :0 at System.Xml.XmlBinaryDictionaryReader.Read () [0x00000] in :0 at System.Xml.XmlReader.Skip () [0x00000] in :0 at System.Runtime.Serialization.SerializationMap.DeserializeContent (System.Xml.XmlReader reader, System.Runtime.Serialization.XmlFormatterDeserializer deserializer, Boolean empty) [0x00000] in :0 at System.Runtime.Serialization.SerializationMap.DeserializeContent (System.Xml.XmlReader reader, System.Runtime.Serialization.XmlFormatterDeserializer deserializer) [0x00000] in :0 at System.Runtime.Serialization.SerializationMap.DeserializeObject (System.Xml.XmlReader reader, System.Runtime.Serialization.XmlFormatterDeserializer deserializer) [0x00000] in :0 at System.Runtime.Serialization.XmlFormatterDeserializer.DeserializeByMap (System.Xml.XmlQualifiedName name, System.Type type, System.Xml.XmlReader reader) [0x00000] in :0 at System.Runtime.Serialization.XmlFormatterDeserializer.DeserializeCore (System.Type type, System.Xml.XmlReader reader) [0x00000] in :0 at System.Runtime.Serialization.XmlFormatterDeserializer.Deserialize (System.Type type, System.Xml.XmlReader reader) [0x00000] in :0 at System.Runtime.Serialization.CollectionTypeMap.DeserializeContent (System.Xml.XmlReader reader, System.Runtime.Serialization.XmlFormatterDeserializer deserializer) [0x00000] in :0 at System.Runtime.Serialization.SerializationMap.DeserializeObject (System.Xml.XmlReader reader, System.Runtime.Serialization.XmlFormatterDeserializer deserializer) [0x00000] in :0 at System.Runtime.Serialization.XmlFormatterDeserializer.DeserializeByMap (System.Xml.XmlQualifiedName name, System.Type type, System.Xml.XmlReader reader) [0x00000] in :0 at System.Runtime.Serialization.XmlFormatterDeserializer.DeserializeCore (System.Type type, System.Xml.XmlReader reader) [0x00000] in :0 at System.Runtime.Serialization.XmlFormatterDeserializer.Deserialize (System.Type type, System.Xml.XmlReader reader) [0x00000] in :0 at System.Runtime.Serialization.XmlFormatterDeserializer.Deserialize (System.Xml.XmlReader reader, System.Type type, System.Runtime.Serialization.KnownTypeCollection knownTypes, IDataContractSurrogate surrogate, System.String name, System.String ns, Boolean verifyObjectName) [0x00000] in :0 at System.Runtime.Serialization.DataContractSerializer.ReadObject (System.Xml.XmlDictionaryReader reader, Boolean verifyObjectName) [0x00000] in :0 at System.Runtime.Serialization.XmlObjectSerializer.ReadObject (System.Xml.XmlDictionaryReader reader) [0x00000] in :0 at System.ServiceModel.Dispatcher.DataContractMessagesFormatter.MessageToParts (System.ServiceModel.Description.MessageDescription md, System.ServiceModel.Channels.Message message) [0x00000] in :0 at System.ServiceModel.Dispatcher.BaseMessagesFormatter.DeserializeReply (System.ServiceModel.Channels.Message message, System.Object[] parameters) [0x00000] in :0 at System.ServiceModel.ClientRuntimeChannel.Request (System.ServiceModel.Description.OperationDescription od, System.Object[] parameters) [0x00000] in :0 at System.ServiceModel.ClientRuntimeChannel.DoProcess (System.Reflection.MethodBase method, System.String operationName, System.Object[] parameters) [0x00000] in :0 at System.ServiceModel.ClientRuntimeChannel.Process (System.Reflection.MethodBase method, System.String operationName, System.Object[] parameters) [0x00000] in :0
UnityEngine.Debug:Log(Object)
应该是反序列化时数据量太大了。是因为有一个字符串属性的字符串内容长度为12220导致的。



两个都是反序列化的问题,一般来说是没问题的(正常添加DataContract,一个字符串属性长度不要太大)。

结论:可以使用。

=====================================================================

打包成Hololens的SLN时有问题,

 1. System.ServiceModel.dll重复了,说是已经存在了。修改dll的设置,WSAPlayer不选,只在Unity中使用。


 2. 前面生成的客户端代码不能用,特性相关的dll不能使用。修改代码,整个WCF客户端代码文件用#if UNITY_EDITOR包起来,只在Unity中测试用。

虽然在Hololens中无法使用,但是不如说正好接上UWP中的版本,一个用于在Unity中测试,测试好后再打包到UWP中测试,能显著减少打包成UWP的次数。



推荐阅读
  • 本文介绍了如何使用C#制作Java+Mysql+Tomcat环境安装程序,实现一键式安装。通过将JDK、Mysql、Tomcat三者制作成一个安装包,解决了客户在安装软件时的复杂配置和繁琐问题,便于管理软件版本和系统集成。具体步骤包括配置JDK环境变量和安装Mysql服务,其中使用了MySQL Server 5.5社区版和my.ini文件。安装方法为通过命令行将目录转到mysql的bin目录下,执行mysqld --install MySQL5命令。 ... [详细]
  • 安装mysqlclient失败解决办法
    本文介绍了在MAC系统中,使用django使用mysql数据库报错的解决办法。通过源码安装mysqlclient或将mysql_config添加到系统环境变量中,可以解决安装mysqlclient失败的问题。同时,还介绍了查看mysql安装路径和使配置文件生效的方法。 ... [详细]
  • Linux如何安装Mongodb的详细步骤和注意事项
    本文介绍了Linux如何安装Mongodb的详细步骤和注意事项,同时介绍了Mongodb的特点和优势。Mongodb是一个开源的数据库,适用于各种规模的企业和各类应用程序。它具有灵活的数据模式和高性能的数据读写操作,能够提高企业的敏捷性和可扩展性。文章还提供了Mongodb的下载安装包地址。 ... [详细]
  • Go Cobra命令行工具入门教程
    本文介绍了Go语言实现的命令行工具Cobra的基本概念、安装方法和入门实践。Cobra被广泛应用于各种项目中,如Kubernetes、Hugo和Github CLI等。通过使用Cobra,我们可以快速创建命令行工具,适用于写测试脚本和各种服务的Admin CLI。文章还通过一个简单的demo演示了Cobra的使用方法。 ... [详细]
  • Imtryingtofigureoutawaytogeneratetorrentfilesfromabucket,usingtheAWSSDKforGo.我正 ... [详细]
  • Linux服务器密码过期策略、登录次数限制、私钥登录等配置方法
    本文介绍了在Linux服务器上进行密码过期策略、登录次数限制、私钥登录等配置的方法。通过修改配置文件中的参数,可以设置密码的有效期、最小间隔时间、最小长度,并在密码过期前进行提示。同时还介绍了如何进行公钥登录和修改默认账户用户名的操作。详细步骤和注意事项可参考本文内容。 ... [详细]
  • Spring源码解密之默认标签的解析方式分析
    本文分析了Spring源码解密中默认标签的解析方式。通过对命名空间的判断,区分默认命名空间和自定义命名空间,并采用不同的解析方式。其中,bean标签的解析最为复杂和重要。 ... [详细]
  • 【Windows】实现微信双开或多开的方法及步骤详解
    本文介绍了在Windows系统下实现微信双开或多开的方法,通过安装微信电脑版、复制微信程序启动路径、修改文本文件为bat文件等步骤,实现同时登录两个或多个微信的效果。相比于使用虚拟机的方法,本方法更简单易行,适用于任何电脑,并且不会消耗过多系统资源。详细步骤和原理解释请参考本文内容。 ... [详细]
  • ZSI.generate.Wsdl2PythonError: unsupported local simpleType restriction ... [详细]
  • 本文介绍了Web学习历程记录中关于Tomcat的基本概念和配置。首先解释了Web静态Web资源和动态Web资源的概念,以及C/S架构和B/S架构的区别。然后介绍了常见的Web服务器,包括Weblogic、WebSphere和Tomcat。接着详细讲解了Tomcat的虚拟主机、web应用和虚拟路径映射的概念和配置过程。最后简要介绍了http协议的作用。本文内容详实,适合初学者了解Tomcat的基础知识。 ... [详细]
  • 解决VS写C#项目导入MySQL数据源报错“You have a usable connection already”问题的正确方法
    本文介绍了在VS写C#项目导入MySQL数据源时出现报错“You have a usable connection already”的问题,并给出了正确的解决方法。详细描述了问题的出现情况和报错信息,并提供了解决该问题的步骤和注意事项。 ... [详细]
  • 在重复造轮子的情况下用ProxyServlet反向代理来减少工作量
    像不少公司内部不同团队都会自己研发自己工具产品,当各个产品逐渐成熟,到达了一定的发展瓶颈,同时每个产品都有着自己的入口,用户 ... [详细]
  • imx6ull开发板驱动MT7601U无线网卡的方法和步骤详解
    本文详细介绍了在imx6ull开发板上驱动MT7601U无线网卡的方法和步骤。首先介绍了开发环境和硬件平台,然后说明了MT7601U驱动已经集成在linux内核的linux-4.x.x/drivers/net/wireless/mediatek/mt7601u文件中。接着介绍了移植mt7601u驱动的过程,包括编译内核和配置设备驱动。最后,列举了关键词和相关信息供读者参考。 ... [详细]
  • r2dbc配置多数据源
    R2dbc配置多数据源问题根据官网配置r2dbc连接mysql多数据源所遇到的问题pom配置可以参考官网,不过我这样配置会报错我并没有这样配置将以下内容添加到pom.xml文件d ... [详细]
  • 本文介绍了如何清除Eclipse中SVN用户的设置。首先需要查看使用的SVN接口,然后根据接口类型找到相应的目录并删除相关文件。最后使用SVN更新或提交来应用更改。 ... [详细]
author-avatar
mmakarlen
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有