4

I created a simple function

       [OperationContract]
       [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json)]
       string Start();

Definition,

       public String Start()
       {
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            return serializer.Serialize("Check");
       }

From browser using Javascript/Jquery, http://localhost/service1.svc tells me I have created a service and all other info.. Looks fine.
I'm trying to call this using http://localhost/service1.svc/Start

I get a 400 bad request for this call. I hope I'm not doing something totally wrong here. I should be able to access WCF service from browser right? I tried looking a lot before I thought of posting. But I'm unable to get this basic thing working is frustrating me.

EDIT & UPDATE Now I'm at this stage. The service page is telling me that the metadata service is disabled and is asking me to insert the following text

   <serviceMetadata httpGetEnabled="true" />

I inserted the text - but still it shows the same text!! This is getting too confusing now..

Tom
  • 208
  • 1
  • 4
  • 11
  • Do you have any payload with that status code? If no, try to turn on the option that sends error messages back to client. It is under ServiceBehaviors/Behavior element. – Sergey Sirotkin Sep 20 '11 at 22:31
  • It is set to true. But I do not get any payload. Just the status code. – Tom Sep 20 '11 at 22:35
  • @a.dimo is right, your specified that your method should only be called using POST method. When you request it with browser, it sends GET request. – Sergey Sirotkin Sep 20 '11 at 22:39
  • I did not directly call from browser. I used a Jquery call – Tom Sep 20 '11 at 23:40
  • Could you post the server error message here? – Sergey Sirotkin Sep 21 '11 at 16:53
  • Error: Cannot import wsdl:portTypeDetail: An exception was thrown while running a WSDL import extension: System.ServiceModel.Description.DataContractSerializerMessageContractImporterError: Schema with target namespace 'http://tempuri.org/' could not be found – Tom Sep 21 '11 at 19:55
  • This error is visible onlywhen you use WCFTestClient.exe, it works from the browser now.... – Tom Sep 21 '11 at 19:56

2 Answers2

1

Try to change POST with GET and restart the request

ADIMO
  • 1,107
  • 12
  • 24
  • Nopes. That did not help. I already tried every combination - start, start(int id), GET and POST – Tom Sep 20 '11 at 22:37
1

Works for me. I created WCF Rest Service.

I use URL which looks like http://localhost:8080/Service1/Start

Here is the code:

using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Web.Script.Serialization;

namespace WcfRestService1
{
    // Start the service and browse to http://<machine_name>:<port>/Service1/help to view the service's generated help page
    // NOTE: By default, a new instance of the service is created for each call; change the InstanceContextMode to Single if you want
    // a single instance of the service to process all calls.   
    [ServiceContract]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    // NOTE: If the service is renamed, remember to update the global.asax.cs file
    public class Service1
    {
        [OperationContract]
        [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json)]
        public string Start()
        {
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            return serializer.Serialize("Check");
        }
    }
}
Sergey Sirotkin
  • 1,668
  • 11
  • 7
  • Hmm.. I am unable to determine what is going wrong in my code. – Tom Sep 20 '11 at 22:58
  • What kind of project did you create? Just WCF Service Application (under WCF tab in VS) or WCF REST Service Application (under Web tab). – Sergey Sirotkin Sep 20 '11 at 23:00
  • From WCF -> WCF Service application> I did some more investigation and here is what I found.. Cannot import wsdl:portType Details – Tom Sep 20 '11 at 23:37
  • similar to text in gray area in this question http://stackoverflow.com/questions/4727091/metadata-error-when-running-wcf-service-as-particular-user – Tom Sep 20 '11 at 23:39
  • I'm not sure what I should do though! – Tom Sep 20 '11 at 23:42
  • Hi Sergey - I have updated the question - please let me know if you have any insight – Tom Sep 21 '11 at 14:58