问题让 WCF 服务侦听不同 POST 请求的相同 URI
Issue getting a WCF service to listen on the same URI for different POST requests
我在让 WCF 服务侦听 URI 并根据收到的 POST 类型进行不同处理时遇到问题...
示例:
WCF 服务是这样公开的。
http://localhost/test
上面的端点将接收以下 POST 请求:
POST http://localhost/test HTTP/1.1
Content-Type: application/json
User-Agent: PostmanRuntime/7.25.0
Accept: */*
Cache-Control: no-cache
Host: localhost
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Content-Length: 42
{"input":"example string"}
端点还将在同一 URI 上收到以下 POST 请求:
POST http://localhost/test HTTP/1.1
Content-Type: application/json
User-Agent: PostmanRuntime/7.25.0
Accept: */*
Cache-Control: no-cache
Host: localhost
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Content-Length: 42
{"input":"example string","turtle":"I Love Turtles"}
我希望能够根据请求体的内容对请求进行不同的处理,我试过这个:
[OperationContract]
[WebInvoke(Method = "POST",
UriTemplate = "test/",
BodyStyle = WebMessageBodyStyle.WrappedRequest,
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json)]
string PostWithOneBis(string input,string turtle);
string PostWithOnePrim(string input);
如前所述,我想根据收到的请求进入第一个功能或第二个功能。我不想设置不同的 URI 来处理这些不同的请求类型...
希望我的解释清楚。
每个[OperationContract]只能修改一个方法,所以你的代码没有解决问题,可以让turtle默认值为null来解决这个问题,对于接收到的请求,需要判断是否turtle为空知道要用哪个方法运行,这里有一个demo:
Class
[OperationContract]
[WebInvoke(Method = "POST",
UriTemplate = "test/{input}/{turtle=null}",
BodyStyle = WebMessageBodyStyle.WrappedRequest,
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json)]
string PostWithOne(string input, string turtle);
实施class
public string PostWithOne(string input, string turtle)
{
if (turtle == null) // string PostWithOnePrim(string input);
{
return "string PostWithOnePrim(string input)";
}
else
{ // string PostWithOneBis(string input,string turtle);
return "string PostWithOneBis(string input,string turtle)";
}
}
我们在PostWithOne中判断是使用PostWithOneBis方法还是PostWithOnePrim方法
如果不在URI中传递参数,PostWithOne会收到任何json,同时还需要判断得到的json为判断是使用 PostWithOnePrim 方法还是 PostWithOneBis 方法。
Class
[OperationContract]
[WebInvoke(Method = "POST",
UriTemplate = "test/",
BodyStyle = WebMessageBodyStyle.WrappedRequest,
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json)]
string PostWithOne(string input, string turtle);
WCF Rest服务是通过URI判断不同的请求,而不是通过请求内容来判断,所以PostWithOneBis(string input, string turtle)和PostWithOnePrim没有区别(字符串输入) 用于 WCF。
一个UriTemplate和一个[OperationContract]只能修改一个方法,UriTemplate不能在一个服务中重复
如果问题仍然存在,请随时告诉我。
我在让 WCF 服务侦听 URI 并根据收到的 POST 类型进行不同处理时遇到问题...
示例:
WCF 服务是这样公开的。
http://localhost/test
上面的端点将接收以下 POST 请求:
POST http://localhost/test HTTP/1.1
Content-Type: application/json
User-Agent: PostmanRuntime/7.25.0
Accept: */*
Cache-Control: no-cache
Host: localhost
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Content-Length: 42
{"input":"example string"}
端点还将在同一 URI 上收到以下 POST 请求:
POST http://localhost/test HTTP/1.1
Content-Type: application/json
User-Agent: PostmanRuntime/7.25.0
Accept: */*
Cache-Control: no-cache
Host: localhost
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Content-Length: 42
{"input":"example string","turtle":"I Love Turtles"}
我希望能够根据请求体的内容对请求进行不同的处理,我试过这个:
[OperationContract]
[WebInvoke(Method = "POST",
UriTemplate = "test/",
BodyStyle = WebMessageBodyStyle.WrappedRequest,
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json)]
string PostWithOneBis(string input,string turtle);
string PostWithOnePrim(string input);
如前所述,我想根据收到的请求进入第一个功能或第二个功能。我不想设置不同的 URI 来处理这些不同的请求类型...
希望我的解释清楚。
每个[OperationContract]只能修改一个方法,所以你的代码没有解决问题,可以让turtle默认值为null来解决这个问题,对于接收到的请求,需要判断是否turtle为空知道要用哪个方法运行,这里有一个demo:
Class
[OperationContract]
[WebInvoke(Method = "POST",
UriTemplate = "test/{input}/{turtle=null}",
BodyStyle = WebMessageBodyStyle.WrappedRequest,
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json)]
string PostWithOne(string input, string turtle);
实施class
public string PostWithOne(string input, string turtle)
{
if (turtle == null) // string PostWithOnePrim(string input);
{
return "string PostWithOnePrim(string input)";
}
else
{ // string PostWithOneBis(string input,string turtle);
return "string PostWithOneBis(string input,string turtle)";
}
}
我们在PostWithOne中判断是使用PostWithOneBis方法还是PostWithOnePrim方法
如果不在URI中传递参数,PostWithOne会收到任何json,同时还需要判断得到的json为判断是使用 PostWithOnePrim 方法还是 PostWithOneBis 方法。
Class
[OperationContract]
[WebInvoke(Method = "POST",
UriTemplate = "test/",
BodyStyle = WebMessageBodyStyle.WrappedRequest,
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json)]
string PostWithOne(string input, string turtle);
WCF Rest服务是通过URI判断不同的请求,而不是通过请求内容来判断,所以PostWithOneBis(string input, string turtle)和PostWithOnePrim没有区别(字符串输入) 用于 WCF。
一个UriTemplate和一个[OperationContract]只能修改一个方法,UriTemplate不能在一个服务中重复
如果问题仍然存在,请随时告诉我。