MaxReceivedMessageSize在WCF的控制台应用程序托管服务
问题描述:
我在我的控制台应用程序托管WCF服务如下:MaxReceivedMessageSize在WCF的控制台应用程序托管服务
static void Main(string[] args)
{
Uri baseAddress = new Uri("http://localhost:8080/Test");
// Create the ServiceHost.
using (ServiceHost host = new ServiceHost(typeof(TestService), baseAddress))
{
// Enable metadata publishing.
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
host.Description.Behaviors.Add(smb);
host.Open();
Console.WriteLine("The Test service is ready at {0}", baseAddress);
Console.WriteLine("Press <Enter> to stop the service.");
Console.ReadLine();
// Close the ServiceHost.
host.Close();
}
}
我有一个客户在Windows应用商店(WinRT的)应用程序。我越来越
“(413)请求实体过大”
试图通过一个大的字节数组时。我如何通过代码在我的服务中设置MaxReceivedMessageSize
?
答
你需要创建一个绑定,然后指定MaxReceivedMessageSize:
Uri baseAddress = new Uri("http://localhost:8080/Test");
var serviceHost = new ServiceHost(typeof(TestService));
var basicHttpBinding = new BasicHttpBinding();
basicHttpBinding.MaxReceivedMessageSize = int.MaxValue;
serviceHost.AddServiceEndpoint(typeof(IService), basicHttpBinding, baseAddress);
答
如果你的字节数组太大,那么你可以随时将其分割到更小的块,并把这些在一个循环。您甚至可能希望在另一个线程中执行此操作并更新用户界面的进度。