1

I'm building an Android app and I need to upload a photo taken with the camera. I need to upload it to a restful WCF service. I have looked at many tutorials but I just can't seem to get it working. I think my problem lies with the WCF service. I do not get any exceptions but I get a response 400 BAD REQUEST. Since the WCF service is currently running on my localhost, I am using 10.0.2.2 to access it from the android emulator. I am able to call other service methods on my local service but this one fails.

Java

HttpPost httppost = new HttpPost("http://10.0.2.2:53943/ImageService/UploadInspectionPhoto");
File photo = new File(Environment.getExternalStorageDirectory(), "01.jpg");
MultipartEntity t = new MultipartEntity();
t.addPart("t", new FileBody(photo));
//t.addPart(new FormBodyPart("t", new FileBody(photo))); I tired this too
httppost.setEntity(t);
HttpResponse response = httpclient.execute(httppost);

WCF

[ServiceContract]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class ImageService
{
        [WebInvoke(Method = "POST")]
        public void UploadInspectionPhoto(Stream t)
        {
            // I put a breakpoint here but it never gets here
            // Do something with the stream
        }
}

Config file

<?xml version="1.0"?>
<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
      <customErrors mode="Off"></customErrors>
  </system.web>

  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
      <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
    </modules>
  </system.webServer>

  <system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
      <bindings>
          <!--<basicHttpBinding>
              <binding name="BasicHttpStreaming" transferMode="Streamed">
              </binding>
          </basicHttpBinding>-->
          <webHttpBinding>
              <binding name="WebHttpDtreaming" transferMode="Streamed" >
              </binding>
          </webHttpBinding>
      </bindings>
    <standardEndpoints>
      <webHttpEndpoint>
        <!-- 
            Configure the WCF REST service base address via the global.asax.cs file and the default endpoint 
            via the attributes on the <standardEndpoint> element below
        -->
        <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true" />
      </webHttpEndpoint>
    </standardEndpoints>
      <services>
          <service behaviorConfiguration="PublishMetadataBehavior" name="SystemDesign.Collaborate.Services.ImageService">
              <endpoint address="soap" binding="basicHttpBinding" name="soap" contract="SystemDesign.Collaborate.Services.ImageService"/>
              <endpoint address="mex" binding="mexHttpBinding" name="mex" contract="IMetadataExchange" />
          </service>
      </services>
      <behaviors>
          <serviceBehaviors>
              <behavior name="PublishMetadataBehavior">
                  <serviceMetadata httpGetEnabled="true" policyVersion="Policy15"/>
              </behavior>
          </serviceBehaviors>
      </behaviors>
  </system.serviceModel>

</configuration>

What am I doing wrong?

Edgar Gonzalez
  • 1,862
  • 1
  • 15
  • 19
jcruz
  • 718
  • 6
  • 13
  • are you able to call the WCF method from a different client (e.g.: a test client that you created in the same solution)? If not, then the issue is probably in your service code or even more likely in your configuration. Then again, it could just be that the URI you're trying to use is incorrect... – Brian Driscoll Feb 18 '11 at 17:55
  • Yes. I called another method in the same service using Fiddler and using the android browser in the emulator. Both worked. It's just this particular call that wont work from my android app. – jcruz Feb 18 '11 at 18:54
  • I think you should at least remover the komma behind getExternalStorageDirectory(). – Rutger Apr 17 '11 at 15:17

2 Answers2

0

Is using the WebHttpBinding raw programming model on the service side an option for you? See these two blog posts for detailed step-by-step instructions on how to pull it off.

krisragh MSFT
  • 1,908
  • 1
  • 13
  • 22
0

For anyone that comes across this, as I just did, it turned out that I needed to update the maxReceivedMessageSize for my WCF service. This question helped: WCF REST service 400 Bad Request

Community
  • 1
  • 1
Sean
  • 2,453
  • 6
  • 41
  • 53