Passing bmp file/Image as parameter in wcf service - asp.net

I am trying to pass a BMP file as a parameter in a WCF Service. I'mm getting an error when the size of the BMP file is 350kb. When I pass a small file of 10-20 kb it runs perfectly.
The remote server returned an unexpected response: (413) Request Entity Too Large.
[OperationContract]
byte[] ConvertData(Bitmap bmp);
I have tried to solve the issue with changing IIS setting following this link: http://blogs.msdn.com/b/jiruss/archive/2007/04/13/http-413-request-entity-too-large-can-t-upload-large-files-using-iis6.aspx but no luck
My Server web.config is
<system.web>
<compilation debug="true" targetFramework="4.0" />
<httpRuntime executionTimeout="240000"/>
</system.web>
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="BasicHttpBinding_IService1" closeTimeout="00:10:00" openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00" hostNameComparisonMode="StrongWildcard" maxReceivedMessageSize="2147483647">
<readerQuotas maxDepth="32" maxStringContentLength="2147483647" maxArrayLength="2147483647"/>
</binding>
</wsHttpBinding>
</bindings>
<services>
<service name="ReDrawImgService.Service1">
<endpoint address="" binding="wsHttpBinding" bindingConfiguration="" contract="ReDrawImgService.IService1"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- 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>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
My Client Web.Config is
<system.web>
<compilation debug="true" targetFramework="4.0" />
<httpRuntime executionTimeout="240000"/>
</system.web>
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="BasicHttpBinding_IService1" closeTimeout="00:10:00" openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00" hostNameComparisonMode="StrongWildcard" maxReceivedMessageSize="2147483647">
<readerQuotas maxDepth="32" maxStringContentLength="2147483647" maxArrayLength="2147483647"/>
</binding>
</wsHttpBinding>
</bindings>
<services>
<service name="ReDrawImgService.Service1">
<endpoint address="" binding="wsHttpBinding" bindingConfiguration="" contract="ReDrawImgService.IService1"/>
</service>
</services>
<client>
<endpoint address="http://localhost:8012/Service1.svc" binding="wsHttpBinding"
bindingConfiguration="BasicHttpBinding_IService1" contract="ServiceClient.IService1"
name="BasicHttpBinding_IService1" />
</client>
</system.serviceModel>

On your readerQuotas, try to set maxStringContentLength to 2147483647 as well, and make sure this config is set on both ends.
So both the server and the client need to have these values. It's no help if the client is allowed to send a large file, if the server will reject it anyway.
What is the value of maxArrayLength on the wcf server application

Related

Namespaces for WCF Web.config, client and server side, maxReceivedMessageSize

I'm not a VB developer and I'm having some issues with Namespace for a Web service. I own both the client and server side of this so I have no issues changing settings.
I'm trying to connect to a webservice and while doing so I get a MaxReceivedMessageSize exception.
They way I go about this is that I have a solution with a "web project" and more "class projects", in one of my "class projects" I have a Service reference and a app.config file where I do my settings.
I have read that I need to have app.config settings in my application and add settings on server web.config for the web service. I'm confused about the namespaces I need to set in the web.config for the web.service on server side. Can anybody help me out a bit? I use this settings
(server)
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="Erp_QueryServiceSoap" maxReceivedMessageSize="2147483647">
<readerQuotas maxDepth="32" maxStringContentLength="2147483647" maxArrayLength="16348" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
</binding>
</wsHttpBinding>
</bindings>
<services>
<service name="WebService.Erp_QueryService.Erp_QueryServiceSoap">
<endpoint address="Erp_QueryService.Erp_QueryServiceSoap"
binding="wsHttpBinding" bindingConfiguration="Erp_QueryServiceSoap"
contract="Erp_QueryService.Erp_QueryServiceSoap" name="Erp_QueryServiceSoap" />
</service>
</services>
</system.serviceModel>
(client)
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="Erp_QueryServiceSoap" maxReceivedMessageSize="2147483647">
<readerQuotas maxDepth="32" maxStringContentLength="2147483647" maxArrayLength="16348" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
</binding>
</wsHttpBinding>
</bindings>
<client>
<endpoint address="CONNECTION TO SERVICE"
binding="wsHttpBinding" bindingConfiguration="Erp_QueryServiceSoap"
contract="ServiceQuery.Erp_QueryServiceSoap" name="Erp_QueryServiceSoap" />
</client>
</system.serviceModel>
i think may be problem is not with namespaces.
try to set maxItemsInObjectGraph to the following.
in behaviour section
<behavior name="YourServiceNameBehaviour">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
**<dataContractSerializer maxItemsInObjectGraph="2147483647" />**
</behavior>
i think you already have added those codes except
**<dataContractSerializer maxItemsInObjectGraph="2147483647" />**
try adding this code line.

