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.

patrick d.
  • 41
  • 1
  • 4
  • Set a breakpoint on 'return obj'. Is the appropriate object returned? – Eminem Mar 22 '16 at 07:07
  • Yes the appropriated object is returned! – patrick d. Mar 22 '16 at 07:08
  • 1
    Type in the full url (http://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 – Eminem Mar 22 '16 at 07: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. – patrick d. Mar 22 '16 at 07:24
  • Maybe you should specify `[DataContract]` attribute to your returning class? https://stackoverflow.com/questions/29170160/wcf-entity-framework-err-connection-reset – David S. Mar 22 '16 at 07:32
  • tested. But same response – patrick d. Mar 22 '16 at 07:37
  • Exactly. Run the web service in Chrome, hit F12 to view Developer Options, and open the Network tab. We'd need to see exactly what your service is returning, eg : http://i.stack.imgur.com/OhVNw.png Btw, I was having this issue when a database exception occurred which contained a line-break in it. http://stackoverflow.com/questions/36006976/wcf-web-services-returning-an-error-exception-string – Mike Gledhill Mar 22 '16 at 07:41
  • [Exception picture](http://www.pic-upload.de/view-30107671/fail.png.html) – patrick d. Mar 22 '16 at 07:47

2 Answers2

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

Eminem
  • 7,206
  • 15
  • 53
  • 95
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");
   }
}