1

我有一个 wcf 服务,我想用它来上传可能很大的图像。我有一个启用流式传输的基本 http 端点。

目前,当我从 Windows 应用程序调用此服务时,我得到一个 http 400。奇怪的是,它在 400 错误之后仍然进入方法(它达到了我的断点),但我传递给它的流无法读取(uploadStream.ReadByte()返回 -1)

我还创建了一个方法,它接受一个字符串并返回一个字符串来测试,它可以 100% 工作。

我已经阅读了许多解决方案文章,但似乎没有解决我的问题。

网页配置

  <system.web>
        <httpRuntime
        maxRequestLength="65536"
        />
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="FileUploadServiceBinding" closeTimeout="04:01:00"
                         openTimeout="04:01:00" receiveTimeout="04:10:00" sendTimeout="04:01:00" 
                         allowCookies="false" bypassProxyOnLocal="false"
                         hostNameComparisonMode="StrongWildcard"
                         maxBufferSize="2147483647" maxBufferPoolSize="2147483647"
                         maxReceivedMessageSize="2147483647"
                         messageEncoding="Mtom" textEncoding="utf-8"
                         transferMode="Streamed"
                         useDefaultWebProxy="true">
                    <security mode="None">
                        <transport clientCredentialType="None" />
                    </security>
                    <readerQuotas maxDepth="128"
                        maxStringContentLength="2147483647" maxArrayLength="2147483647"
                        maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
                </binding>
            </basicHttpBinding>
        </bindings>
        <services>
            <service name="Smd.ThreeSixtyDataTransfer.ThreeSixtyDataTransferService">
                <endpoint address="" binding="basicHttpBinding" bindingConfiguration="FileUploadServiceBinding"
                 contract="Smd.ThreeSixtyDataTransfer.IThreeSixtyDataTransferService">
                    <identity>
                        <dns value="localhost" />
                    </identity>
                </endpoint>
                <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
                <host>
                    <baseAddresses>
                        <add baseAddress="http://localhost:8732/ThreeSixtyDataTransferService/" />
                    </baseAddresses>
                </host>
            </service>
        </services>
        <behaviors>
            <serviceBehaviors>
                <behavior>
                    <!-- 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="False" />
                </behavior>
            </serviceBehaviors>
        </behaviors>
    </system.serviceModel>
 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>

</configuration>

服务

public class ThreeSixtyDataTransferService : IThreeSixtyDataTransferService
{

    public RequestResponse AttachPhotoToDataItem(UploadMessage photo)
    {

        ...

        return new RequestResponse() { UploadSucceeded = true };
    }

    public string Test(string test)
    {
            return test;
    }
}
[ServiceContract]
public interface IThreeSixtyDataTransferService
{

    [OperationContract]
    RequestResponse AttachPhotoToDataItem(UploadMessage photo);

    [OperationContract]
    string Test(string test);


}


[MessageContract]
public class UploadMessage
{
    [MessageHeader(MustUnderstand = true)]
    public Guid DataId { get; set; }

    [MessageBodyMember(Order = 1)]
    public Stream Stream { get; set; }
}


[MessageContract]
public class RequestResponse
{
    [MessageBodyMember(Order = 1)]
    public bool UploadSucceeded { get; set; }
}

客户

 using (Stream uploadStream =new FileStream("D:\\Folder\\somefoto.jpg", FileMode.Open))
 {

            using (ThreeSixtyDataTransferService.ThreeSixtyDataTransferServiceClient threeSixtyDataTransferService = new ThreeSixtyDataTransferService.ThreeSixtyDataTransferServiceClient())
            {
                        var test =threeSixtyDataTransferService.Test("Test");//works    
                        threeSixtyDataTransferService.AttachPhotoToDataItem(Guid.NewGuid(), uploadStream);//400
            }
 }

非常感谢任何帮助

4

0 回答 0