如何使自托管wcf接受REST/JSON?
我已经在几篇文章中读到,可以创建一个接受JSON作为输入的自托管wcf REST服务。但我无法让它工作,我的头一次又一次地撞在同一块石头上,似乎: 这是我的基本代码,我无法工作。 Im在VS 2017中使用.net 4.6.1如何使自托管wcf接受REST/JSON?,json,rest,wcf,rest-client,Json,Rest,Wcf,Rest Client,我已经在几篇文章中读到,可以创建一个接受JSON作为输入的自托管wcf REST服务。但我无法让它工作,我的头一次又一次地撞在同一块石头上,似乎: 这是我的基本代码,我无法工作。 Im在VS 2017中使用.net 4.6.1 [ServiceContract] public interface IService1 { //[WebInvoke(Method = "POST", UriTemplate = "Save")] [WebInvoke(Method = "POST", BodyStyl
[ServiceContract]
public interface IService1
{
//[WebInvoke(Method = "POST", UriTemplate = "Save")]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
[OperationContract]
bool Save(BatchOfRows request);
}
public bool Save(BatchOfRows request)
{
return true;
}
作为回应,我把这个拿回来了
"StatusCode: UnsupportedMediaType, Content-Type: , Content-Length: 0)"
Content: ""
ContentEncoding: ""
ContentLength: 0
ContentType: ""
Cookies: Count = 0
ErrorException: null
ErrorMessage: null
Headers: Count = 3
IsSuccessful: false
ProtocolVersion: {1.1}
RawBytes: {byte[0]}
Request: {RestSharp.RestRequest}
ResponseStatus: Completed
ResponseUri: {http://127.0.0.1:8000/Save}
Server: "Microsoft-HTTPAPI/2.0"
StatusCode: UnsupportedMediaType
StatusDescription: "Cannot process the message because the content type 'application/x-www-form-urlencoded' was not the expected type 'application/soap+xml; charset=utf-8'."
我不能让它工作!我尝试了不同的客户机实现,得到了相同的结果。
在每一个答案中,我都会检查它是否只是使用
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
我应该很好。但是它不起作用,我有点沮丧,因为这是我能想到的最简单的例子
那么我做错了什么
我以前也使用过相同类型的实现,但用于SOAP项目,效果很好。这次我不能这么做。兄弟,我们需要使用WebHttpBinding来创建Restful风格的WCF服务。 这里有一个例子。 当我们创建Restful风格的服务时,Webservicehost也需要承载该服务。或者,我们可以向主机添加WebHttpBehavior端点行为,如下所示
static void Main(string[] args)
{
Uri uri = new Uri("http://localhost:9999");
WebHttpBinding binding = new WebHttpBinding();
using (ServiceHost sh=new ServiceHost(typeof(MyService),uri))
{
ServiceEndpoint se=sh.AddServiceEndpoint(typeof(IService),binding,"");
se.EndpointBehaviors.Add(new WebHttpBehavior());
sh.Open();
Console.WriteLine("Service is ready....");
Console.ReadLine();
sh.Close();
}
}
结果。
此外,对于BodyStyle=WebMessageBodyStyle.Wrapped属性,当我们有自定义对象参数时,请注意数据格式
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
[OperationContract]
bool Save(BatchOfRows request);
假设BatchOfRows有一个ID属性。根据您的定义,Json数据应该是
{“请求”:{“ID”:1}
详情请参阅。
如果有什么我可以帮忙的,请随时告诉我。但现在我有另一个问题……我简化了BatchOrRows类,以确保这不是问题所在。现在我又添加了完整的类,它停止了分叉。如果我删除了所有的datetime属性,它就可以工作了…知道如何解决吗?没关系…它可以用这个工作了,它对http非常有效…但对https不起作用。还有很多需要改变才能让https正常工作吗?为了避免在评论中出现冗长的讨论,请发布一个新的帖子,我会跟进。我把它作为一个新问题发布,我真的很希望你能帮上忙!
static void Main(string[] args)
{
Uri uri = new Uri("http://localhost:9999");
WebHttpBinding binding = new WebHttpBinding();
using (ServiceHost sh=new ServiceHost(typeof(MyService),uri))
{
ServiceEndpoint se=sh.AddServiceEndpoint(typeof(IService),binding,"");
se.EndpointBehaviors.Add(new WebHttpBehavior());
sh.Open();
Console.WriteLine("Service is ready....");
Console.ReadLine();
sh.Close();
}
}
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
[OperationContract]
bool Save(BatchOfRows request);