[转]silverlight向wcf传递大于8192字节(8k)的字符串
默认情况下,silverlight在调用wcf时,如果传递的参数长度大于8192字节,即8k,会提示Not Found错误。
解决方法如下:
1、wcf服务端修改web.config 如下:
01 |
<?xml version="1.0"?> |
02 |
|
03 |
<!-- |
04 |
For more information on how to configure your ASP.NET application, please visit |
06 |
--> |
07 |
|
08 |
<configuration> |
09 |
<system.web> |
10 |
<compilation debug="true" targetFramework="4.0" /> |
11 |
</system.web> |
12 |
<system.serviceModel> |
13 |
<behaviors> |
14 |
<serviceBehaviors> |
15 |
<!--注:此处的name值要跟下面的behaviorConfiguration值对应--> |
16 |
<behavior name="A"> |
17 |
<serviceMetadata httpGetEnabled="true"/> |
18 |
<serviceDebug includeExceptionDetailInFaults="false"/> |
19 |
<dataContractSerializer maxItemsInObjectGraph="2147483647"/> |
20 |
</behavior> |
21 |
</serviceBehaviors> |
22 |
</behaviors> |
23 |
<services> |
24 |
<!--注1:此处的behaviorConfiguration值要跟上面的name值对应--> |
25 |
<!--注2:此处的name值不能随便修改,命名格式为:完全命名空间+类名 --> |
26 |
<service behaviorConfiguration="A" name="WCF_SL_8192.Web.WCF.HelloWorld"> |
27 |
<!--注1:此处的contract值不能随便修改,命名格式为:完全命名空间+类名 --> |
28 |
<!--注2:此处的bindingConfiguration值要与下面 binding name中的name值对应--> |
29 |
<endpoint address="" bindingConfiguration="BBB" binding="basicHttpBinding" contract="WCF_SL_8192.Web.WCF.HelloWorld"/> |
30 |
</service> |
31 |
</services> |
32 |
<bindings> |
33 |
<basicHttpBinding> |
34 |
<binding name="BBB" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647"> |
35 |
<!--name=随意命名,但要与上面的bindingConfiguration="BBB"对应 --> |
36 |
<readerQuotas maxStringContentLength="2147483647" maxArrayLength="2147483647"/> |
37 |
<security mode="None"></security> |
38 |
</binding> |
39 |
</basicHttpBinding> |
40 |
</bindings> |
41 |
</system.serviceModel> |
42 |
</configuration> |
附:wcf的代码
01 |
using System.ServiceModel; |
02 |
|
03 |
namespace WCF_SL_8192.Web.WCF |
04 |
{ |
05 |
[ServiceContract] |
06 |
public class HelloWorld |
07 |
{ |
08 |
[OperationContract] |
09 |
public int Test(string msg) |
10 |
{ |
11 |
return msg.Length; |
12 |
} |
13 |
} |
14 |
} |
2、SL端修改ClientConfig如下:
01 |
<configuration> |
02 |
<system.serviceModel> |
03 |
<bindings> |
04 |
<basicHttpBinding> |
05 |
<binding name="BasicHttpBinding_HelloWorld" maxBufferSize="2147483647" |
06 |
maxReceivedMessageSize="2147483647"> |
07 |
<security mode="None" /> |
08 |
</binding> |
09 |
</basicHttpBinding> |
10 |
<!--下面这个节点是关键--> |
11 |
<customBinding> |
12 |
<binding name="BasicHttpBinding_HelloWorld"> |
13 |
<textMessageEncoding messageVersion="Default" writeEncoding="utf-8" /> |
14 |
<httpTransport maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" /> |
15 |
</binding> |
16 |
</customBinding> |
17 |
</bindings> |
18 |
<client> |
19 |
<endpoint address="http://localhost:1588/WCF/HelloWorld.svc" |
20 |
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_HelloWorld" |
21 |
contract="WCF.HelloWorld" name="BasicHttpBinding_HelloWorld" /> |
22 |
</client> |
23 |
</system.serviceModel> |
24 |
</configuration> |
附:SL的调用代码
01 |
using System; |
02 |
using System.Windows; |
03 |
using System.Windows.Controls; |
04 |
using WCF_SL_8192.WCF; |
05 |
|
06 |
namespace WCF_SL_8192 |
07 |
{ |
08 |
public partial class MainPage : UserControl |
09 |
{ |
10 |
public MainPage() |
11 |
{ |
12 |
InitializeComponent(); |
13 |
|
14 |
this.Loaded += new RoutedEventHandler(MainPage_Loaded); |
15 |
|
16 |
|
17 |
} |
18 |
|
19 |
void MainPage_Loaded(object sender, RoutedEventArgs e) |
20 |
{ |
21 |
HelloWorldClient client = new HelloWorldClient(); |
22 |
client.TestCompleted += new EventHandler<TestCompletedEventArgs>(client_TestCompleted); |
23 |
System.Text.StringBuilder sb = new System.Text.StringBuilder(); |
24 |
for (int i = 0; i < 100000; i++) |
25 |
{ |
26 |
sb.Append("A"); |
27 |
} |
28 |
client.TestAsync(sb.ToString()); |
29 |
} |
30 |
|
31 |
void client_TestCompleted(object sender, TestCompletedEventArgs e) |
32 |
{ |
33 |
MessageBox.Show(e.Result.ToString()); |
34 |
} |
35 |
} |
36 |
} |

浙公网安备 33010602011771号