Question

Here, when i am trying to send byte array it is giving me bad request error. I have checked with maximum values for all message limits. All are set to maximum value. However i am able to send string values to service method by keeping byte array parameter as null. I don't know why this happening here. I have also checked the service behavior configuration name and binding configuration names(i.e., included namespace) which seems to be fine.Any help is greatly appreciated.

This is my client side code:

....
ServiceClient ac = new ServiceClient();
string ServiceUrl = "http://localhost:20314/Service.svc";
EndpointAddress address = new EndpointAddress(ServiceUrl);
BasicHttpBinding httpbinding = new BasicHttpBinding();   
using (ChannelFactory<IServiceChannel> factory = new   ChannelFactory<IServiceChannel>(httpbinding))
{
    factory.Endpoint.Address = address;
    using (IServiceChannel oService = factory.CreateChannel())
    {

       using (OperationContextScope scope = new OperationContextScope(oService))
       {

         Guid myToken = new Guid("guid here");                      

         MessageHeader<Guid> mhg = new MessageHeader<Guid>(myToken);                       
         MessageHeader untyped = mhg.GetUntypedHeader("identity", "ns");
         OperationContext.Current.OutgoingMessageHeaders.Add(untyped);
         // here i am getting bad request error                                                               
         var fdt = (oService).GetData(bytearray, value);

         }
    }
}
.......

client web config:

<system.web>
<compilation debug="true" targetFramework="4.0"/>     
<httpRuntime maxRequestLength="500000000" executionTimeout="360"/> 
</system.web>  
<system.serviceModel>
<bindings>
  <basicHttpBinding>        
    <binding name="BasicHttpBinding_IService" closeTimeout="01:01:00"
      openTimeout="01:01:00" receiveTimeout="01:10:00" sendTimeout="01:01:00"
      allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
      maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647"
      messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
      useDefaultWebProxy="true">
      <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647"
        maxBytesPerRead="4096" maxNameTableCharCount="2147483647" />
      <security mode="None">
        <transport clientCredentialType="None" proxyCredentialType="None"
          realm="" />
        <message clientCredentialType="UserName" algorithmSuite="Default" />
      </security>
    </binding>
  </basicHttpBinding>
</bindings>

<client>      
  <endpoint address="http://localhost:20314/Service.svc" binding="basicHttpBinding"
    bindingConfiguration="BasicHttpBinding_IService" contract="LocalService.IService"
    name="BasicHttpBinding_IService"/> 
</client>

<services>      
  <service name="LocalService.Service" behaviorConfiguration="ServiceBehavior">
    <endpoint address="http://localhost:20314/Service.svc" binding="wsHttpBinding" contract="LocalService.IService" />
  </service>
</services>

<behaviors>
  <serviceBehaviors>        
    <behavior name="ServiceBehavior">
      <serviceMetadata httpGetEnabled="true"/>
      <serviceDebug httpHelpPageEnabled="true" includeExceptionDetailInFaults="true"/>
    </behavior>
  </serviceBehaviors>
</behaviors>

Service Web Config

<system.web>
<compilation debug="true" targetFramework="4.0" />
<httpRuntime maxRequestLength="500000000" executionTimeout="360"/>
</system.web>
<system.webServer>
<httpErrors errorMode="Detailed" />
<asp scriptErrorSentToBrowser="true" />
</system.webServer>  
<system.serviceModel>    
<behaviors>
  <serviceBehaviors>
    <behavior>          
      <serviceAuthorization serviceAuthorizationManagerType="ServiceApp.APIKeyAuthorization, ServiceApp" />
      <serviceMetadata httpGetEnabled="true" />
      <dataContractSerializer maxItemsInObjectGraph="2147483647" />
      <serviceDebug httpHelpPageEnabled="true" includeExceptionDetailInFaults="true"/>
    </behavior>
  </serviceBehaviors>
</behaviors>
<standardEndpoints>
  <webHttpEndpoint>
    <standardEndpoint
      automaticFormatSelectionEnabled="true"
      helpEnabled="true" />
  </webHttpEndpoint>
</standardEndpoints>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="false"/>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
Was it helpful?

Solution

There's a couple of things I see in your config files.

First, in your client config you have a <services> section - unless your client is also hosting a service, you don't need that there. Additionally, your <services> section is using wsHttpBinding, but your client is specifying basicHttpBinding - the client and service bindings need to match.

Second, you've set higher values for the binding on the client side, but the client side binding configuration has no effect on the server side. You don't have an explicit endpoint defined in your service config, so you're getting the default endpoint with default binding (basicHttpBinding), which has the normal default values.

You can either add a default configuration for basicHttpBinding in your service config (by omitting the name attribute) like this:

<bindings>
  <basicHttpBinding>        
    <binding closeTimeout="01:01:00"
             openTimeout="01:01:00" receiveTimeout="01:10:00" sendTimeout="01:01:00"
             allowCookies="false" bypassProxyOnLocal="false" 
             hostNameComparisonMode="StrongWildcard"
             maxBufferSize="2147483647" maxBufferPoolSize="2147483647" 
             maxReceivedMessageSize="2147483647"
             messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
             useDefaultWebProxy="true">
      <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
                    maxArrayLength="2147483647"
                    maxBytesPerRead="4096" maxNameTableCharCount="2147483647" />
      <security mode="None">
        <transport clientCredentialType="None" proxyCredentialType="None"
                   realm="" />
        <message clientCredentialType="UserName" algorithmSuite="Default" />
      </security>
    </binding>
  </basicHttpBinding>
</bindings>

This is the same definition from your client.config, minus the name attribute. (Also, why do you have transport and message security defined but security mode set to "none"?).

Another option is to explicitly define an endpoint in your service config and specify a binding configuration to use. Similar to what you did in the client config file. Let's assume you have a basicHttpBinding named "BasicHttpBinding_IService". Your endpoint would look something like this:

<services>      
  <service name="LocalService.Service" behaviorConfiguration="ServiceBehavior">
    <endpoint address="http://localhost:20314/Service.svc" 
              binding="basicHttpBinding" 
              bindingConfiguration="BasicHttpBinding_IService"
              contract="LocalService.IService" />
  </service>
</services>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top