尝试上传时,WCF返回400 Bad Request

时间:2012-03-23 00:48:05

标签: c# .net wcf file-upload stream

我现在已经有一段时间了,我真的很感激对此的第二个意见。似乎无法弄清楚为什么我收到400 Bad Request。

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;
    }

非常感谢你为我看这个。

2 个答案:

答案 0 :(得分:1)

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

请参阅以下链接 -

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

答案 1 :(得分:0)

尝试在您的服务上启用Tracing,以了解您获得400 BadRequest的原因。

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

另外,请确保您已使用服务的KnowType属性来标识您的UploadMedia和UploadStatus对象以进行反序列化。