简体   繁体   English

尝试上传时,WCF返回400错误请求

[英]WCF returns 400 Bad Request when trying to upload

I have been at this for a while now and would really appreciate a second opinion on this. 我已经在这里待了一段时间了,非常感谢对此有第二种意见。 Can't seem to figure out why i'm getting a 400 Bad Request. 似乎无法弄清楚为什么我收到400错误的请求。

WCF Web Config 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" />

And then the Service Contract: 然后是服务合同:

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

And the SVC: 和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;
    }

Thank you so much for looking over this for me. 非常感谢您为我查看此内容。

Change the size of the message in basicHTTPBinding, it might be because of file size is greater than the size can be handled by WCF. 在basicHTTPBinding中更改消息的大小,这可能是因为文件大小大于WCF可以处理的大小。

refer to below link - 请参阅以下链接-

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

Try to enable Tracing on your service to see why you are getting a 400 BadRequest. 尝试在您的服务上启用跟踪 ,以查看为什么收到400 BadRequest的原因。

In order to access your service in a RESTful way you need to have your binding as webHttpBinding where as in your config you are using basicHttpBinding 为了以RESTful方式访问您的服务,您需要将绑定设置为webHttpBinding,而在配置中,您将使用basicHttpBinding

Also make sure that you have used the KnowType attribute for your service to identify your UploadMedia and UploadStatus objects for deserialization. 另外,请确保已使用服务的KnowType属性来标识要反序列化的UploadMedia和UploadStatus对象。

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

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