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)
    {
        try
        {
            if (!Directory.Exists(_path))
            {
                Directory.CreateDirectory(_path);
            }
            using (FileStream outputStream = new FileStream(_path + pocoStream.FileName, FileMode.OpenOrCreate, FileAccess.Write))
            {
                pocoStream.Stream.CopyTo(outputStream);
                outputStream.Flush();
            }
        }
        catch (Exception ex)
        {
            Debug.WriteLine(ex);
        }
    }
}

and the PocoStream is like this:

[MessageContract]
public class PocoStream
{
    [MessageHeader]
    public string FileName { get; set; }

    [MessageBodyMember]
    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:

<system.serviceModel>
  <services>
    <service name="test1.FileUploadService">
      <host>
        <baseAddresses>
          <add baseAddress="http://localhost:2668/"/>
        </baseAddresses>
      </host>
      <!-- Service Endpoints -->
      <endpoint address="FileUploadService.svc" binding="basicHttpBinding"
                contract="test1.IFileUploadService" bindingConfiguration="haha" >
        <identity>
          <dns value="localhost"/>
        </identity>
      </endpoint>
      <!-- Metadata Endpoints -->
      <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
    </service>
  </services>
  <behaviors>
    <serviceBehaviors>
      <behavior name="">
        <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
        <serviceDebug includeExceptionDetailInFaults="true"/>
      </behavior>
    </serviceBehaviors>
  </behaviors>
  <bindings>
    <basicHttpBinding>
      <binding name="haha" closeTimeout="0:10:00" 
               receiveTimeout="00:20:00" sendTimeout="00:20:00" maxBufferSize="65536" 
               maxReceivedMessageSize="2147483647000" transferMode="StreamedRequest"/>
    </basicHttpBinding>
  </bindings>
  <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
</system.serviceModel>
  • 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:

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

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