Okay, the situation is like this, I want to add an exist WCF service to an exist asp.net website, but I got a headache 400 problem:

  • I've got a SOAP WCF service sending large filestream which works pretty well.

FileUploadService.cs is like this and the interface is well configured.

public class FileUploadService : IFileUploadService
    private readonly string _path = Directory.GetCurrentDirectory() + \\sharedfiles\\";
    public string GetData(int value)
        return string.Format("You entered: {0}", value);
    public void UploadPocoStream(PocoStream pocoStream)
            if (!Directory.Exists(_path))
            using (FileStream outputStream = new FileStream(_path + pocoStream.FileName, FileMode.OpenOrCreate, FileAccess.Write))
        catch (Exception ex)

and the PocoStream is like this:

public class PocoStream
    public string FileName { get; set; }

    public Stream Stream { get; set; }
  • I'm going to combine the WCF service with an exist demo website.

I added this to website's web.config file:

    <service name="test1.FileUploadService">
          <add baseAddress="http://localhost:2668/"/>
      <!-- Service Endpoints -->
      <endpoint address="FileUploadService.svc" binding="basicHttpBinding"
                contract="test1.IFileUploadService" bindingConfiguration="haha" >
          <dns value="localhost"/>
      <!-- Metadata Endpoints -->
      <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      <behavior name="">
        <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
        <serviceDebug includeExceptionDetailInFaults="true"/>
      <binding name="haha" closeTimeout="0:10:00" 
               receiveTimeout="00:20:00" sendTimeout="00:20:00" maxBufferSize="65536" 
               maxReceivedMessageSize="2147483647000" transferMode="StreamedRequest"/>
  <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
  • I successfully added service reference and build a test client.

test client is like this:

FileUploadServiceClient a = new FileUploadServiceClient();
using (FileStream fs = new FileStream(filepath, FileMode.Open, FileAccess.Read))
    var b = new PocoStream() { FileName = fileName, Stream = fs };
    a.UploadPocoStream(b.FileName, b.Stream);

and the client’s app.config:

      <binding name="BasicHttpBinding_IFileUploadService" closeTimeout="0:10:00" receiveTimeout="00:20:00" sendTimeout="00:20:00" maxBufferSize="65536" maxReceivedMessageSize="2147483647000" transferMode="StreamedRequest"/>
        <endpoint address="http://localhost:2668/FileUploadService.svc"
            binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IFileUploadService"
            contract="ServiceReference1.IFileUploadService" name="BasicHttpBinding_IFileUploadService" />

But the result is that once run the client, I got an unexpected 400 bad request at client: enter image description here

and this at server: enter image description here

I think I got something wrong at the config file. Any one can help? Thanks a lot!

Related posts

Recent Viewed