2012-01-01 94 views
1

我试图发送图像到我的WCF,当我得到这个。远程服务器返回一个意外的响应:(400)错误的请求,WCF

远程服务器返回了意外的响应:(400)错误的请求。

一切工作正常发送到WCF和图像不是那么大〜90kb。我在这方面发现了很多线索,但没有什么能帮助我。我试图增加大小限制,但这不起作用。

的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> 

页代码。

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代码。

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); 
} 

回答

4

maxReceivedMessageSize属性需要存在于服务边界,服务和消费者的两端。所以你需要在你的客户端配置中添加一个绑定元素。

UPDATE

您需要包括从web.config绑定在你的服务的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> 

然后你就可以在你的服务端点在您的应用程序引用此绑定元素.config通过使用端点bindingConfiguration属性,并在这种情况下,您将其设置为与wsHttpBinding元素的名称相同的值,即“WSHttpBinding_IService”。

例如

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

你能告诉我一个代码示例,我真的不知道你的服务边界,服务和消费的意思。 – Frozendragon 2012-01-01 18:15:19

+0

已更新我的原始答案。为了您的信息,服务是公共运营的事情,消费者是要调用运营的东西,而服务边界是它们之间的分离。 – 2012-01-02 11:17:11

+0

+1 ...救了我很多挫折!谢谢。 – Sam 2012-09-25 19:23:25

相关问题