所以我被困在这一点上。我正在尝试与 JanRain 的“auth_info”服务进行通信。事实上,首先,我只是想获得当你直接在浏览器中浏览它时得到的“错误”消息/object/'.jsthingy':
https://rpxnow.com/api/v2/auth_info
但我想通过 WCF 调用来恢复。
根据 Fiddler 的说法,该 url 处信息的内容类型是text/javascript。但是,据我所知,当通过 WCF 调用它时,WCF 并没有给我这个选项。我有两个选择: WebMessageFormat.Json
或WebMessageFormat.Xml
。
我在 Visual Studio 中收到以下错误:
用户代码未处理 InvalidOperationException - 传入消息具有意外的消息格式“原始”。该操作的预期消息格式为“Xml”、“Json”。这可能是因为尚未在绑定上配置 WebContentTypeMapper。有关详细信息,请参阅 WebContentTypeMapper 的文档。
怎么回事?那么 WCF 甚至可以做到这一点吗?(我怀疑前面有一个更手动的解决方案)
JanRain 的在线代码示例在 C# 示例中略有不足。
他们关于 auth_info 的文档链接在这里 https://rpxnow.com/docs#auth_info
他们的 auth_info 服务的地址在这里:
https://rpxnow.com/api/v2/auth_info
[TestMethod]
public void CallJanRain()
{
var x = new JanRainProxy("https://rpxnow.com/api/v2");
x.GetAuthInfo("", ""); //the params are apiKey, and token. not passing either at the moment as I want to be able to know how to at least handle the error first. After all, the *browser* at least got it..
}
[ServiceContract]
public interface IJanRainContract
{
[OperationContract(Name="auth_info")]
[WebInvoke(BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Xml)]
JanRainAuthInfo GetAuthInfo(string apiKey, string token);
}
[DataContract]
public class JanRainAuthInfo
{
[DataMember(Name="identifier")]
public string Identifier { get; set; }
}
public class JanRainProxy: ClientBase<IJanRainContract>
{
public JanRainProxy(string url, WebHttpSecurityMode securityMode = WebHttpSecurityMode.Transport)
: base(ConstructEndpoint(url, securityMode))
{
}
//JanRainContract
public JanRainAuthInfo GetAuthInfo(string apiKey, string token)
{
return base.Channel.GetAuthInfo(apiKey, token);
}
// This method constructs a WebHttpBinding endpoint with all the appropriate
// settings for talking to our services.
private static ServiceEndpoint ConstructEndpoint(string serviceUri, WebHttpSecurityMode securityMode)
{
var contract = ContractDescription.GetContract(typeof(IJanRainContract));
var binding = new WebHttpBinding(securityMode);
//{
// MaxBufferPoolSize = 524288000,
// MaxReceivedMessageSize = 65536000
//};
var address = new EndpointAddress(serviceUri);
var endpoint = new ServiceEndpoint(
contract,
binding,
address);
var webHttpBehavior = new WebHttpBehavior()
{
DefaultBodyStyle = WebMessageBodyStyle.Wrapped,
DefaultOutgoingRequestFormat = WebMessageFormat.Json,
DefaultOutgoingResponseFormat = WebMessageFormat.Json,
AutomaticFormatSelectionEnabled = true,
FaultExceptionEnabled = true
};
endpoint.Behaviors.Add(webHttpBehavior);
return endpoint;
}
}
我想也许我应该将内容类型保留在 json 并调整行为或绑定。