WCF - Remote server returned an error: 404

I am new to WCF and i am trying to host my first WCF application in a different machine. It works in my local machine from IIS. I hosted in the remote machine and i am able to view the WSDL. I have enabled anonymous authentication as well.
Problem is when i try to test my .svc with SOAP UI, it errors out saying
Error loading [http://domain.com/folder/wcfservice.svc?xsd=xsd0]: java.io.IOException: Attempted read from closed stream
So, i tried using svcutil command and it errors out with
Attempting to download metadata from 'http: //domain.com/folder/wcfService.svc?wsdl' using WS-Metadata Exchange or
DISCO. Error: Cannot obtain Metadata from http:
//domain.com/folder/wcfService.svc?wsdl WS-Metadata Exchange Error
URI: http: //domain.com/folder/wcfService.svc?wsdl Metadata contains a
reference that cannot be resolved: 'http:
//domain.com/folder/wcfService.svc?wsdl'.
There was no endpoint listening at http: //domain.com/folder/wcfService.svc?wsdl that could accept the
message. This is often caused by an incorrect address or SOAP action.
See InnerException, if present, for more details.
The remote server returned an error: (404) Not Found.HTTP GET Error
URI: http ://domain.com/folder/wcfService.svc?wsdl The document was understood, but it could not be processed. The WSDL document
contains links that could not be resolved. There was an error
downloading 'http ://domain.com/folder/wcfService.svc?xsd=xsd0'. The
underlying connection was closed: An unexpected error occurred on a
receive. Unable to read data from the transport connection: An
existing connection was forcibly closed by the remote host.
Here is my webconfig for your reference.
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation targetFramework="4.0" />
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
<behavior name="defaultBehaviors">
<serviceMetadata httpGetEnabled="true"/>
<dataContractSerializer maxItemsInObjectGraph="2147483646"/>
<serviceDebug httpHelpPageEnabled="true" includeExceptionDetailInFaults="true"/>
<serviceTimeouts transactionTimeout="10.00:00:00"/>
</behavior>
<behavior name="metadataSupport">
<serviceDebug includeExceptionDetailInFaults="False" />
<!--Enable WSDL Data Binding-->
<serviceMetadata httpGetEnabled="true" httpGetUrl=""/>
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpBinding" scheme="http" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="false" multipleSiteBindingsEnabled="true" />
<bindings>
<basicHttpBinding>
<binding name="wcfservice.SSO"
maxBufferSize="2147483647"
maxBufferPoolSize="2147483647"
maxReceivedMessageSize="2147483647">
<readerQuotas maxDepth="32"
maxStringContentLength="2147483647"
maxArrayLength="2147483647"
maxBytesPerRead="4096"
maxNameTableCharCount="16384" />
<security mode="None">
<transport clientCredentialType="None" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<services>
<service name="wcfservice.wcfservice_Service" behaviorConfiguration="defaultBehaviors">
<host>
<baseAddresses>
<add baseAddress="http://domain.com/folder/wcfService.svc" />
</baseAddresses>
</host>
<endpoint address="xml"
binding="basicHttpBinding"
bindingConfiguration="wcfservice.SSO"
contract="wcfservice.Iwcfservice" />
<endpoint address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange">
</endpoint>
</service>
</services>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<directoryBrowse enabled="true"/>
</system.webServer>
</configuration>
I think its worthwhile to mention that this WCF application was created in VS2012 and i just changed the targetframework to 4.0 in web.config after deployment.
Can somebody please help what is causing the error? Am i missing something in web.config?

ASP.NET hosting WCF Services, needs to increase MaxStringContentLength, but how?

My ASP.NET server is providing a set of WCF services which are consumed by my WPF client. Everything was working just fine until the length of a string field exceeded 8K. This now generates the following exception on the ASP.NET server...
There was an error deserializing the object of type Project.ModelType.
The maximum string content length quota (8192) has been exceeded while
reading XML data. This quota may be increased by changing the
MaxStringContentLength property on the XmlDictionaryReaderQuotas
object used when creating the XML reader.
I have increased the value of the MaxStringContentLength to be 64K on the WPF app.config but this has not solved the issue. So I guess I need to increase this value on the ASP.NET side as well. But I do not have any values in the web.config to change! Here is my web.config to show this...
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
<authentication mode="Forms">
<forms name=".ASPXFTOAUTH"
timeout="10"
slidingExpiration="true"
cookieless="UseCookies"/>
</authentication>
<membership>
<providers>
<clear/>
</providers>
</membership>
<customErrors defaultRedirect="~/Error.htm" mode="RemoteOnly">
<error statusCode="404" redirect="~/404.aspx" />
</customErrors>
</system.web>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
multipleSiteBindingsEnabled="true" />
<behaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
So how do I update the server to indicate the higher MaxStringContentLength value? The app.config for my service looks like this...
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IAccess" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
allowCookies="false" bypassProxyOnLocal="false"
hostNameComparisonMode="StrongWildcard"
maxBufferSize="131072" maxBufferPoolSize="524288" maxReceivedMessageSize="131072"
messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="65536" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None" realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IAccess"
contract="AccessService.IAccess"
name="BasicHttpBinding_IAccess" />
</client>
</system.serviceModel>
Any ideas?
UPDATE:
My services are defined by having a class 'Access.svc'
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Access : IAccess
{
// ...IAccess method implementations
}
...which has the following markup...
<%# ServiceHost Language="C#" Debug="true"
Service="Ecotech.AMS.WebServer.Access"
CodeBehind="Access.svc.cs" %>
...there is nothing specific about the service in the web.config, as noted in comments.
Try clicking "Add Service Reference" via the right-click menu of your Project. Doing it this way will add the necessary configuration information into your web.config file.
Once you do this, you can set the maxReceivedMessageSize property on your binding. Doing it this way will most accurately reflect what you are doing on the WCF service side of things.
The place where you need to work on is the web config, you need to add service behaviours where you can set the size of data. For example like this,
<behaviors>
<serviceBehaviors>
<behavior name="SilverlightWCFLargeDataApplication">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
<dataContractSerializer maxItemsInObjectGraph="2147483647"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="SilverlightWCFLargeDataApplication">
<dataContractSerializer maxItemsInObjectGraph="2147483647"/>
</behavior>
</endpointBehaviors>
</behaviors>
if that does not work, post your web config here.Hope it helps.

Transfer large of data using wcf service

When I try to transfer a large data in WCF, I get this error:
The formatter threw an exception while trying to deserialize the
message: There was an error while trying to deserialize parameter
http://tempuri.org/:requete. The InnerException message was 'There was
an error deserializing the object of type System.String. The maximum
string content length quota (8192) has been exceeded while reading XML
data. This quota may be increased by changing the
MaxStringContentLength property on the XmlDictionaryReaderQuotas
object used when creating the XML reader. Line 1, position 8843.'.
Please see InnerException for more details.
This is my app.config file :
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.web>
<compilation debug="true" />
</system.web>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="NewBinding0" maxBufferSize="52428800" maxBufferPoolSize="52428800">
<readerQuotas maxStringContentLength="52428800" />
</binding>
</basicHttpBinding>
</bindings>
<services>
<service name="TSO_WCF.Service1">
<host>
<baseAddresses>
<add baseAddress="http://localhost:8732/Design_Time_Addresses/TSO_WCF/Service1/" />
</baseAddresses>
</host>
<!-- Service Endpoints -->
<!-- Unless fully qualified, address is relative to base address supplied above -->
<endpoint address="" binding="wsHttpBinding" contract="TSO_WCF.IService1" >
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="True"/>
<serviceDebug includeExceptionDetailInFaults="False" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
<appSettings>
<add key="Mapping" value="D:\workspace\oddo.app.ldw.tsomanagertool\ProjetTSO\MappingsXML"/>
</appSettings>
</configuration>
You should increse the MaxStringContentLength on the server and client binding. At this moment you are using the default value of 8192.
Perhaps you could change your binding /use streaming. See this page: http://msdn.microsoft.com/en-us/library/ms733742.aspx

Requests to wcf service are unauthorized

I want to use a wcf service in my asp.net application that uses integrated windows-authentication however all the requests to my Model.svc are 401 Unauthorized.
If I change clientCredentialType="Windows" to clientCredentialType="Ntlm" every request is Unauthorized 2 times and the 3rd one is successful.
Here is my web.config:
<configuration>
<appSettings>
</appSettings>
<connectionStrings/>
<system.web>
<compilation debug="true" targetFramework="4.0" />
<authentication mode="Windows" />
</system.web>
<!--WCF Configuration-->
<system.serviceModel>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true"/>
<bindings>
<webHttpBinding>
<binding name="WebHttpBinding_IModel">
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Windows" />
</security>
</binding>
</webHttpBinding>
</bindings>
<client />
<services>
<service behaviorConfiguration="ServiceBehavior" name="Model">
<endpoint address="" behaviorConfiguration="JsonBehavior" binding="webHttpBinding" bindingConfiguration="WebHttpBinding_IModel" contract="IModel">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="JsonBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
2 times unauthorized and third working is a part of the protocol. That is the way NTLM authentication works. Is this posing a problem? WCF is made to handle such scenarios, and so your WCF authentication call will work fine.

Resources