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

2018-01-23 17:11:19 浏览数 (1)

默认情况下,silverlight在调用wcf时,如果传递的参数长度大于8192字节,即8k,会提示Not Found错误。

解决方法如下:

1、wcf服务端修改web.config 如下:

代码语言:javascript复制
<?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的代码

代码语言:javascript复制
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如下:

代码语言:javascript复制
<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的调用代码

代码语言:javascript复制
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());
        }
    }
}

0 人点赞