所以我被困在这一点。我正在尝试与JanRain的“auth_info”服务进行通信。事实上,开始,我只是试图让“错误”消息/对象/当你直接在浏览器中浏览它,你得到“jsthingy。”:与WCF中的Janrain“auth_info”服务进行通信的问题
https://rpxnow.com/api/v2/auth_info
但我想通过WCF调用返回。
根据Fiddler,该网址信息的内容类型为text/javascript。不过,从我所知道的情况来看,当通过WCF调用WCF时,WCF不会给我这个选项。我有两个选择:WebMessageFormat.Json
或WebMessageFormat.Xml
。
我碰到下面的错误在Visual Studio:
InvalidOperationException异常是由用户代码未处理 - 传入的消息有一个意外的消息格式“原始”。预期的操作消息格式是'Xml','Json'。这可能是因为WebContentTypeMapper尚未在绑定上配置。有关更多详细信息,请参阅WebContentTypeMapper的文档。
WTF?那么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离开的contentType并调整行为或绑定。
不知道,但是这看起来可能是我所需要的:http://msdn.microsoft.com/en-us/库/ bb943479.aspx – KevinDeus