c#WCF服务返回集合

本文关键字:集合 返回 服务 c#WCF | 更新日期: 2023-09-27 18:26:23

我已经创建了wcf服务,一切正常,但当我试图返回Type[](我从Assembly.GetTypes()获得它)时,我有以下异常:

    System.ServiceModel.CommunicationException: The underlying connection was closed: The connection was closed unexpectedly. 
---> System.Net.WebException: The underlying connection was closed: The connection was closed unexpectedly.
   at System.Net.HttpWebRequest.GetResponse()
   at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
   --- End of inner exception stack trace ---
Server stack trace:
   at System.ServiceModel.Channels.HttpChannelUtilities.ProcessGetResponseWebException(WebException webException, HttpWebRequest request, HttpAbortReason abortReason)
   at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
   at System.ServiceModel.Channels.RequestChannel.Request(Message message, TimeSpan timeout)
   at System.ServiceModel.Dispatcher.RequestChannelBinder.Request(Message message, TimeSpan timeout)
   at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
   at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
   at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)
Exception rethrown at [0]:
   at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
   at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
   at ConsoleApplication1.ServiceReference1.IWcfAssembly.GetAssemblyTypes(String a)
   at ConsoleApplication1.ServiceReference1.WcfAssemblyClient.GetAssemblyTypes(String a) in D:'Projekty'ConsoleApplication1'ConsoleApplication1'Service References'ServiceReference1'Reference.cs:line 50
   at ConsoleApplication1.Program.Main(String[] args) in D:'Projekty'ConsoleApplication1'ConsoleApplication1'Program.cs:line 17

我认为它可能是maxItemsInObjectGraph属性或maxArrayLenght(以及类似的属性),但它没有帮助。仍然有同样的例外。也许我在财产申报上做错了什么?

这是我的客户端配置:

    <?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="BasicHttpBinding_IWcfAssembly" closeTimeout="00:01:00"
                    openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                    allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                    maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647"
                    messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
                    useDefaultWebProxy="true">
                    <readerQuotas maxDepth="128" maxStringContentLength="2147483647" maxArrayLength="2147483647"
                        maxBytesPerRead="4096" maxNameTableCharCount="2147483647" />
                    <security mode="None">
                        <transport clientCredentialType="None" proxyCredentialType="None"
                            realm="" />
                        <message clientCredentialType="UserName" algorithmSuite="Default" />
                    </security>
                </binding>
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://localhost:57040/WcfAssembly.svc" binding="basicHttpBinding"
                bindingConfiguration="BasicHttpBinding_IWcfAssembly" contract="ServiceReference1.IWcfAssembly"
                name="BasicHttpBinding_IWcfAssembly" />
        </client>
    </system.serviceModel>
</configuration>

这是我的服务配置:

    <?xml version="1.0"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBinding_IWcfAssembly" receiveTimeout="00:10:00" sendTimeout="00:10:00" openTimeout="00:10:00" closeTimeout="00:10:00"
                 maxReceivedMessageSize="2147483647"
                 maxBufferSize="2147483647"
                 maxBufferPoolSize="52428899">
          <readerQuotas maxDepth="128"
                        maxStringContentLength="2147483647"
                        maxArrayLength="2147483647"
                        maxBytesPerRead="4096"
                        maxNameTableCharCount="2147483647" />
          <security mode="None"/>
        </binding>
      </basicHttpBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
</configuration>

我还添加了[ServiceBehavior(MaxItemsInObjectGraph=int.MaxValue)]作为类属性。我以前在我的服务配置中有它,但这是一次尝试。

伙计们,你们有什么想法吗?为什么从Assembly.GetTypes()返回的数组会导致这样的错误?(int[]的大数组工作正常)

c#WCF服务返回集合

问题是System.RunTimeType未序列化(在Type的内部实现中)

使用这个模拟:

void Main()
{
    var dto = new MyDto();
    dto.Tada = new Type[] { this.GetType() };
    DataContractSerializer ser =
        new DataContractSerializer(typeof(MyDto));
    var ms = new MemoryStream();
    ser.WriteObject(ms, dto);
    ms.Dump();
    dto.Dump();
}
public class MyDto
{
   public Type[]  Tada { get; set; }
}

投掷:

SerializationException:具有数据协定的类型"System.RuntimeType"name'运行时间类型:http://schemas.datacontract.org/2004/07/System'是不应为。考虑使用DataContractResolver或添加任何类型对于已知类型列表来说不是静态已知的-例如,通过使用KnownTypeAttribute属性或将其添加到列表中传递给DataContractSerializer的已知类型的。

这是有道理的,因为RunTimeType是内部的(Type本身是抽象的)。你必须想出你自己的序列化类型,可以传达你想要的信息。