本文介绍了在DataContract内部传递类对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个WCF服务,其中包含多个可以正常运行的OperationContracts.每个操作合同都会返回一个名为Status的DataContract,其结构如下:

I created a WCF Service with multiple OperationContracts that work fine. Every operation contract returns a DataContract called Status which is structured like so:

        [DataContract]
	public class Status
	{
		public enum Error { 
			NoError, 
			AuthTokenExpired, 
			InvalidAuthToken, 
			InvalidUsername, 
			InvalidPassword, 
			UsernameAlreadyRegistered,  
			DatabaseInsertFail, 
			DatabaseUpdateFail, 
			PasswordResetRequired,
			UnauthorizedAccess,
			DatabaseRequestFail,
			EmailServiceError,
			DatabaseDeleteFail,
			InvalidEmail,
			GeneralNetworkException,
			NotCurrentVersion
		};
		[DataMember] public Error status_code;
		[DataMember] public object content;

                public Status(Error status_code, object content)
                {
                    this.status_code = status_code;
                    this.content = content;
                }
        }

如您所见,这些字段之一只是一个通用对象,因此我可以传回任意数量的内容,并且仍然使用Status类.当我想传回基本数据类型(例如字符串,整数,浮点数等)但失败却惨不忍睹时,这种方法就可以很好地工作了 当我想回传另一堂课时.如果我自己使用与OperationContract的返回类型相同的类,则调用可以正常工作,但是以某种方式将其嵌套在DataContract中会导致问题.

As you can see one of the fields is just a generic object so that I can pass back any number of things and still use the Status class. This works beautifully when I want to pass back basic data types like string, ints, floats, etc but fails miserably when I want to pass back another class. If I use the same class as the return type for the OperationContract by itself the call works fine , but somehow having it nested inside of my DataContract causes issues.

我在客户端遇到的错误是:

The error I get on the client side is this:

收到针对的HTTP响应时发生错误 http://[为发布进行IP消毒]/DataService.svc. 服务端点绑定不使用HTTP协议.这也可能是由于服务器终止了HTTP请求上下文(可能是由于服务关闭了).有关更多详细信息,请参见服务器日志.

An error occurred while receiving the HTTP response to http://[IP Sanitized for post]/DataService.svc. This could be due to the service endpoint binding not using the HTTP protocol. This could also be due to an HTTP request context being aborted by the server (possibly due to the service shutting down). See server logs for more details. 

我本来以为它可能与请求的大小有关,所以我制作了另一个带有布尔值的假类,并且 同样的问题.然后,我尝试调整配置文件中的缓冲区大小,但仍然无法正常工作.消除过程表明,仅通过在DataMember的object字段内发送一个类,就会发生一些奇怪的事情.有类似的主题 但是它们都没有一个很明确的答案,所以除了只是将函数的返回类型设置为blah"以外,没有人对此有任何经验或有其他解决方案.最后,我将不可避免地这样做.

I originally thought it might have to do with the size of the request so I made another fake class with a boolean in it and had the same issue. Then I tried adjusting the buffer size in the config files and that still didn't work. Process of elimination would suggest there is something odd going on just by sending a class inside of an object field in a DataMember. There are similar threads but none of them have quite clear answers so does anyone have any experience with this or have a solution other than "just set the return type of that function to blah" which I will inevitably do as a last resort.

推荐答案


这篇关于在DataContract内部传递类对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 20:40