2

I have a WCF service that is hosted on a remote server using IIS. I want to make this service android compatible so I can consume the service on an android client. The problem I'm having is that I'm trying to use ksoap2 to connect to the service but it's giving me a 400 error every time I try to call the method. I've used fiddler2 to try and see what the problem is and in addition to the 400 error, it's also sending back a content-length: 0 (don't know if that is the cause. Interesting fact, when I specify a content-length in the request on fiddler, it doesn't give me a 400 error but rather doesn't return a response at all) I've already tried changing the binding to a webHttpBinding instead of wsHttpBinding and adding a WebGet attribute above the method with a URITemplate, BodyStyle, etc. but no luck. Here's my code:

Service

    public class PublicService : IPublicService
{
    public Wallpaper[] GetWallpapers()
    {
        //return _wallpaperRepository.Items.ToArray();
        return new Wallpaper[]{ new Wallpaper()
                                    {
                                        Id = 10
                                    }};
    }
}

Interface

[ServiceContract(Namespace = "http://XXXXXXXX.com/ServiceA")]
public interface IPublicService
{
    [OperationContract]
    Wallpaper[] GetWallpapers();
}

Web.Config

    <bindings>
            <wsHttpBinding>
<binding name="android" 
maxReceivedMessageSize="4097152" 
maxBufferPoolSize="4097152">
                    <readerQuotas 
maxStringContentLength="4097152" 
maxArrayLength="4097152" 
maxBytesPerRead="4097152" 
maxNameTableCharCount="4097152" 
maxDepth="4097152"/>
                </binding>
            </wsHttpBinding>
        </bindings>
        <services>
            <service name="Service.Concrete.PublicService" behaviorConfiguration="ServiceBehavior">
                <endpoint address="PublicService" binding="wsHttpBinding" name="PublicService" contract="Service.Abstract.IPublicService" bindingConfiguration="android"/>
                <endpoint binding="mexHttpBinding" bindingConfiguration="" name="mex" contract="IMetadataExchange"/>
            </service>

I've been beating my head against the wall for the past several hours and going through countless googling, I'm at a complete lost on what to do next. If anyone has any idea what I'm doing wrong, any advice would be greatly appreciated.

Sathariel
  • 340
  • 1
  • 5
  • 13
  • Better avoid using KSOAP2 for maximum comparability with android. Google android does not support SOAP officially. KSOAP2 may work but it is not the right solution for commercial android apps. – Sree Rama Aug 27 '12 at 09:43

1 Answers1

3

Use basicHttpBinding instead. You can try it with default configuration:

<endpoint address="PublicService" binding="basicHttpBinding" name="PublicService" contract="Service.Abstract.IPublicService" />

In your android service make sure that you are specifying correct SOAP action, correct namespaces and that SoapSerializationEnvelope is constructed for SOAP 1.1 and has dotNet flag set to true.

WsHttpBinding defaults to many advanced WS-* protocols. For example WS-Addressing which is set of well known SOAP headers. If you don't include them in your message WCF service will reject the request.

WebHttpBinding will also not solve the problem because that binding is used for REST services but you want to consume the service with ksoap = you need SOAP service.

Edit:

Another question on Stack overflow has nice example in the answer.

Community
  • 1
  • 1
Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
  • No luck, I changed my binding to basicHttpBinding and used android code from the link but I'm still getting a XmlPullParserException when I invoke the call method on my HttpTransportSE object. The message is "unexpected type (position:END_DOCUMENT null@1:0 in java.io.InputStreamReader@44e98d20) " – Sathariel May 29 '11 at 18:11
  • Turn on WCF tracing on your service and check trace log with SvcTraceViewer: http://msdn.microsoft.com/en-us/library/ms732023.aspx It should show you why WCF service doesn't like request from your Android client. You can also try creating .NET client for your service and compare requests from .NET client and Android client (captured by Fiddler). – Ladislav Mrnka May 29 '11 at 23:09
  • Thanks for your help. I found this really useful program called soapui that helped me see what my soap requests should look like. – Sathariel Jun 01 '11 at 06:30
  • http://stackoverflow.com/questions/11374416/mobile-app-development-plan-with-rest-api-is-good-idea/11374681#11374681 Try to use RESTful client in android, Google Android does not support SOAP officially. RestFul client is always recommeneded for commercial android apps. – Sree Rama Aug 27 '12 at 09:45