WCF在尝试上载时返回400错误请求

本文关键字:返回 错误 请求 上载 WCF | 更新日期: 2023-09-27 18:28:56

我已经做了一段时间了,如果能有其他意见,我将不胜感激。我似乎不明白为什么我会收到一个400坏请求。

WCF Web配置

  <system.serviceModel>
<bindings>
  <basicHttpBinding>
    <binding name="FileTransferServicesBinding"
      transferMode="Streamed" messageEncoding="Mtom"
      sendTimeout="01:05:00" receiveTimeout="00:10:00" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647">
      <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
 maxArrayLength="2147483647" maxBytesPerRead="2147483647"
 maxNameTableCharCount="2147483647" />
    </binding>
  </basicHttpBinding>
</bindings>
<behaviors>
  <endpointBehaviors>
    <behavior name="FileTransferServiceBehavior">
    </behavior>
  </endpointBehaviors>
  <serviceBehaviors>
    <behavior name="FileTransferServiceBehavior">
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="true" />
    </behavior>
  </serviceBehaviors>
</behaviors>
<services>
  <service name="PP.Live.WCFService.MobileClient">
    <endpoint name="MobileClientEndpoint" binding="basicHttpBinding" bindingConfiguration="FileTransferServicesBinding" contract="PP.Live.WCFService.LiveService"/>
  </service>
</services>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />

然后是服务合同:

[ServiceContract]
public interface LiveService
{
    [OperationContract]
    [WebInvoke(Method = "POST")]
    UploadMedia UploadFile(System.IO.Stream fileData);

 }
[DataContract]
public class UploadMedia
{
    [DataMember]
    public UploadStatus Status { get; set; }
    [DataMember]
    public string Message { get; set; }
}
public enum UploadStatus
{
    Success,
    Error
}

以及SVC:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class MobileClient : LiveService
{
    public UploadMedia UploadFile(System.IO.Stream fileData)
    {
        Trace.WriteLine("Uploading File");
        UploadMedia uploadMedia = new UploadMedia();
        try
        {
            //byte[] imageBytes = StreamHelper.ReadToEnd(fileData);
            var memoryStream = new MemoryStream();
            fileData.CopyTo(memoryStream);
            byte[] imageBytes = memoryStream.ToArray();
            memoryStream.Close();
            memoryStream.Dispose();
            string token = StoreFile(imageBytes);
            fileData.Close();
            fileData.Dispose();
            if (!string.IsNullOrWhiteSpace(token))
            {
                uploadMedia.Message = token;
                uploadMedia.Status = UploadStatus.Success;
            }
            else
            {
                uploadMedia.Message = "No Token";
                uploadMedia.Status = UploadStatus.Error;
            }
        }
        catch (Exception ex)
        {
            uploadMedia.Message = ex.Message + ex.StackTrace;
            uploadMedia.Status = UploadStatus.Error;
        }
        return uploadMedia;
    }

非常感谢你帮我看这个。

WCF在尝试上载时返回400错误请求

更改basicHTTPBinding中消息的大小,可能是因为文件大小大于WCF可以处理的大小。

请参阅以下链接-

http://forums.asp.net/t/1299246.aspx/1

尝试在您的服务上启用跟踪,以了解为什么会收到400 BadRequest。

为了以RESTful的方式访问您的服务,您需要将绑定设置为webHttpBinding,在您的配置中,您使用的是basicHttpBinding

此外,请确保已使用服务的KnowType属性来标识要反序列化的UploadMedia和UploadStatus对象。