【问题标题】:Using Lists in webservice在 Web 服务中使用列表
【发布时间】:2012-02-22 10:52:29
【问题描述】:

我正在制作一个 web 服务(由于 .net req 无法转换到 wcf)。我的 WebServiceRequest 类中有其他类的列表。尝试编译代码时出现以下错误:

request.List = list;

不能隐式转换类型 'System.Collections.Generic.List' 到 'ServiceReference1.foo[]'

当我执行request.List = list.ToArray(); 时,此错误消失 但是当我运行代码时,我得到了这个异常:

接收到的 HTTP 响应时出错 http://localhost:60380/fooService/fooService.asmx。这可能是由于 到不使用 HTTP 协议的服务端点绑定。这 也可能是由于 HTTP 请求上下文被 服务器(可能是由于服务关闭)。查看服务器日志 了解更多详情。

内部异常:

底层连接已关闭:发生意外错误 一个接收。

服务器堆栈跟踪:

在 System.ServiceModel.Channels.HttpChannelUtilities.ProcessGetResponseWebException(WebException webException, HttpWebRequest 请求, HttpAbortReason abortReason)
在 System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(时间跨度 超时)在 System.ServiceModel.Channels.RequestChannel.Request(消息消息, TimeSpan 超时)在 System.ServiceModel.Dispatcher.RequestChannelBinder.Request(消息 消息,TimeSpan 超时)在 System.ServiceModel.Channels.ServiceChannel.Call(字符串动作, Boolean oneway, ProxyOperationRuntime 操作, Object[] ins, Object[] 出局,TimeSpan 超时)在 System.ServiceModel.Channels.ServiceChannel.Call(字符串动作, Boolean oneway, ProxyOperationRuntime 操作, Object[] ins, 对象 [] 出局)在 System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall,ProxyOperationRuntime 操作)在 System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage 留言)

在 [0] 处重新抛出异常:在 System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg,IMessage retMsg)在 System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(消息数据& msgData,Int32 类型)在 ServiceReference1.testpingtestSoap.Calculatetestping(CalculatetestpingRequest1 请求)在 ServiceReference1.testpingtestSoapClient.ServiceReference1.testpingtestSoap.Calculatetestping(CalculatetestpingRequest1 请求)在 c:\Users\User\AppData\Local\Temp\Temporary ASP.NET 文件\test\2e05987f\91110734\App_WebReferences.rwmro5zd.0.cs:855 行 在 ServiceReference1.testpingtestSoapClient.Calculatetestping(CalculatetestpingRequest _request) 在 c:\Users\User\AppData\Local\Temp\Temporary ASP.NET Files\test\2e05987f\91110734\App_WebReferences.rwmro5zd.0.cs:line 862 在 testCost2.Calculatetestping() 中 c:\Users\User\Desktop\test\InfoSystem\testping\testCost2.aspx.cs:line 203 at testCost2.Submit_Go(Object sender, EventArgs e) in c:\Users\User\Desktop\test\InfoSystem\testping\testCost2.aspx.cs:line 148 在 System.Web.UI.WebControls.Button.OnClick(EventArgs e) 在 System.Web.UI.WebControls.Button.RaisePostBackEvent(字符串 事件参数)在 System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(字符串 事件参数)在 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl,字符串 eventArgument) 在 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
在 System.Web.UI.Page.ProcessRequestMain(布尔 includeStagesBeforeAsyncPoint,布尔型 includeStagesAfterAsyncPoint)

编辑 1: Web 服务的请求和响应类

 public class Item
    {

        public List<string> Composites { get; set; }
        public string Composites_List { get { return string.Join(", ", Composites.ToArray()); } }



        public List<px> pxes { get; set; }
        public List<ox> oxes { get; set; }

        public List<MethodCalculationType> MethodCalculationTypes { get; set; }
        public ShippingItem() { }
        public void ShippingItemFill(string sku, int quantity)
        {
            \\this method is just written to avoid parameterless constructor error
        }
        public static ItemComparison = delegateItem item1, Item item2) { return item1.Area.CompareTo(item2.Area); };
    }
   public class Request
    {
        public List<Item> ItemList;
        public bool showAllRatesField;
        public bool m_Ignore;
        public string pTextField;
        public decimal TotalField;

            }
    public class Responce
    {
        public od [] ods;

    }

【问题讨论】:

  • @Tobias 当我在请求中放入一个简单的字符串并且没有 List 它运行完美,如果您仍然认为这是配置问题,知道什么会被错误配置吗?

标签: c# .net list exception asmx


【解决方案1】:

发现了问题,你可以在webservice的参数中使用尽可能多的Lists&lt;&gt;,这就是我发现的;以防有人需要:

1. 通过在服务器端的 web.config 中添加以下内容,在服务器上启用诊断

<system.diagnostics> <trace autoflush="true" /> <sources> <source name="System.Web.Services.Asmx"> <listeners> <add name="AsmxTraceFile" type="System.Diagnostics.TextWriterTraceListener" initializeData="local.log" traceOutputOptions="LogicalOperationStack, DateTime, Timestamp, ProcessId, ThreadId" /> </listeners> </source> </sources> <switches> <add name="System.Web.Services.Asmx" value="Verbose" /> </switches> </system.diagnostics>


2. Http Exception 发生时;在日志文件中它写了error reading or writing a variable我在webservice代码中查找了那个变量,那个变量是internal,从那个变量中删除了internal关键字,代码开始工作了。

原来soap 需要对参数进行序列化,因此自定义参数类中的任何变量都应该是公共读/写的。正如约翰桑德斯所提到的here

【讨论】:

    【解决方案2】:

    首先使用 .asmx,我忘了这一点,你不能返回一个列表,你必须返回一个数组,所以它需要一个数组

    至于第二个异常,您的服务未正确配置 /setup。如果您想要修复,您将需要发布更多代码。

    顺便说一句,服务器事件日志已在其中。 有时这可能会有所帮助。 开始菜单->管理工具->事件查看器。查看应用程序日志。

    【讨论】:

    • 1)请求类有一个List&lt;&gt; 那么为什么它需要一个数组。 2) 正如我上面提到的,当我传递一个数组时,我得到一个异常
    • ServiceReference1.foo[]' -- 在我看来就像一个数组
    • 这就是我确定它的 List 不是请求类中的数组。
    • 做了很多次仍然需要一个数组。它与网络服务有关的东西列表可能需要序列化或其他东西
    • @JohnSaunders 我不认为 .asmx Web 服务默认返回 .net 类型。对了,无论如何它都不会反序列化为数组?
    猜你喜欢
    • 1970-01-01
    • 2018-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-23
    • 2011-08-27
    相关资源
    最近更新 更多