Skip to content

Instantly share code, notes, and snippets.

@jonasnordlund
Last active February 23, 2023 11:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonasnordlund/3ea7fa401ffd7bcfa159cf1b08ebed37 to your computer and use it in GitHub Desktop.
Save jonasnordlund/3ea7fa401ffd7bcfa159cf1b08ebed37 to your computer and use it in GitHub Desktop.
Configuring a WCF service in code
public static class Wcf
{
private static WcfClient _server;
// Set via app.config
public static Uri Address
{
get; set;
}
// WcfClient is the Visual Studio generated WCF client code from the WCF server metadata endpoint.
public static WcfClient Connection
{
get
{
if (_server != null)
return _server;
_server = new WcfClient(WCFClientSettings.BinaryHttpBinding,
WCFClientSettings.GetEndpointAddress(Address));
_server.Endpoint.Behaviors.Add(new ReaderQuotaExtension());
_server.Open();
return _server;
}
}
public static class WCFClientSettings
{
public static Binding BinaryHttpBinding
{
get
{
TimeSpan timeout = TimeSpan.FromMinutes(1);
CustomBinding binaryHttpBinding = new CustomBinding
{
Name = "BinaryHttpBinding",
OpenTimeout = timeout,
CloseTimeout = timeout,
ReceiveTimeout = timeout,
SendTimeout = timeout
};
TransportBindingElement transport;
if (Server.Address.Scheme.Equals("https", StringComparison.OrdinalIgnoreCase))
{
transport = new HttpsTransportBindingElement
{
MaxBufferPoolSize = int.MaxValue,
MaxReceivedMessageSize = int.MaxValue
};
}
else
{
transport = new HttpTransportBindingElement
{
MaxBufferPoolSize = int.MaxValue,
MaxReceivedMessageSize = int.MaxValue
};
}
binaryHttpBinding.Elements.AddRange(new BindingElement[]
{
new BinaryMessageEncodingBindingElement
{
ReaderQuotas = new XmlDictionaryReaderQuotas
{
MaxDepth = 32,
MaxStringContentLength = 8192,
MaxArrayLength = 16384,
MaxBytesPerRead = 4096,
MaxNameTableCharCount = 16384
}
},
transport
});
return binaryHttpBinding;
}
}
public static EndpointAddress GetEndpointAddress(Uri uri)
{
EndpointAddress endpointAddress = new EndpointAddress(uri);
return endpointAddress;
}
}
public class ReaderQuotaExtension : IEndpointBehavior
{
public void Validate(ServiceEndpoint endpoint) { }
public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters) { }
public void ApplyDispatchBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.EndpointDispatcher endpointDispatcher)
{
ModifyDataContractSerializerBehavior(endpoint);
}
public void ApplyClientBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.ClientRuntime clientRuntime)
{
ModifyDataContractSerializerBehavior(endpoint);
}
private static void ModifyDataContractSerializerBehavior(ServiceEndpoint endpoint)
{
foreach (OperationDescription operation in endpoint.Contract.Operations)
{
var behavior = operation.Behaviors.Find<DataContractSerializerOperationBehavior>();
behavior.MaxItemsInObjectGraph = 2147483647;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment