网上的解决方案:
出现这种错误,先去修改服务器端和客户端的MaxReceivedMessageSize值,如果还报错那么查看你的代码调用了是不是new了一个新的BasicHttpBinding对象,如果是new了新的那么你的webconfig根本就没有被调用,在new的时候要将 <binding name="DataSyncService" 中的name值作为参数传进去,或者是在代码里重新设置MaxReceivedMessageSize值
第一种
DataSyncServiceClient service = new DataSyncServiceClient(new BasicHttpBinding("DataSyncService"), new EndpointAddress(new Uri(con)));
第二种
DataSyncServiceClient service = new DataSyncServiceClient(new BasicHttpBinding(), new EndpointAddress(new Uri(con)));
(service.Endpoint.Binding as BasicHttpBinding).MaxReceivedMessageSize = int.MaxValue;
(service.Endpoint.Binding as BasicHttpBinding).MaxBufferPoolSize = int.MaxValue;
(service.Endpoint.Binding as BasicHttpBinding).MaxBufferSize = int.MaxValue;
我自己项目中的问题:
//SMC.Binding binding = U8ServiceClient.CreateDefaultBinding();
//string remoteAddress = U8ServiceClient.EndpointAddress.Uri.ToString();
//EndpointAddress endpoint = new EndpointAddress(remoteAddress);
//U8Client = new U8ServiceClient(binding, endpoint);
//SessionID = U8Client.StartSession();
我们之前是用上面的这个方法来的:即使我在服务器端设置了:
maxReceivedMessageSize="2147483647"
<dataContractSerializer maxItemsInObjectGraph="2147483647" />
也还是不起作用。
<?xml version="1.0"?> <configuration> <system.web> <compilation debug="true" targetFramework="4.0"> <assemblies> <add assembly="Interop.U8Login, Version=1.14.0.0, Culture=neutral, PublicKeyToken=79A4E7AD54EEABCA"/></assemblies></compilation> <customErrors mode="Off"/> </system.web> <appSettings> <add key="defaultConnection" value="sqlCon"/> <add key="DALAssemblyName" value="HZYA.DALSQLServer"/> <add key="DALFactoryType" value="HZYA.DALSQLServer.DALMSSqlFactory"/> <add key="SessionTimeOut" value="00:30:00"/> </appSettings> <connectionStrings> <!--<add name="sqlCon" connectionString="Data Source=CQY-PC\SQLEXPRESS2005;Initial Catalog=UFDATA_001_2010;User ID=sa;Password=sasa" providerName="System.Data.SqlClient"/> <add name="System" connectionString="Data Source=CQY-PC\SQLEXPRESS2005;Initial Catalog=UFSysTem;User ID=sa;Password=sasa" providerName="System.Data.SqlClient"/> <add name="Barcode" connectionString="Data Source=CQY-PC\SQLEXPRESS2005;Initial Catalog=Barcode;User ID=sa;Password=sasa" providerName="System.Data.SqlClient"/>--> <add name="sqlCon" connectionString="Data Source=wuyf\sql2005;Initial Catalog=UFDATA_001_2012;User ID=sa;Password=sasa" providerName="System.Data.SqlClient"/> <add name="System" connectionString="Data Source=wuyf\sql2005;Initial Catalog=UFSysTem;User ID=sa;Password=sasa" providerName="System.Data.SqlClient"/> <add name="Barcode" connectionString="Data Source=wuyf\sql2005;Initial Catalog=Barcode;User ID=sa;Password=sasa" providerName="System.Data.SqlClient"/> </connectionStrings> <system.serviceModel> <services> <service name="CommonService" behaviorConfiguration="ServiceBehavior"> <endpoint address="" binding="wsHttpBinding" contract="ICommonService" bindingConfiguration="LargeSizeWsHttpBinding"/> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> <endpoint address="basic" binding="basicHttpBinding" contract="ICommonService" bindingConfiguration="LargeSizeBasicHttpBinding"/> </service> <service name="BarcodeService" behaviorConfiguration="ServiceBehavior"> <endpoint address="" binding="wsHttpBinding" contract="IBarcodeService"/> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> <endpoint address="basic" binding="basicHttpBinding" contract="IBarcodeService"/> </service> <service name="PrintService" behaviorConfiguration="ServiceBehavior"> <endpoint address="" binding="wsHttpBinding" contract="IPrintService"/> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> <endpoint address="basic" binding="basicHttpBinding" contract="IPrintService"/> </service> <service name="LoginService" behaviorConfiguration="ServiceBehavior" > <!--<endpoint address="" binding="wsHttpBinding" contract="ILoginService" bindingConfiguration="NoneSecurity" /> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>--> <endpoint address="basic" binding="basicHttpBinding" contract="ILoginService"/> </service> <service name="U8Service" behaviorConfiguration="ServiceBehavior" > <endpoint address="" binding="wsHttpBinding" contract="IU8Service" /> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> <endpoint address="basic" binding="basicHttpBinding" contract="IU8Service"/> </service> <!--<service name="U8Service" behaviorConfiguration="ServiceBehavior" > <endpoint address="" binding="wsHttpBinding" contract="ILoginService" bindingConfiguration="NoneSecurity" /> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> <endpoint address="basic" binding="basicHttpBinding" contract="IU8Service"/> </service>--> </services> <bindings> <basicHttpBinding> <binding name="LargeSizeBasicHttpBinding" maxBufferPoolSize="2147483647" receiveTimeout="00:00:10" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647"> <readerQuotas maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxDepth="2147483647" maxNameTableCharCount="2147483647" maxStringContentLength="2147483647"/> </binding> </basicHttpBinding> <wsHttpBinding> <binding name="LargeSizeWsHttpBinding" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647"> <readerQuotas maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxDepth="2147483647" maxNameTableCharCount="2147483647" maxStringContentLength="2147483647"/> </binding> <binding name="NoneSecurity" receiveTimeout="00:00:50" maxBufferPoolSize="12000000" maxReceivedMessageSize="12000000" useDefaultWebProxy="false"> <readerQuotas maxStringContentLength="12000000" maxArrayLength="12000000"/> <reliableSession enabled="true"/> <security mode="None"> <message establishSecurityContext="true"/> </security> </binding> </wsHttpBinding> </bindings> <behaviors> <serviceBehaviors> <behavior name="ServiceBehavior"> <serviceMetadata httpGetEnabled="true"/> <serviceDebug includeExceptionDetailInFaults="true"/> <dataContractSerializer maxItemsInObjectGraph="2147483647" /> </behavior> <behavior name=""> <serviceMetadata httpGetEnabled="true"/> <serviceDebug includeExceptionDetailInFaults="false"/> </behavior> </serviceBehaviors> </behaviors> <serviceHostingEnvironment multipleSiteBindingsEnabled="true"/> </system.serviceModel> <system.webServer> <directoryBrowse enabled="true" showFlags="Date, Time, Size, Extension, LongDate"/> </system.webServer> </configuration>
直到看到网上的这篇文章后,我修改了我的客户端 binding 类型为 basehttpbinding 后,才解决了。
代码如下:
BasicHttpBinding binding = new BasicHttpBinding(); binding.MaxReceivedMessageSize = int.MaxValue; binding.MaxBufferPoolSize = int.MaxValue; binding.MaxBufferSize = int.MaxValue; string remoteAddress = U8ServiceClient.EndpointAddress.Uri.ToString(); EndpointAddress endpoint = new EndpointAddress(remoteAddress); U8Client = new U8ServiceClient(binding, endpoint);