silverlight向wcf传递大于8192字节(8k)的字符串

默认情况下,silverlight在调用wcf时,如果传递的参数长度大于8192字节,即8k,会提示Not Found错误。
解决方法如下:
1、wcf服务端修改web.config 如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?xml version="1.0"?>
 
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
 
<configuration>
    <system.web>
        <compilation debug="true" targetFramework="4.0" />
    </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <!--注:此处的name值要跟下面的behaviorConfiguration值对应-->
        <behavior name="A">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
          <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <!--注1:此处的behaviorConfiguration值要跟上面的name值对应-->
      <!--注2:此处的name值不能随便修改,命名格式为:完全命名空间+类名 -->
      <service behaviorConfiguration="A" name="WCF_SL_8192.Web.WCF.HelloWorld">
        <!--注1:此处的contract值不能随便修改,命名格式为:完全命名空间+类名 -->
        <!--注2:此处的bindingConfiguration值要与下面 binding name中的name值对应-->
        <endpoint address="" bindingConfiguration="BBB" binding="basicHttpBinding" contract="WCF_SL_8192.Web.WCF.HelloWorld"/>     
      </service>
    </services>
    <bindings>
      <basicHttpBinding>
        <binding name="BBB" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">
          <!--name=随意命名,但要与上面的bindingConfiguration="BBB"对应 -->
          <readerQuotas maxStringContentLength="2147483647" maxArrayLength="2147483647"/>
          <security mode="None"></security>
        </binding>
      </basicHttpBinding>
    </bindings>
  </system.serviceModel>
</configuration>
附:wcf的代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
using System.ServiceModel;
 
namespace WCF_SL_8192.Web.WCF
{
    [ServiceContract]
    public class HelloWorld
    {
        [OperationContract]
        public int Test(string msg)
        {
            return msg.Length;
        }
    }
}
2、SL端修改ClientConfig如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<configuration>
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBinding_HelloWorld" maxBufferSize="2147483647"
             maxReceivedMessageSize="2147483647">
          <security mode="None" />
        </binding>
      </basicHttpBinding>
      <!--下面这个节点是关键-->
      <customBinding>
        <binding name="BasicHttpBinding_HelloWorld">
          <textMessageEncoding messageVersion="Default" writeEncoding="utf-8" />
          <httpTransport maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" />
        </binding>
      </customBinding>
    </bindings>
    <client>
      <endpoint address="http://localhost:1588/WCF/HelloWorld.svc"
          binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_HelloWorld"
          contract="WCF.HelloWorld" name="BasicHttpBinding_HelloWorld" />
    </client>
  </system.serviceModel>
</configuration>
附:SL的调用代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
using System;
using System.Windows;
using System.Windows.Controls;
using WCF_SL_8192.WCF;
 
namespace WCF_SL_8192
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
 
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
 
 
        }
 
        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            HelloWorldClient client = new HelloWorldClient();          
            client.TestCompleted += new EventHandler<TestCompletedEventArgs>(client_TestCompleted);
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            for (int i = 0; i < 100000; i++)
            {
                sb.Append("A");
            }
            client.TestAsync(sb.ToString());
        }
 
        void client_TestCompleted(object sender, TestCompletedEventArgs e)
        {
            MessageBox.Show(e.Result.ToString());
        }
    }
}
posted @   菩提树下的杨过  阅读(3190)  评论(4编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述
历史上的今天:
2010-06-29 Silverlight:Mouse Avoiding 躲避鼠标效果
点击右上角即可分享
微信分享提示