【发布时间】:2016-03-22 06:45:44
【问题描述】:
我有一个网络服务
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;}
我的一个 webMethods 应该返回一个动态对象
网络方法:
解决方案 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;
}
解决方案 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;
}
两者都不工作。响应是 ERR_CONNECTION_RESET
知道如何实现吗? 感谢帮助。
【问题讨论】:
-
在'return obj'上设置断点。是否返回了适当的对象?
-
是的,相应的对象被返回了!
-
在网络浏览器中输入完整的 url (localhost:8323/somewebservice/isTest?class=fish) 并给出完整的错误信息。如果您使用的是 IE,请尝试按 F11(我认为)并观察您的网络响应
-
在 Networktab 中,我只是得到结果被中止的信息......我的其他 webMethod /running 返回 true..所以我没有连接问题。
-
也许你应该为你的返回班指定
[DataContract]属性? stackoverflow.com/questions/29170160/…
标签: c# json webmethod webservicehost