c#WCF服务返回集合

时间:2013-08-05 09:41:10

标签: c# arrays wcf return

我已经创建了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 []的大数组正常工作)

1 个答案:

答案 0 :(得分:0)

问题是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”   名称'RuntimeType:http://schemas.datacontract.org/2004/07/System'是   没想到。考虑使用DataContractResolver或添加任何类型   静态地知道已知类型的列表 - 例如,by   使用KnownTypeAttribute属性或将它们添加到列表中   传递给DataContractSerializer的已知类型。

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