Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
WCF服务上载字节[]_Wcf_Web Services_Upload_Byte_Document - Fatal编程技术网

WCF服务上载字节[]

WCF服务上载字节[],wcf,web-services,upload,byte,document,Wcf,Web Services,Upload,Byte,Document,我有一个服务,应该通过下载和上传来处理文件。通过返回byte[]数组,下载文档可以完美地工作 上载我已尝试发送字节[]作为输入,但出现以下错误: “远程服务器返回意外响应:(400)错误请求” 通过谷歌搜索,我找到了在web.config中使用以下代码的解决方案 httpRuntime maxRequestLength=“2000000” 还是一样的错误。。。 My web.config是vs2008的默认设置(太长而无法发布?) 和我的app.config: <?xml version=

我有一个服务,应该通过下载和上传来处理文件。通过返回byte[]数组,下载文档可以完美地工作

上载我已尝试发送字节[]作为输入,但出现以下错误: “远程服务器返回意外响应:(400)错误请求”

通过谷歌搜索,我找到了在web.config中使用以下代码的解决方案 httpRuntime maxRequestLength=“2000000”

还是一样的错误。。。 My web.config是vs2008的默认设置(太长而无法发布?) 和我的app.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <bindings>
            <wsHttpBinding>
                <binding name="WSHttpBinding_IDocService" closeTimeout="00:01:00"
                    openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                    bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
                    maxBufferPoolSize="524288" maxReceivedMessageSize="1000000"
                    messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
                    allowCookies="false">
                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="1000000"
                        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    <reliableSession ordered="true" inactivityTimeout="00:10:00"
                        enabled="false" />
                    <security mode="Message">
                        <transport clientCredentialType="Windows" proxyCredentialType="None"
                            realm="" />
                        <message clientCredentialType="Windows" negotiateServiceCredential="true"
                            algorithmSuite="Default" establishSecurityContext="true" />
                    </security>
                </binding>
            </wsHttpBinding>
        </bindings>
        <client>
            <endpoint address="xxxxx.svc"
                binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IDocService"
                contract="WCFDocumentService.IDocService" name="WSHttpBinding_IDocService">
                <identity>
                    <dns value="localhost" />
                </identity>
            </endpoint>
        </client>
    </system.serviceModel>
</configuration>

我的web.config文件:

<?xml version="1.0"?>
<!--
    Note: As an alternative to hand editing this file you can use the 
    web admin tool to configure settings for your application. Use
    the Website->Asp.Net Configuration option in Visual Studio.
    A full list of settings and comments can be found in 
    machine.config.comments usually located in 
    \Windows\Microsoft.Net\Framework\v2.x\Config 
-->
<configuration>


    <configSections>
      <sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
        <sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
          <section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
          <sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
            <section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="Everywhere" />
            <section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication" />
            <section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication" />
            <section name="roleService" type="System.Web.Configuration.ScriptingRoleServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication" />
          </sectionGroup>
        </sectionGroup>
      </sectionGroup>
    </configSections>  


    <appSettings>
        <add key="test" value="123" />
    </appSettings>
    <connectionStrings/>

    <system.web>
        <httpRuntime maxRequestLength="2000000"/>
        <!--
            Set compilation debug="true" to insert debugging 
            symbols into the compiled page. Because this 
            affects performance, set this value to true only 
            during development.
        -->
        <compilation debug="true">

          <assemblies>
            <add assembly="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
            <add assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
          </assemblies>

        </compilation>
        <!--
            The <authentication> section enables configuration 
            of the security authentication mode used by 
            ASP.NET to identify an incoming user. 
        -->
        <authentication mode="Windows" />
        <!--
            The <customErrors> section enables configuration 
            of what to do if/when an unhandled error occurs 
            during the execution of a request. Specifically, 
            it enables developers to configure html error pages 
            to be displayed in place of a error stack trace.

        <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
            <error statusCode="403" redirect="NoAccess.htm" />
            <error statusCode="404" redirect="FileNotFound.htm" />
        </customErrors>
        -->


      <pages>
        <controls>
          <add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        </controls>
      </pages>

      <httpHandlers>
        <remove verb="*" path="*.asmx"/>
        <add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false"/>
      </httpHandlers>
      <httpModules>
        <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
      </httpModules>


    </system.web>

    <system.codedom>
      <compilers>
        <compiler language="c#;cs;csharp" extension=".cs" warningLevel="4"
                  type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
          <providerOption name="CompilerVersion" value="v3.5"/>
          <providerOption name="WarnAsError" value="false"/>
        </compiler>
      </compilers>
    </system.codedom>

    <!--
        The system.webServer section is required for running ASP.NET AJAX under Internet
        Information Services 7.0.  It is not necessary for previous version of IIS.
    -->
    <system.webServer>
      <validation validateIntegratedModeConfiguration="false"/>
      <modules>
        <add name="ScriptModule" preCondition="integratedMode" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
      </modules>
      <handlers>
        <remove name="WebServiceHandlerFactory-Integrated"/>
        <add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode"
             type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add name="ScriptHandlerFactoryAppServices" verb="*" path="*_AppService.axd" preCondition="integratedMode"
             type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add name="ScriptResource" preCondition="integratedMode" verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
      </handlers>
    </system.webServer>


  <system.serviceModel>
      <bindings>
          <wsHttpBinding>
              <binding name="myBinding" maxReceivedMessageSize="1000000">
                  <readerQuotas maxArrayLength="1000000"/>
              </binding>
          </wsHttpBinding>
      </bindings>
    <services>
      <service behaviorConfiguration="WCFDocService.Service1Behavior"
        name="WCFDocService.docService">
        <endpoint address="" binding="wsHttpBinding" contract="WCFDocService.IDocService">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="WCFDocService.Service1Behavior">
          <!-- 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="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

