4

I have an webservice

WebServiceHost webServiceHost= new WebServiceHost(typeof(WebMethods), new Uri(url));
webServiceHost.Open();

public class Fish { public string name = "I am a fish"; }
public class Dog { public int legs = 4; }
public class Cat { public DateTime dt = DateTime.Now;}

One of my webMethods should return a dynamic object

WebMethod:

Solution 1

[OperationBehavior]
[WebGet(ResponseFormat = WebMessageFormat.Json, UriTemplate = "/isTest?class={cl}")]
object isTest(string cl)
{
    object obj;

    switch (cl)
    {
        case "fish":
            obj= new Fish();
            break;
        case "dog":
            obj= new Dog();
            break;
        default:
            obj= new Cat();
            break;

    }
    return obj;

}

Solution 2

[OperationBehavior]
[WebGet(ResponseFormat = WebMessageFormat.Json, UriTemplate = "/isTest?class={cl}")]
dynamic isTest(string cl)
{
    dynamic obj;

    switch (cl)
    {
        case "fish":
            obj= new Fish();
            break;
        case "dog":
            obj= new Dog();
            break;
        default:
            obj= new Cat();
            break;

    }
    return obj;
}

Both are not working. The response is ERR_CONNECTION_RESET

Any idea how to realise it? Thanks for help.

8
  • Set a breakpoint on 'return obj'. Is the appropriate object returned? Commented Mar 22, 2016 at 7:07
  • Yes the appropriated object is returned! Commented Mar 22, 2016 at 7:08
  • 1
    Type in the full url (localhost:8323/somewebservice/isTest?class=fish) in a web browser and give the full error msg. if you are using IE try pressing F11 (I think) and watch your network responses Commented Mar 22, 2016 at 7:13
  • In the Networktab i just get the information that the result is aborted... My other webMethod /running returns true.. So i have no connection problems. Commented Mar 22, 2016 at 7:24
  • Maybe you should specify [DataContract] attribute to your returning class? stackoverflow.com/questions/29170160/… Commented Mar 22, 2016 at 7:32

2 Answers 2

0

You are not returning a JSON string. Add the following to your uses:

using System.Web.Script.Serialization;

and the following in your body

return new JavaScriptSerializer().Serialize(obj);

change your return type to string instead of object

Sign up to request clarification or add additional context in comments.

2 Comments

OP has already used ResponseFormat = WebMessageFormat.Json. It is expected that the WCF do the translation.
I need to return an object. Returning a string is not a possible solution.
0

You can cast the "HttpResponseMessage" response or you can just send the model object in create response method.

[WebGet(UriTemplate = "{id}")]
public HttpResponseMessage isTest(int id)
{
   Model model = Model.table.Where(p => p.Id == id).FirstOrDefault();
   if (model != null)
   {
      //return Request.CreateResponse<Model>(HttpStatusCode.OK, model);
      //or
      return Request.CreateResponse(HttpStatusCode.OK, model);
   }
   else
   {
      return Request.CreateErrorResponse(HttpStatusCode.NotFound, "Model Not Found");
   }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.