How to increase maxquerystringlength and maxquerylength in wcf rest services? - wcf

I had written this code in wcf service web config file,i had communicating my sql server from android application using this service,i am sending request from android application using httppost method but its showing querystring is beyond the length. Can we increase length by using any other property,because i want to send larger input so that is there any chance to send or else i have to restrict my query string
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.web>
<httpRuntime maxQueryStringLength="2097151" maxUrlLength="2097151"/>
</system.web>
<connectionStrings >
<add name="DBConnection" connectionString="Integrated Security=true;Data Source=LENOVO\SQLEXPRESS;Initial Catalog=Order160415" providerName="System.Data.SqlClient"/>
</connectionStrings>
<system.serviceModel>
<services>
<service name="JsonWcfService.GetEmployees" behaviorConfiguration="EmpServiceBehaviour">
<endpoint address ="" binding="webHttpBinding" contract="JsonWcfService.IGetEmployees" behaviorConfiguration="web">
</endpoint>
<host>
<baseAddresses>
<add baseAddress="http://localhost/" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="EmpServiceBehaviour">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="2147483647" />
</requestFiltering>
</security>
</system.webServer>
</configuration>

You can define a binding configuration for the webHttpBinding and assign it to the endpoint, just like any other binding. To do this, add something like the following to your <system.serviceModel> section in the config file:
<bindings>
<webHttpBinding>
<binding name="MyWebHttpBinding"
maxBufferPoolSize="2097151"
maxBufferSize="2097151"
maxReceivedMessageSize="2097151">
<readerQuotas maxStringContentLength="2097151" />
</binding>
</webHttpBinding>
</bindings>
I set the values equal to what you set in the <httpRuntime> element in your posted config.
The final step is to assign this binding configuration to your service endpoint using the bindingConfiguration attribute in the service element:
<endpoint address=""
binding="webHttpBinding"
bindingConfiguration="MyWebHttpBinding"
contract="JsonWcfService.IGetEmployees"
behaviorConfiguration="web">
To sum up:
Create a binding configuration definition in the web.config file with the values you want.
Assign the binding configuration definition (using the name attribute) to the bindingConfiguration attribute in the <service> element.
In the absence of a binding configuration definition (and its assignment to an endpoint), WCF will use the default values for the binding protocol specified. In this case, the default values would be 524,288 for maxBufferSize and maxBufferPoolSize, 65,536 for maxReceivedMessageSize and 8,192 for maxStringContentLength, which are all smaller than the 2,097,151 limit you set in the <httpRuntime> element.
These settings are specific to the WCF service (or client, if it's in the client config), so even if IIS will accept the larger message, WCF will reject it if it exceeds the limits defined by the binding.
For more information, see on MSDN.

Related

Could not find default endpoint element while acessing WCF service

I am unable to consume the wcf restful service hosted in IIS.i.e at http://192.168.100.87:7001/ProductRESTService.svc/.
I have created a client application and added the service reference and created the proxy for service and accesing the using prxoy.
The error is:
Could not find default endpoint element that references contract 'ServiceReference2.IProductRESTService' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element.
My web.config file of service is following.
<system.serviceModel>
<services>
<service name="MyRESTService.ProductRESTService" behaviorConfiguration="serviceBehavior">
<endpoint address=""
binding="webHttpBinding"
contract="MyRESTService.IProductRESTService"
behaviorConfiguration="web"></endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="serviceBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
hi, please check my client configuration file.
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_ICreate_Restful_WCF_Service"/>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://192.168.1.xxx:2777/ProductRESTService.svc" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_ICreate_Restful_WCF_Service" contract="ServiceReference1.IProductRESTService" name="BasicHttpBinding_ICreate_Restful_WCF_Service"/>
</client>
</system.serviceModel>
</configuration>
Why do you use client applicatin for rest service? For rest service, we could call it directly by httpWebRequest. Add Service Reference uses WSDL or ws-metadataExchange, and rest does not have metadata. If you use add service reference, it will not generate any app.config settings, and it will produce this error. If you want to try add service reference to use wcf service, you need to use soap service instead of rest service.

WCF - convert HTTP to Named Pipe

Guys I've been looking at a ton of blogs, SO posts this week and I am still unsure as to how I convert my WCF service from HTTP bindings to using Named Pipes.
I think there are different ways to do it, but I am using the web.configs and using a service reference in my code.
Rather than detail everything here I've tried, can I ask this question?
What are the steps I need to take to go from HTTP Binding to Named Pipes?
Do I need this MEX thing I see mentioned in (some) blogs/SO posts?
I know I need to set IIS to enabled protocols: net.pipe... and that IIS Express doesn't support this (that took an afternoon!)
Some relevant code, what I have right now:
in IEmployeeData:
namespace Mobile.ServiceLayer {
[ServiceContract]
public interface IEmployeeData
{ ... }
Calling the WCF service:
string endpointConfigName = "BasicHttpBinding_IEmployeeData";
EmployeeSvcRef.EmployeeDataClient edc = new EmployeeSvcRef.EmployeeDataClient(endpointConfigName);
EmployeeSvcRef.EmployeeListResponse emp = edc.EmployeeList();
WCF service web.config:
<services>
<service name="Mobile.ServiceLayer.EmployeeData">
<host>
<baseAddresses>
<add baseAddress="http://localhost:62734/EmployeeData" />
</baseAddresses>
</host>
</service>
...
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information, set the value below to false before deployment -->
<serviceMetadata httpGetEnabled="true" />
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
client web.config:
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IEmployeeData" />
...
<client>
<endpoint address="http://localhost:62734/EmployeeData.svc" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IEmployeeData" contract="EmployeeSvcRef.IEmployeeData"
name="BasicHttpBinding_IEmployeeData" />
Like I said I've looked at SO posts and blogs but there's always a piece of the puzzle missing it seems!
EDIT: Client web.config after wizard:
<endpoint address="net.pipe://localhost/EmployeeData.svc/" binding="netNamedPipeBinding"
bindingConfiguration="NewBinding0" contract="IEmployeeData"
name="" kind="" endpointConfiguration="">
<identity>
<certificateReference storeName="My" storeLocation="LocalMachine"
x509FindType="FindBySubjectDistinguishedName" />
</identity>
</endpoint>
Ok this is what I did. You may find it easier to use the built-in tools mentioned in Max's comments. Right click on a web.config and choose Edit WCF Configuration. Get the WCF service done first and, providing the endpoints are set up, running this on the client (right click it's web.config) will present you with a wizard.
SERVER WEB.CONFIG
Service name is the fully-qualified name of the interface e.g. Mobile.ServiceLayer.IEmployeeData
The base address changes to
net.pipe://localhost/EmployeeData.svc
. Notice the port number is removed and the .svc is present(!)
Create an endpoint, the contract being your interface and binding of type netNamedPipeBinding.
Add a second endpoint for MEX which is MetadataEXchange.
Set ServiceMetaData httpGetEnabled to false.
<system.serviceModel>
<services>
<service name="Mobile.ServiceLayer.IEmployeeData">
<host>
<baseAddresses>
<add baseAddress="net.pipe://localhost/EmployeeData.svc" />
</baseAddresses>
</host>
<!-- NetPipe -->
<endpoint
address=""
binding="netNamedPipeBinding"
contract="IEmployeeData" name="MyNetPipe" />
<!-- Mex (Net.Tcp / Net.Pipe ) -->
<endpoint name="EmployeeDataNetPipeMex" address="mex" binding="mexNamedPipeBinding" contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="false" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
CLIENT WEB.CONFIG
Remove the binding entry from 'basicHttpBinding'
Add the section with an entry named NetNamedPipeBinding_IEmployeeData
Inside 'client' add an endpoint with the address
net.pipe://localhost/EmployeeData.svc
the contract being the 'referencename'.'interface'
<bindings>
<basicHttpBinding>
</basicHttpBinding>
<netNamedPipeBinding>
<binding name="NetNamedPipeBinding_IEmployeeData" />
</netNamedPipeBinding>
</bindings>
<client>
<endpoint address="net.pipe://localhost/EmployeeData.svc" binding="netNamedPipeBinding"
bindingConfiguration="NetNamedPipeBinding_IEmployeeData" contract="EmployeeSvcRef.IEmployeeData"
name="NetNamedPipeBinding_IEmployeeData" />
</client>

Could not find a base address that matches scheme net.tcp for the endpoint with binding NetTcpBinding. Registered base address schemes are [http]

I have been using http for all these and when i am working with net.tcp and when adding the reference i am getting an error like
Could not find a base address that matches scheme net.tcp for the endpoint with binding NetTcpBinding. Registered base address schemes are [http].
my web.config
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<services>
<service behaviorConfiguration="servicebehave" name="WcfServ.Service1">
<endpoint address="" binding="netTcpBinding"
bindingConfiguration="" name="nettcp" contract="WcfServ.IService1" />
<endpoint address="mex" binding="mexTcpBinding" bindingConfiguration=""
name="mex" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:51560/Service1.svc" />
<add baseAddress="http://localhost:8080/Service1.svc"/>
</baseAddresses>
</host>
</service>
</services>
<bindings>
<netTcpBinding>
<binding name="netTcpBinding">
<security mode="Transport" />
</binding>
</netTcpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="servicebehave">
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="false"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="false" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
Can anybody tell me where i am doing wrong?
You can't host net.tcp under IIS 6, it supports only HTTP(s). So you are limited with HTTP bindings only (basic, ws or 2007). In order to provide net.tcp and other protocols, WAS is required. You can activate it standalone, but I advice you to install it both with IIS 7 (it is installed as a part of it), because IIS 7 gives a convinient service management platform in addition.
Another solution it so change the hosting environment to make it self-hosted or service-hosted, using ServiceHost class instance, which supports tcp protocol.
If you want to configure your wcf service for net.tcp in IIS 7 please refer to this answer:
WCF Service Base Address Http and netTcp
Regards

WCF - Can't Generate Metadata/Implement MEX for NetMsmq Binding under IIS WAS?

I have been successfull in implementing NetTcp bindings and metadata under WAS but I am having a problem with the netMsmqBinding under WAS/IIS. In a nutshell, I can implement MEX for a Self Hosted NetMsmq binding application but not WAS/IIS. It seems like WAS is rejecting any of the addresses that I use where I can use any address in the self hosted app. In other words I can do this in self hosted:
<system.serviceModel>
<services>
<service name="Microsoft.Samples.MSMQTransactedSample.OrderProcessorService" behaviorConfiguration="OrderProcessorServiceBehavior">
<host>
<baseAddresses>
<add baseAddress="http://localhost:8000/ServiceModelSamples/service" />
</baseAddresses>
</host>
<!-- Define NetMsmqEndpoint -->
<endpoint address="net.msmq://localhost/private/ServiceModelSamplesTransacted" binding="netMsmqBinding" bindingConfiguration="TransactedBinding" contract="Microsoft.Samples.MSMQTransactedSample.IOrderProcessor" />
<!-- the mex endpoint is exposed at http://localhost:8000/ServiceModelSamples/service/mex -->
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="OrderProcessorServiceBehavior">
<serviceMetadata httpGetEnabled="True" />
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<netMsmqBinding>
<binding name="TransactedBinding">
<security mode="None" />
</binding>
</netMsmqBinding>
</bindings>
</system.serviceModel>
Hoever, when I try something similar in WAS, it says no endpoint lisening at address 8000. It won't host the metdata. Can someone help me get metadata or implement MEX for NetMsmq in WAS:
<system.serviceModel>
<bindings>
<netMsmqBinding>
<binding name="MsmqBindingNonTransactionalNoSecurity" exactlyOnce="false">
<security mode="None"/>
</binding>
</netMsmqBinding>
</bindings>
<services>
<service name="Portal.LoadSim.Services.MsmqService" behaviorConfiguration="PortalServiceBehavior">
<host>
<baseAddresses>
<add baseAddress="http://localhost:8000/PortalLoadSimServices10/MsmqService"/>
</baseAddresses>
</host>
<!-- Define NetMsmqEndpoint -->
<endpoint address="net.msmq://localhost/private/PortalLoadSimServices10/MsmqService.svc" binding="netMsmqBinding" bindingConfiguration="MsmqBindingNonTransactionalNoSecurity" contract="Portal.LoadSim.Shared.IMsmqService" />
<!-- the mex endpoint is exposed at http://localhost:8000/PortalLoadSimServices10/MsmqService/mex -->
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="PortalServiceBehavior">
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
You need to add URL access control for your services. This command should work for you:
netsh http add urlacl url=http://localhost:8000/PortalLoadSimServices10/MsmqService user="NT AUTHORITY\NetworkService" listen=yes
To delete the entry
delete urlacl url=http://localhost:8000/PortalLoadSimServices10/MsmqService
HTH
UPDATE: This is not the anwer to your problem. This is what you would do if you wanted to run the services as a self-hosted application and run it under non-standard account. Sorry I was getting it mixed up. I tried using your configuration on a sample application at home and found I had to do two things to get it to work - one, I have to rename the queue to the path and name of the svc file (e.g. PortalLoadSimServices10/MsmqService.svc) and rather than using the base address just browse to the svc file in your IIS host application. You might also need to modify the queue permissions so iis can access it correctly. Let me know if this solves your issue.

WCF over IIS through load balancer reports wrong base address

I'm trying to launch a WCF service over SSL on IIS 6 through a load balancer. My initial problem was an obvious and pretty well discussed one - the address shown on the WSDL page pointed to https://SERVERNAME/WebServices/mydomainws.svc instead of www.mydomain.com. The answer to this problem is to add a host header value in IIS. I did that and it worked... sort of. I now get http://www.mydomain.com/WebServices/mydomainws.svc when viewing the wsdl in a browser. If I click on that link (the non-ssl link) I get a service definition that again references the server name.
The next oft recommended remedy is to use WCF Extras which provides an extension that allows you to specify a base address. But setting that config entry only updated the soap12:address. The EndPointReference address is still using the machine name.
To summarize: WSDL as viewed in web browser at https://www.mydomain.com/WebServices/mydomainws.svc: http://www.mydomain.com/WebServices/mydomainws.scv
Clicking the above link brings me to an actual wsdl file with the following service entry:
https://ServerName/WebServices/mydomainws.svc
My server config file has the following serviceModel entries:
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="TransportSecurity">
<security mode="Transport">
<message clientCredentialType="None"/>
<transport clientCredentialType="None"/>
</security>
</binding>
</wsHttpBinding>
</bindings>
<services>
<service name="mydomain.ws.mydomainws" behaviorConfiguration="mydomainwsBehavior">
<!-- Service Endpoints -->
<endpoint address="" **behaviorConfiguration="CorrectEndPoint"** binding="wsHttpBinding" bindingConfiguration="TransportSecurity" contract="mydomain.ws.Imydomainws"/>
<endpoint address="mex" **behaviorConfiguration="CorrectEndPoint"** binding="mexHttpsBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="mydomainwsBehavior">
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="true" />
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
**<behavior name="CorrectEndPoint">
<wsdlExtensions location="https://www.mydomain.com/WebServices/mydomainws.svc" singleFile="true"/>
</behavior>**
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
<extensions>
<behaviorExtensions>
<add name="wsdlExtensions" type="WCFExtras.Wsdl.WsdlExtensionsConfig, WCFExtras, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
</behaviorExtensions>
</extensions>
</system.serviceModel>
Can anyone point me in the right direction?
Thanks,
George
This should be handeled by new behavior useRequestHeadersForMetadataAddress. Try to add this to your service behavior:
<serviceBehaviors>
<behavior name="LoadBalancedBehavior">
<serviceMetadata httpGetEnabled="true" />
<useRequestHeadersForMetadataAddress>
<defaultPorts>
<add scheme="http" port="80" />
<add scheme="https" port="443" />
</defaultPorts>
</useRequestHeadersForMetadataAddress>
<!-- Other service behaviors as necesary -->
</behavior>
</serviceBehaviors>
This behavior is availabel in WCF 4.0 and should be available as KB for WCF 3.x.