简体   繁体   中英

The remote server returned an unexpected response: (400) Bad Request, WCF

I'm trying to send a image to my WCF and when a does this I get.

The remote server returned an unexpected response: (400) Bad Request.

Everything else works fine to send to the WCF and the image is not that large ~90kb. I found a lot of threads on this but nothing that help me. I have tried to increase the size limit but that is not working.

web.config

  <system.serviceModel>
    <bindings>
      <wsHttpBinding>
        <binding name="WSHttpBinding_IService" closeTimeout="00:01:00"
        openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
        bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
        maxBufferPoolSize="2000000" maxReceivedMessageSize="2000000"
        messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
        allowCookies="false">
          <readerQuotas maxDepth="2000000" maxStringContentLength="2000000" maxArrayLength="2000000"
          maxBytesPerRead="2000000" maxNameTableCharCount="2000000" />
          <reliableSession ordered="true" inactivityTimeout="00:10:00" enabled="false" />
          <security mode="Message">
            <transport clientCredentialType="Windows" proxyCredentialType="None" realm="" />
            <message clientCredentialType="Windows" negotiateServiceCredential="true"
            algorithmSuite="Default" establishSecurityContext="true" />
          </security>
        </binding>
      </wsHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://localhost:8732/Design_Time_Addresses/WcfDataLager/Service1/"
        binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IService"
        contract="ServiceReference.IService" name="WSHttpBinding_IService">
        <identity>
          <dns value="localhost" />
        </identity>
      </endpoint>
    </client>
  </system.serviceModel>

app.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
  </configSections>
  <connectionStrings>
    <add name="WcfDataLager.Properties.Settings.WebbshopConnectionString"
      connectionString="Data Source=(local);Initial Catalog=Webbshop;Integrated Security=True"
      providerName="System.Data.SqlClient" />
  </connectionStrings>
  <system.web>
    <compilation debug="true" />
  </system.web>
  <system.serviceModel>
    <bindings />
    <client />
    <services>
      <service name="WcfDataLager.Service">
        <endpoint address="" binding="wsHttpBinding" contract="WcfDataLager.IService">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8732/Design_Time_Addresses/WcfDataLager/Service1/" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="True"/>
          <serviceDebug includeExceptionDetailInFaults="True" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

Page code.

protected void btnAdd_Click(object sender, EventArgs e)
{
    Produkt produkt = new Produkt();
    produkt.Namn = txtNamn.Text;
    produkt.Pris = Convert.ToDouble(txtPris.Text);
    produkt.Beskrivning = txtbeskrivning.Text;
    produkt.LagerAntal = Convert.ToInt32(txtAntal.Text);
    produkt.Typ = txtGenre.Text;
    //produkt.ImageAsByte = fupBild.FileBytes;
    produkt.Bild = new System.Drawing.Bitmap(fupBild.PostedFile.InputStream);
    using (ServiceReference.ServiceClient wcfClient = new ServiceReference.ServiceClient())
    {
        wcfClient.AddProdukt(produkt);
    }
}

WCF code.

public void AddProdukt(Produkt produkt)
{
   DataSetTableAdapters.ProduktTableAdapter itemsTA = new
WcfDataLager.DataSetTableAdapters.ProduktTableAdapter();
   byte[] bmpAsByte;
   using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
   {
       produkt.Bild.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
       stream.Position = 0;
       bmpAsByte = new byte[stream.Length];
       stream.Read(bmpAsByte, 0, (int)stream.Length);
       stream.Close();
   }
   produkt.ID = 7;
   itemsTA.InsertProdukt(produkt.Namn, produkt.Pris, produkt.Beskrivning, produkt.LagerAntal, bmpAsByte);
   itemsTA.InsertGenre(produkt.Typ, produkt.ID);
   DataSet dataset = new DataSet();
   itemsTA.Adapter.Update(dataset);
}

The maxReceivedMessageSize attribute needs to be present on both sides of the service boundary, service and consumer. So you need to add a bindings Element into your client side config.

UPDATE

You need to include the binding from the web.config in your service app.config:

<bindings>
  <wsHttpBinding>
    <binding name="WSHttpBinding_IService" closeTimeout="00:01:00"
    openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
    bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
    maxBufferPoolSize="2000000" maxReceivedMessageSize="2000000"
    messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
    allowCookies="false">
      <readerQuotas maxDepth="2000000" maxStringContentLength="2000000" maxArrayLength="2000000"
      maxBytesPerRead="2000000" maxNameTableCharCount="2000000" />
      <reliableSession ordered="true" inactivityTimeout="00:10:00" enabled="false" />
      <security mode="Message">
        <transport clientCredentialType="Windows" proxyCredentialType="None" realm="" />
        <message clientCredentialType="Windows" negotiateServiceCredential="true"
        algorithmSuite="Default" establishSecurityContext="true" />
      </security>
    </binding>
  </wsHttpBinding>
</bindings>

Then you can reference this binding element in your service endpoint in your app.config by using the endpoint bindingConfiguration attribute, and in this case you set it to the same value as the name of the wsHttpBinding element, which is "WSHttpBinding_IService".

For example

<endpoint address="" 
          binding="wsHttpBinding" 
          contract="WcfDataLager.IService"
          bindingConfiguration="WSHttpBinding_IService">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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