WCF seems ignore web.config and gives 413: Entity too large - c#

I have a WCF web services which keep giving me a 413 response when I post a large JSON request to it, I have added the maximum limit into the web.config but it seems they are ignored, I would like to know is there anything wrong in my web.config?
Here is my web.config
<system.serviceModel>
<services>
<service name="myService">
<endpoint address="http://localhost/myService" binding="webHttpBinding"
bindingConfiguration="myBinding" name="myService"
contract="ImyService" />
</service>
</services>
<bindings>
<webHttpBinding>
<binding name="myBinding" maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" >
<readerQuotas maxStringContentLength="2147483647" maxArrayLength="2147483647" />
<security mode="Transport" />
</binding>
</webHttpBinding>
</bindings>
</system.serviceModel>
Thanks!
EDITED:
I have "solved" the problem, by adding a new binding with name = "", which overwrite the default binding setting, and everything goes fine.
Being said, myBinding is actually being ignored or not bound to my service endpoint...
Can anyone figuring out the reason behind??

Related

C# .NetTcpBinding 2 services at same port 5000

I am hosting two services using NetTcpBinding on same port 5000 like below.
In my service app.config I have like below
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="netTcpBindingConfiguration"
closeTimeout="00:10:00"
openTimeout="00:10:00"
receiveTimeout="00:10:00"
sendTimeout="00:10:00"
maxBufferSize="2147483647"
maxBufferPoolSize="2147483647"
maxReceivedMessageSize="2147483647">
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
maxArrayLength="2147483647" maxBytesPerRead="2147483647"
maxNameTableCharCount="2147483647" />
</binding>
</netTcpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="netTcpServiceBehavior">
<serviceDebug includeExceptionDetailInFaults="True" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<!--First Service [net.tcp://localhost:5000/MyService/FirstService] -->
<service behaviorConfiguration="netTcpServiceBehavior" name="FirstserviceLib">
<endpoint address="" binding="netTcpBinding" contract="IFirstService"
bindingConfiguration="netTcpBindingConfiguration" />
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:5000/MyService/FirstService" />
</baseAddresses>
</host>
</service>
<!--Second Service [net.tcp://localhost:5000/MyService/SecondService] -->
<service behaviorConfiguration="netTcpServiceBehavior" name="SecondserviceLib">
<endpoint address="" binding="netTcpBinding" contract="ISecondService"
bindingConfiguration="netTcpBindingConfiguration" />
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:5000/MyService/SecondService" />
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
I am using console application to self-host like below
static void Main(string[] args)
{
ServiceHost firstHost = new ServiceHost(typeof(IFirstService));
firstHost.Open();
ServiceHost secondHost = new ServiceHost(typeof(ISecondService));
secondHost.Open();
Console.WriteLine("Services Hosted");
}
When I run my console application I get Service Hosted message. I feel my services are running.
On client side, I have below in my app.config
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="netTcpBindingConfiguration"
closeTimeout="00:10:00"
openTimeout="00:10:00"
receiveTimeout="00:10:00"
sendTimeout="00:10:00"
maxBufferSize="2147483647"
maxBufferPoolSize="2147483647"
maxReceivedMessageSize="2147483647">
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
maxArrayLength="2147483647" maxBytesPerRead="2147483647"
maxNameTableCharCount="2147483647" />
</binding>
</netTcpBinding>
</bindings>
<client>
<endpoint address="net.tcp://localhost:5000/MyService/SecondService"
binding="netTcpBinding"
contract="ISecondService"
name="NetTcpBinding_SecondService"
bindingConfiguration="netTcpBindingConfiguration" />
</client>
</system.serviceModel>
I am calling a method in SecondService like below
string path = System.Reflection.Assembly.GetExecutingAssembly().Location;
Configuration config = ConfigurationManager.OpenExeConfiguration(path);
ConfigurationChannelFactory<ISecondService> chn =
new ConfigurationChannelFactory<ISecondService>(
"NetTcpBinding_SecondService",
config,
new EndpointAddress("net.tcp://localhost:5000/MyService/SecondService"));
ISecondService facade = chn.CreateChannel();
string fullName = facade.GetName();
I am getting exception like below
System.ServiceModel.EndpointNotFoundException: There was no endpoint listening at net.tcp://localhost:5000/MyService/SecondService that could accept the message. This is often caused by an incorrect address or SOAP action
Since I am working on Windows 10 I enabled and running below service
Note: If I host only 1 service everything is good. But when I add SecondService it is not working. Please let me know what mistake I am making.
I can test IFirstService but not ISecondService
UPDATE 1:
I opened command prompt and went to C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.7.2 Tools
I gave svcutil net.tcp://localhost:5000/MyService/SecondService
I do not get any error message like There was no endpoint listening at blah blah...
but when I give svcutil net.tcp://localhost:5000/MyService/FirstService
I do get error message like There was no endpoint listening at blah blah...
It seems my service itself is not hosted properly.
It works fine now.
In my consolehost app.config I made sure there are no spaces between <service> in <services> section.
I removed all references to FirstService in Main method in Program.cs of my console host and also in App.config.
I ran my consolehost exe and now ISecondService is hosted
I ran svcutil net.tcp://localhost:5000/MyService/SecondService and now I do not see endpoint not listening error message.
On client side, I was able to execute below line which threw exception in GetName()
ISecondService facade = chn.CreateChannel();
string fullName = facade.GetName();
I fixed my code and re-executed it and this time it was fine.
Now I re-added references to FirstService in Console Host app.config and in Main() of Program.cs
I ran consolehost exe to host both services and now both below did not show me no endpoint message
svcutil net.tcp://localhost:5000/MyService/FirstService
svcutil net.tcp://localhost:5000/MyService/SecondService
Now everything works fine.

Object values defaulting

I converted my WCF Services today to use WSHttpBinding instead of BasicHttpBinding. It is still in the development stage and thus I am using self signed certificates. The example that I followed is located Here
After finally getting the code to work like the example illustrates (follow the examples of the configs in the code that one can download), I decided to proceed to use Channel Factories like I did before.
Now when I make a call to a WCF method, I can clearly see that the object that I am sending is populated with the expected values, but if I step into the WCF side, the values are their defaults. For example Guid's will be Empty Guid's and int's will be 0. Always.
Any idea what might be causing this? here is some of my code:
In the web.config:
<add key="ClientCertificate" value="WcfClient" />
<add key="ServerCertificate" value="WcfServer" />
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="CustomBehavior">
<clientCredentials>
<clientCertificate findValue="WcfClient"
x509FindType="FindBySubjectName"
storeLocation="CurrentUser"
storeName="My" />
<serviceCertificate>
<authentication certificateValidationMode="PeerTrust"/>
</serviceCertificate>
</clientCredentials>
</behavior>
</endpointBehaviors>
</behaviors>
<bindings>
<wsHttpBinding>
<binding name="WSHttpBinding_IDocumentsService" closeTimeout="00:10:00"
openTimeout="00:10:00" sendTimeout="00:10:00" maxBufferPoolSize="2147483647"
maxReceivedMessageSize="2147483647" messageEncoding="Mtom" allowCookies="true">
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
<security mode="Message">
<transport clientCredentialType="Windows" proxyCredentialType="None"
realm="" />
<message clientCredentialType="Certificate" negotiateServiceCredential="true"
algorithmSuite="Default" establishSecurityContext="true" />
</security>
</binding>
</wsHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:58790/DocumentsService.svc"
binding="wsHttpBinding"
bindingConfiguration="WSHttpBinding_IDocumentsService"
contract="DocumentsService.IDocumentsService"
name="WSHttpBinding_IDocumentsService"
behaviorConfiguration="CustomBehavior">
<identity>
<dns value="WcfServer" />
</identity>
</endpoint>
</client>
</system.serviceModel>
This is my channel factory
public static class ServiceObjects
{
public static IDocumentsService DocumentsSVC { get { return GetDocServiceClient(); } }
#region Private Members
private static WSHttpBinding _DMBinding = new WSHttpBinding("WSHttpBinding_IDocumentsService");
private static EndpointIdentity _DMIdentity = EndpointIdentity.CreateDnsIdentity(ConfigurationManager.AppSettings.Get("ServerCertificate"));
private static EndpointAddress _DMEndpoint = new EndpointAddress(new Uri(ConfigurationManager.AppSettings.Get("DocumentsService")), _DMIdentity);
private static IDocumentsService GetDocServiceClient()
{
ChannelFactory<IDocumentsService> _docSvcFactory = new ChannelFactory<IDocumentsService>(_DMBinding, _DMEndpoint);
_docSvcFactory.Credentials.ServiceCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.PeerOrChainTrust;
_docSvcFactory.Credentials.ClientCertificate.SetCertificate(StoreLocation.CurrentUser, StoreName.My, X509FindType.FindBySubjectName, ConfigurationManager.AppSettings.Get("ClientCertificate"));
_docSvcFactory.Credentials.ServiceCertificate.SetDefaultCertificate(StoreLocation.CurrentUser, StoreName.My, X509FindType.FindBySubjectName, ConfigurationManager.AppSettings.Get("ServerCertificate"));
return _docSvcFactory.CreateChannel();
}
#endregion
}
When I call the service, on the Client side for example:
private static Guid _UserID = (HttpContext.Current.User as Titan.Web.Classes.Identity.CustomPrincipal).UserId;
ServiceObjects.DocumentsSVC.GetDocumentsByFolderID(new DocumentRequest { CurrentUserID = _UserID })
I can see _UserID is populated, but on the server side it's not.
This is in my service's config
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="wsHttpEndpointBinding" closeTimeout="00:10:00" openTimeout="00:10:00" sendTimeout="00:10:00" allowCookies="true" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" messageEncoding="Mtom">
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
<security>
<message clientCredentialType="Certificate" />
</security>
</binding>
</wsHttpBinding>
</bindings>
<services>
<service name="Titan.WCF.Documents.DocumentsService" behaviorConfiguration="DocumentsServiceBehavior">
<endpoint address="" binding="wsHttpBinding" bindingConfiguration="wsHttpEndpointBinding" contract="Titan.WCF.Documents.IDocumentsService">
<!--
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>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="DocumentsServiceBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
<dataContractSerializer maxItemsInObjectGraph="2147483647" />
<serviceCredentials>
<clientCertificate>
<!-- Remove the NoCheck in production, this is only for when we use a self signed cert -->
<authentication certificateValidationMode="PeerOrChainTrust" revocationMode="NoCheck" />
</clientCertificate>
<serviceCertificate findValue="WCfServer"
storeLocation="CurrentUser"
storeName="My"
x509FindType="FindBySubjectName" />
</serviceCredentials>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
Of course, since you've changed your configuration in the service you need to update the service reference so that the client too can update its configuration on its side. Unless you do that the client will keep calling the service with old configuration which it reads from its config file while the service runs with the new configuration settings.
It may be tiring, but it's the way it is.
It seems the issue lies in the way you make a call to the service using the following code:
ServiceObjects.DocumentsSVC.GetDocumentsByFolderID(new DocumentRequest { CurrentUserID = _UserID });
Here you are trying to invoke the service function without creating a proxy object of the service. And that is because you have written a static class in the service.
In a WCF service you can't use static classes. You need to create an instance of the class (service proxy object) and then invoke the service function.
I am not sure why this would make a difference, but it did.
I needed to update my service references.
A rookie mistake I guess, but why would that make a difference if the only thing I did was to change the bindings, endpoints etc?

WCF - StreamedResponse - minimal latency

I'm trying to stream live data using WCF streaming between SilverLight 5 application and .NET 4 server. What I need is minimal latency between the server and the client. However, it looks like WCF is buffering data before sending it. I'm following the example from this site: http://blogs.msdn.com/b/carlosfigueira/archive/2010/07/08/using-transfermode-streamedresponse-to-download-files-in-silverlight-4.aspx. I've implemented custom stream and I see that I get e.g. 10 reads of small chunks (less than 400 bytes) during one second but on the wire the server returns data every 10 seconds (it returns 32776 bytes - that's the number I get from Wireshark). I want the server to return data as soon as it is ready or at least set the buffer to very small value e.g. 512 bytes.
I tried to change client and server configuration but with no luck. My configuration looks like this:
Client
<client>
<endpoint address=""
binding="customBinding" bindingConfiguration="CustomBinding_IWcfDownloadService_StreamedResponse"
contract="IWcfDownloadService" name="CustomBinding_IWcfDownloadService_StreamedResponse" />
</client>
<bindings>
<customBinding>
<binding name="CustomBinding_IWcfDownloadService_StreamedResponse">
<binaryMessageEncoding />
<httpTransport maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" transferMode="StreamedResponse" />
</binding>
</customBinding>
</bindings>
Server
<services>
<service name="WcfDownloadService">
<endpoint address="" binding="customBinding" bindingConfiguration="customBinding0" contract="IWcfDownloadService" />
</service>
</service>
<bindings>
<customBinding>
<binding name="customBinding0">
<binaryMessageEncoding />
<httpTransport transferMode="StreamedResponse" />
</binding>
</customBinding>
</bindings>

How can I configure a TransportSecurityBindingElement for a CustomBinding within a web.config?

I have the following (reduced) code that I'd like to configure through a web.config
var security = new TransportSecurityBindingElement();
security.EndpointSupportingTokenParameters.SignedEncrypted.Add(new UserNameSecurityTokenParameters());
var binding = new CustomBinding(security);
Is it possible to configure this custom binding using the web config? If it is possible how could I configure the endpoint supporting token parameters? I've tried The following config, however it does not validate against DotNetConfig.xsd:
<system.serviceModel>
<bindings>
<customBinding>
<binding name="SomeBinding" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00">
<transportSecurity> <!-- Fails validation -->
<!-- How do I configure the EndpointSupportingTokenParameters -->
</transportSecurity>
</binding>
</customBinding>
</bindings>
<client>
...
</client>
</system.serviceModel>
I think you just use customBinding/security, perhaps with an authenticationMode of UserNameOverTransport:
<system.serviceModel>
<bindings>
<customBinding>
<binding>
<security authenticationMode="UserNameOverTransport" />
</binding>
</customBinding>
</bindings>
</system.serviceModel>
Failing that, you could try adding the token manually to issuedTokenParameters:
<system.serviceModel>
<bindings>
<customBinding>
<binding>
<security authenticationMode="UserNameOverTransport">
<issuedTokenParameters tokenType="http://schemas.microsoft.com/ws/2006/05/identitymodel/tokens/UserName" />
</security>
</binding>
</customBinding>
</bindings>
</system.serviceModel>
(tokenType stolen from UserNameSecurityTokenParameters)
To be honest, it's probably so much hassle that it's easier to just do it in code. If you need to to be different per environment, define the factory in the config and use DI to use it to create the binding.

WCF - The maximum nametable character count quota (16384) has been exceeded while reading XML data

I'm having a WCF Service that uses wsHttpBinding. The server configuration is as follows :
<bindings>
<wsHttpBinding>
<binding name="wsHttpBinding" maxBufferPoolSize="2147483647"
maxReceivedMessageSize="2147483647">
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
<security mode="None">
<transport clientCredentialType="Windows" proxyCredentialType="None"
realm="" />
<message clientCredentialType="Windows" negotiateServiceCredential="true"
algorithmSuite="Default" establishSecurityContext="true" />
</security>
</binding>
</wsHttpBinding>
</bindings>
At the client side I'm including the Service reference of the WCF-Service. It works great if I have limited functions say 90 Operation Contract in my IService but if add one more OperationContract than I'm unable to Update the Service reference nor i'm able to add that service reference. In this article it's mentioned that by changing those config files(i.e devenv.exe.config, WcfTestClient.exe.config and SvcUtil.exe.config) it will work but even including those bindings in those config files still that error pops up saying
There was an error downloading 'http://10.0.3.112/MyService/Service1.svc/mex'.
The request failed with HTTP status 400: Bad Request.
Metadata contains a reference that cannot be resolved: 'http://10.0.3.112/MyService/Service1.svc/mex'.
There is an error in XML document (1, 89549).
The maximum nametable character count quota (16384) has been exceeded while reading XML data. The nametable is a data structure used to store strings encountered during XML processing - long XML documents with non-repeating element names, attribute names and attribute values may trigger this quota. This quota may be increased by changing the MaxNameTableCharCount property on the XmlDictionaryReaderQuotas object used when creating the XML reader. Line 1, position 89549.
If the service is defined in the current solution, try building the solution and adding the service reference again.
Any idea how to solve this????
Try the following:
In the installation directory of your Visual Studio where devenv.exe is located (e.g. C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE) add this section to the devenv.exe.cofig
<system.serviceModel>
<client>
<endpoint binding="customBinding" bindingConfiguration="largeServiceBinding" contract="IMetadataExchange" name="http" />
</client>
<bindings>
<customBinding>
<!-- NOTE: The binding name must be the same as specified in the config file of the wcf service -->
<binding name="largeServiceBinding" >
<textMessageEncoding>
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
</textMessageEncoding>
<httpTransport transferMode="Buffered" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647"/>
</binding>
</customBinding>
</bindings>
</system.serviceModel>
in the app.config of your WCF-service add the same binding:
<bindings>
<customBinding >
<!-- NOTE: The binding name must be the same as specified in the devenv.exe.config file located ..\Common7\IDE folder of the VS installation directory -->
<binding name="largeServiceBinding" >
<textMessageEncoding>
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647"
maxNameTableCharCount="2147483647" />
</textMessageEncoding>
<httpTransport transferMode="Buffered" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647"/>
</binding>
</customBinding>
</bindings>
Note that the name attribute of the binding tags from the two files must match (e.g. largeServiceBinding)
Finally add the following mex endpoint into your service tag:
<endpoint address="mex" binding="customBinding" contract="IMetadataExchange" bindingName="testBinding" bindingConfiguration="largeServiceBinding" name="http"/>
this may look like this:
<services>
<service behaviorConfiguration="MyServiceBehavior"
name="MyService.MyService">
<endpoint address="" binding="wsHttpBinding" contract="MyService.IMyService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="customBinding" contract="IMetadataExchange" bindingName="testBinding" bindingConfiguration="largeServiceBinding" name="http"/>
<host>
<baseAddresses>
<add baseAddress="http://localhost:8731/Design_Time_Addresses/MyService/MyService/" />
</baseAddresses>
</host>
</service>
</services>
I know it has been a while, but I got the same problem and found other (simpler) solution in codeproject
In the solution given there the values are set in the code rather than the .config file.
BasicHttpBinding binding = new BasicHttpBinding();
binding.Security.Mode = BasicHttpSecurityMode.Transport;
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;
binding.MaxReceivedMessageSize = 50000000;
binding.ReaderQuotas.MaxArrayLength = 50000000;
binding.ReaderQuotas.MaxStringContentLength = 50000000;
binding.ReaderQuotas.MaxNameTableCharCount = 50000000;
EndpointAddress endpoint = new EndpointAddress(new Uri("https://server/EWS/Exchange.asmx"));
ExchangeServicePortTypeClient ews = new ExchangeServicePortTypeClient(binding, endpoint);
However, I changed the values in the relevant values in the .config file ( in both the <binding> and the <readerQuotas> sections) and solved the problem (rather than adding custom bindings):
<binding name="ITransactionProcessor" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferSize="50000000" maxBufferPoolSize="524288" maxReceivedMessageSize="50000000"
messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="50000000" maxArrayLength="50000000"
maxBytesPerRead="4096" maxNameTableCharCount="50000000" />
<security mode="TransportWithMessageCredential">
<transport clientCredentialType="None" proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
I hope this will help somebody :)
One thing to recognize is that the message refers to the svcutil reader quotas not the service ones! Svcutil has a limit on how much metadata it can read. This limit can be changed with a config file. The solution is to create a config file for svcutil and place it in the same folder as the tool. Next time you run svcutil, the config file values will be taken into account.
http://geekswithblogs.net/claraoscura/archive/2007/08/20/114806.aspx
in your app.config or dll.config on the the client add:
<configuration>
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="myMex" maxReceivedMessageSize="1024000"> <!-- modify this to avoid stupid error -->
<readerQuotas maxNameTableCharCount="163840" /> <!-- DO NOT touch this one -->
<security mode="None" />
</binding>
</netTcpBinding>
</bindings>
...
<client>
<endpoint binding="netTcpBinding" bindingConfiguration="myMex"
contract="IMetadataExchange" name="net.tcp" />
...
</client>
</system.serviceModel>
</configuration>
And there you go!
This is one of the really annoying things with WCF and as often google just yields you alot of bs. Wasted tons of time with this.

Categories

Resources