您已将客户端配置为支持文档下载:
maxReceivedMessageSize
maxArrayLength
。您必须在服务上执行相同操作才能支持上载


顺便说一句,您还应该在客户端和服务上将
messageEncoding
更改为MTOM-这将改进字节数组的序列化,因为此时它作为消息体编码的一部分传递给base64=>消息的大小将增长约33%。如果使用MTOM,它将作为消息附件传递,而不编码到base64。

您已将客户端配置为支持文档下载:
maxReceivedMessageSize
maxArrayLength
。您必须在服务上执行相同操作才能支持上载


顺便说一句,您还应该在客户端和服务上将
messageEncoding
更改为MTOM-这将改进字节数组的序列化,因为此时它作为消息体编码的一部分传递给base64=>消息的大小将增长约33%。如果您使用MTOM,它将作为消息附件传递,而不编码到base64。

在我们的应用程序中,我们正在大量使用WCF。只是一个建议。使用简单的字节传输,您无法上载/下载足够大的文件。要传输大文件或字节数组,应使用流绑定。如果您感兴趣,请查看我们是如何使用它的:

  • 信息
  • 信息
  • 服务合同
  • 服务客户端
  • 服务实现
  • 主机应用程序
  • 客户端应用程序

在我们的业务中,我们与WCF进行了大量合作。只是一个建议。使用简单的字节传输,您无法上载/下载足够大的文件。要传输大文件或字节数组,应使用流绑定。如果您感兴趣,请查看我们是如何使用它的:

  • 信息
  • 信息
  • 服务合同
  • 服务客户端
  • 服务实现
  • 主机应用程序
  • 客户端应用程序

    • 我也遇到了同样的问题,我刚刚意识到我必须提高客户端和服务的限制。如果您遇到相同的问题,请确保使用发布的配置文件的语法提高两侧的数组限制。

      我遇到了相同的问题,刚刚意识到我必须提高客户端和服务的限制。如果遇到相同的问题,请确保使用发布的配置文件的语法提高两侧的数组限制。

      是否尝试增加readerQuotas中的maxStringContentLength属性?根据我的经验,WCF通常会告诉您这是否是问题所在,但可能值得一试。您是否尝试过在readerQuotas中增加maxStringContentLength属性?根据我的经验,WCF通常会告诉您这是否是问题所在,但可能值得一试。我已经发布了添加了绑定的web.config文件,但仍然不走运……很抱歉,我对web服务比较陌生。。。改为出现此错误:“无法激活请求的服务'xxxx.svc'。有关详细信息,请参阅服务器的诊断跟踪日志。“首先,您的服务配置没有使用您定义的绑定-它缺少
      bindingConfiguration
      属性。如果您需要启用跟踪,请使用。我已经发布了添加了绑定的web.config文件,但仍然不走运…对不起,我对web服务比较陌生。。。改为出现此错误:“无法激活请求的服务'xxxx.svc'。有关详细信息,请参阅服务器的诊断跟踪日志。“首先,您的服务配置没有使用您定义的绑定-它缺少
      bindingConfiguration
      属性。如果需要启用跟踪,请使用。