繁体   English   中英

将文件字节流发送到WCF服务时,为什么会出现“ 401-未经授权”错误?

[英]Why am I getting a “401 - Unauthorized” error when sending a file byte stream to a WCF service?

我有一个WCF服务,在Silverlight客户端调用的II7上托管有basicHttp绑定。 我可以调用终结点计算机上的所有服务,除了一个服务之外没有任何问题。

我正在尝试上传文件,以便该服务接收一个字节数组。 如果我上传的文件大小超过3MB,则会出现以下错误。

当我尝试致电此服务时:

[OperationContract]
public AuditResponse UploadVendorAuditFile( int vendorID, 
                                            int sourceSystemID, 
                                            string fileName,
                                            byte[] fileBytes )
{
    // stuff
}

我收到以下错误:

401-未经授权:由于凭据无效,访问被拒绝。 您无权使用您提供的凭据查看此目录或页面。

这是我的配置。

端点绑定

<basicHttpBinding>
  <binding 
    name="basicHttpBindingConfiguration" 
    maxBufferSize="2147483647"
    maxBufferPoolSize="2147483647" 
    maxReceivedMessageSize="2147483647"
    >
    <readerQuotas 
    maxDepth="2147483647" 
    maxStringContentLength="2147483647"
    maxArrayLength="2147483647" 
    maxBytesPerRead="2147483647" 
    maxNameTableCharCount="2147483647" 
    />
    <security mode="TransportCredentialOnly">
    <transport clientCredentialType="Ntlm" proxyCredentialType="None" realm="" />
    </security>
  </binding>
</basicHttpBinding>

服务配置

<behaviors>
  <serviceBehaviors>
    <behavior name="">
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="true" />
      <dataContractSerializer maxItemsInObjectGraph="2147483647" />
    </behavior>
  </serviceBehaviors>
</behaviors>

客户

<system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding 
              name="BasicHttpBinding_WMService" 
              maxBufferSize="2147483647"
              maxReceivedMessageSize="2147483647"
              >
                <security mode="TransportCredentialOnly" />
            </binding>
        </basicHttpBinding>
    </bindings>

失败的文件是否大于1MB左右? 尝试启用跟踪以了解错误的实际原因。

如果文件较大,则可能是由于需要在服务器端和客户端将读取器配额设置设置为较大的值引起的

还可以考虑将maxItemsInObjectGraph添加到您的serviceBehaviour中,如图所示

 <serviceBehaviors>        
    <behavior>
      <dataContractSerializer maxItemsInObjectGraph="2147483647" />
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="true" />
    </behavior>
  </serviceBehaviors>

注意:确保在客户端和服务器端具有相同的readerQuotas设置

也尝试在web.config中设置以下设置

<system.web>  
     <httpRuntime maxRequestLength ="32768"/>
  </system.web>  

确保在WCF服务的绑定中设置最大邮件大小。

例如:

<binding name="xxx" maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647">
  <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="50000000" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
</binding>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM