c# wcf客户端调用服务端的接口提示错误:413 request entity too large

文章目录

        • 1.配置wcf服务的App.config文件
        • 2.给指定终结点增加绑定配置
        • 3.修改控制台寄宿程序的绑定
        • 4.重新生成项目测试

1.配置wcf服务的App.config文件

在system.serviceModel节点增加bindings,如下:

<bindings>
      <basicHttpBinding>
        <binding name="LargeDataTransferServicesBinding" maxReceivedMessageSize="2147483647" messageEncoding="Text" transferMode="Streamed" />
      </basicHttpBinding>
</bindings>

2.给指定终结点增加绑定配置

使用bindingConfiguration给终结点绑定,如下:

<service name="LookPictureService.LookPictureService">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8733/Design_Time_Addresses/LookPictureService/LookPictureService/" />
          </baseAddresses>
        </host>
        <!-- Service Endpoints -->
        <!-- Unless fully qualified, address is relative to base address supplied above -->
        <endpoint address="" binding="basicHttpBinding" contract="LookPictureService.ILookPictureService" bindingConfiguration="LargeDataTransferServicesBinding">
          <!-- 
              Upon deployment, the following identity element should be removed or replaced to reflect the 
              identity under which the deployed service runs.  If removed, WCF will infer an appropriate identity 
              automatically.
          -->
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <!-- Metadata Endpoints -->
        <!-- The Metadata Exchange endpoint is used by the service to describe itself to clients. -->
        <!-- This endpoint does not use a secure binding and should be secured or removed before deployment -->
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      </service>

3.修改控制台寄宿程序的绑定

给指定终结点增加绑定,如下:

using LookPictureService;
using System;
using System.ServiceModel;
using System.ServiceModel.Description;

namespace TestLookPictureService
{
    class Program
    {
        static void Main(string[] args)
        {
            // Step 1: Create a URI to serve as the base address.
            Uri baseAddress = new Uri("http://localhost:8734/Design_Time_Addresses/LookPictureService/CalculatorService/");
            Uri baseAddress1 = new Uri("http://localhost:8734/Design_Time_Addresses/LookPictureService/LookPictureService/");

            // Step 2: Create a ServiceHost instance.
            ServiceHost selfHost = new CustomServiceHost(typeof(CalculatorService), baseAddress);
            ServiceHost selfHost1 = new CustomServiceHost(typeof(LookPictureService.LookPictureService), baseAddress1);

            try
            {
                //修改默认Binding
                BasicHttpBinding binding = new BasicHttpBinding();
                binding.MaxBufferSize = 2147483647;
                binding.MaxReceivedMessageSize = 2147483647;

                // Step 3: Add a service endpoint.
                selfHost.AddServiceEndpoint(typeof(ICalculator), new BasicHttpBinding(), "CalculatorService");
                selfHost1.AddServiceEndpoint(typeof(ILookPictureService), binding, "LookPictureService");//使用修改的的Binding

                // Step 4: Enable metadata exchange.
                ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
                ServiceMetadataBehavior smb1 = new ServiceMetadataBehavior();
                smb.HttpGetEnabled = true;
                smb1.HttpGetEnabled = true;
                selfHost.Description.Behaviors.Add(smb);
                selfHost1.Description.Behaviors.Add(smb1);

                // Step 5: Start the service.
                selfHost.Open();
                selfHost1.Open();
                Console.WriteLine("The service is ready.");
                Console.WriteLine("The service1 is ready.");

                // Close the ServiceHost to stop the service.
                Console.WriteLine("Press  to terminate the service.");
                Console.WriteLine("Press  to terminate the service1.");
                Console.WriteLine();
                Console.ReadLine();
                selfHost.Close();
                selfHost1.Close();
            }
            catch (CommunicationException ce)
            {
                Console.WriteLine("An exception occurred: {0}", ce.Message);
                selfHost.Abort();
            }
        }
    }
}

4.重新生成项目测试

修改了wcf服务,与控制台寄宿程序,就解决了413 request entity too large问题,我这里是传输的4张图片,超过了默认的4M大小,经过上述配置,已经解决该问题。

你可能感兴趣的:(wcf,C#,.net,wcf,413,request,entity,large)