您确实有绑定,只是WebChannelFactory
它会自动为您设置。事实证明,这个工厂总是创建一个带有 的端点WebHttpBinding
,因此您可以在从它创建第一个通道之前更改绑定属性 - 请参见下面的示例。
public class StackOverflow_7013700
{
[ServiceContract]
public interface ITest
{
[OperationContract]
string GetString(int size);
}
public class Service : ITest
{
public string GetString(int size)
{
return new string('r', size);
}
}
public static void Test()
{
string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
WebServiceHost host = new WebServiceHost(typeof(Service), new Uri(baseAddress));
host.Open();
Console.WriteLine("Host opened");
WebChannelFactory<ITest> factory = new WebChannelFactory<ITest>(new Uri(baseAddress));
(factory.Endpoint.Binding as WebHttpBinding).ReaderQuotas.MaxStringContentLength = 100000;
ITest proxy = factory.CreateChannel();
Console.WriteLine(proxy.GetString(100).Length);
try
{
Console.WriteLine(proxy.GetString(60000).Length);
}
catch (Exception e)
{
Console.WriteLine("{0}: {1}", e.GetType().FullName, e.Message);
}
((IClientChannel)proxy).Close();
factory.Close();
Console.Write("Press ENTER to close the host");
Console.ReadLine();
host.Close();
}
}