似乎Mono(4.6.2)在WebInvoke
调用中不支持POST方法,但可能只需要一些额外的调整。
给定服务:
[ServiceContract]
public interface IMyService
{
[OperationContract]
[WebInvoke(Method = "POST",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json)]
string Greet(string input);
}
主持:
public class MyService : IMyService
{
public string Greet(string input)
{
return "Received: " + input;
}
}
public class Host
{
public static void Main()
{
var host = new WebServiceHost(typeof(MyService), new Uri("http://localhost:8080/"));
host.AddServiceEndpoint(typeof(IMyService), new WebHttpBinding(), "");
host.Open();
Console.WriteLine ("--- Press Enter to quit ---");
Console.ReadLine();
host.Close ();
}
}
尝试执行此类客户端调用时:
public class Client
{
public static void Main (string [] args)
{
var f = new WebChannelFactory<IMyService>(new WebHttpBinding(),
new Uri("http://localhost:8080"));
IMyService s = f.CreateChannel();
Console.WriteLine(s.Greet("Test test"));
}
}
客户端失败,但有例外:
System.ArgumentException: JSON prefix must be null or empty. 'z' is specified instead.
Parameter name: prefix
at (wrapper managed-to-native) System.Object:__icall_wrapper_mono_remoting_wrapper (intptr,intptr)
at (wrapper remoting-invoke) IMyService:Greet (string)
at Client.Main (System.String[] args) [0x0001d] in <fde772bc6e9146d497af99b3c03ca760>:0
示例使用Visual Studio 2015编译并正常运行。
我也曾尝试使用BodyStyle=
,UriTemplate=
或将参数放入DataContract
,但它刚刚以其他各种奇怪错误结束。我错过了Mono的“特殊”,或者只是不支持?