我有一个要针对 WCF GET 服务运行的 AJAX 调用。基本上,对服务的调用(通过 jquery)如下所示:
$.get(serviceEndpoint, {query : "some search text", statusTypes: [1, 2]}, function (result) { /* do something*/ }, 'text');
当此调用运行时,我看到 firebug 中的 GET 正确通过,并且我确实到达了端点。但是,参数 statusTypes 始终为 null。
jquery 中的 GET 本身看起来像是经过编码的,但是当我不对括号进行编码时,调用根本不会进入端点:
以及 WCF 服务本身:
[OperationContract]
[WebInvoke(Method= "GET", BodyStyle = WebMessageBodyStyle.WrappedRequest,
ResponseFormat = WebMessageFormat.Json)]public ResultsViewModel GetTags(string query, int[] statusTypes)
是否可以通过 GET 将数组传递给 WCF 服务?
排列并不多,因此我可以“为每个数组”编写一个单独的端点,但我宁愿将其保留在一个中。
最佳答案
这是可能的,但不是开箱即用的 WCF。 WCF codeplex page 中的“jQuery 支持” ,您可以在无类型变量中接收 jQuery 发送的所有数据(包括数组、嵌套对象等),包括正文(对于 POST 请求)和查询字符串(对于 GET)。 jQuery 数组变量(其名称包含“[”和“]”)和操作参数之间的映射不能在 WCF 4.0 中完成(至少不能在不编写消息格式化程序的情况下完成)。
但是,在新的 WCF Web API 上(也可在 codeplex 站点上获得),这应该更简单。
更新:这是适用于您的场景的格式化程序示例:
public class StackOverflow_6445171
{
[ServiceContract]
public class Service
{
[WebGet(ResponseFormat = WebMessageFormat.Json)]
public string GetLabelPacketTags(string query, int[] statusTypes)
{
StringBuilder sb = new StringBuilder();
sb.Append("Query=" + query);
sb.Append(", statusTypes=");
if (statusTypes == null)
{
sb.Append("null");
}
else
{
sb.Append("[");
for (int i = 0; i < statusTypes.Length; i++)
{
if (i > 0) sb.Append(",");
sb.Append(statusTypes[i]);
}
sb.Append("]");
}
return sb.ToString();
}
}
class MyWebHttpBehavior : WebHttpBehavior
{
protected override IDispatchMessageFormatter GetRequestDispatchFormatter(OperationDescription operationDescription, ServiceEndpoint endpoint)
{
return new MyArrayAwareFormatter(operationDescription, this.GetQueryStringConverter(operationDescription));
}
class MyArrayAwareFormatter : IDispatchMessageFormatter
{
OperationDescription operation;
QueryStringConverter queryStringConverter;
public MyArrayAwareFormatter(OperationDescription operation, QueryStringConverter queryStringConverter)
{
this.operation = operation;
this.queryStringConverter = queryStringConverter;
}
public void DeserializeRequest(Message message, object[] parameters)
{
if (message.Properties.ContainsKey("UriMatched") && (bool)message.Properties["UriMatched"])
{
UriTemplateMatch match = message.Properties["UriTemplateMatchResults"] as UriTemplateMatch;
NameValueCollection queryValues = match.QueryParameters;
foreach (MessagePartDescription parameterDescr in this.operation.Messages[0].Body.Parts)
{
string parameterName = parameterDescr.Name;
int index = parameterDescr.Index;
if (parameterDescr.Type.IsArray)
{
Type elementType = parameterDescr.Type.GetElementType();
string[] values = queryValues.GetValues(parameterName + "[]");
Array array = Array.CreateInstance(elementType, values.Length);
for (int i = 0; i < values.Length; i++)
{
array.SetValue(this.queryStringConverter.ConvertStringToValue(values[i], elementType), i);
}
parameters[index] = array;
}
else
{
parameters[index] = this.queryStringConverter.ConvertStringToValue(queryValues.GetValues(parameterName)[0], parameterDescr.Type);
}
}
}
}
public Message SerializeReply(MessageVersion messageVersion, object[] parameters, object result)
{
throw new NotSupportedException("This is a request-only formatter");
}
}
}
public static void Test()
{
string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress));
host.AddServiceEndpoint(typeof(Service), new WebHttpBinding(), "").Behaviors.Add(new MyWebHttpBehavior());
host.Open();
Console.WriteLine("Host opened");
WebClient c = new WebClient();
Console.WriteLine(c.DownloadString(baseAddress + "/GetLabelPacketTags?query=some+text&statusTypes[]=1&statusTypes[]=2"));
Console.WriteLine(c.DownloadString(baseAddress + "/GetLabelPacketTags?query=some+text&statusTypes%5B%5D=1&statusTypes%5B%5D=2"));
Console.Write("Press ENTER to close the host");
Console.ReadLine();
host.Close();
}
}
关于c# - 通过 GET 将数组传递给 WCF 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6445171/
我正在尝试使用ruby和Savon来使用网络服务。测试服务为http://www.webservicex.net/WS/WSDetails.aspx?WSID=9&CATID=2require'rubygems'require'savon'client=Savon::Client.new"http://www.webservicex.net/stockquote.asmx?WSDL"client.get_quotedo|soap|soap.body={:symbol=>"AAPL"}end返回SOAP异常。检查soap信封,在我看来soap请求没有正确的命名空间。任何人都可以建议我
我正在尝试设置一个puppet节点,但rubygems似乎不正常。如果我通过它自己的二进制文件(/usr/lib/ruby/gems/1.8/gems/facter-1.5.8/bin/facter)在cli上运行facter,它工作正常,但如果我通过由rubygems(/usr/bin/facter)安装的二进制文件,它抛出:/usr/lib/ruby/1.8/facter/uptime.rb:11:undefinedmethod`get_uptime'forFacter::Util::Uptime:Module(NoMethodError)from/usr/lib/ruby
我想安装一个带有一些身份验证的私有(private)Rubygem服务器。我希望能够使用公共(public)Ubuntu服务器托管内部gem。我读到了http://docs.rubygems.org/read/chapter/18.但是那个没有身份验证-如我所见。然后我读到了https://github.com/cwninja/geminabox.但是当我使用基本身份验证(他们在他们的Wiki中有)时,它会提示从我的服务器获取源。所以。如何制作带有身份验证的私有(private)Rubygem服务器?这是不可能的吗?谢谢。编辑:Geminabox问题。我尝试“捆绑”以安装新的gem..
我有多个ActiveRecord子类Item的实例数组,我需要根据最早的事件循环打印。在这种情况下,我需要打印付款和维护日期,如下所示:ItemAmaintenancerequiredin5daysItemBpaymentrequiredin6daysItemApaymentrequiredin7daysItemBmaintenancerequiredin8days我目前有两个查询,用于查找maintenance和payment项目(非排他性查询),并输出如下内容:paymentrequiredin...maintenancerequiredin...有什么方法可以改善上述(丑陋的)代
我的代码目前看起来像这样numbers=[1,2,3,4,5]defpop_threepop=[]3.times{pop有没有办法在一行中完成pop_three方法中的内容?我基本上想做类似numbers.slice(0,3)的事情,但要删除切片中的数组项。嗯...嗯,我想我刚刚意识到我可以试试slice! 最佳答案 是numbers.pop(3)或者numbers.shift(3)如果你想要另一边。 关于ruby-多次弹出/移动ruby数组,我们在StackOverflow上找到一
尝试通过RVM将RubyGems升级到版本1.8.10并出现此错误:$rvmrubygemslatestRemovingoldRubygemsfiles...Installingrubygems-1.8.10forruby-1.9.2-p180...ERROR:Errorrunning'GEM_PATH="/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/ruby-1.9.2-p180@global:/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/rub
我需要读入一个包含数字列表的文件。此代码读取文件并将其放入二维数组中。现在我需要获取数组中所有数字的平均值,但我需要将数组的内容更改为int。有什么想法可以将to_i方法放在哪里吗?ClassTerraindefinitializefile_name@input=IO.readlines(file_name)#readinfile@size=@input[0].to_i@land=[@size]x=1whilex 最佳答案 只需将数组映射为整数:@land边注如果你想得到一条线的平均值,你可以这样做:values=@input[x]
我正在使用puppet为ruby程序提供一组常量。我需要提供一组主机名,我的程序将对其进行迭代。在我之前使用的bash脚本中,我只是将它作为一个puppet变量hosts=>"host1,host2"我将其提供给bash脚本作为HOSTS=显然这对ruby不太适用——我需要它的格式hosts=["host1","host2"]自从phosts和putsmy_array.inspect提供输出["host1","host2"]我希望使用其中之一。不幸的是,我终其一生都无法弄清楚如何让它发挥作用。我尝试了以下各项:我发现某处他们指出我需要在函数调用前放置“function_”……这
这个问题在这里已经有了答案:Checktoseeifanarrayisalreadysorted?(8个答案)关闭9年前。我只是想知道是否有办法检查数组是否在增加?这是我的解决方案,但我正在寻找更漂亮的方法:n=-1@arr.flatten.each{|e|returnfalseife
我正在编写一个gem,我必须在其中fork两个启动两个webrick服务器的进程。我想通过基类的类方法启动这个服务器,因为应该只有这两个服务器在运行,而不是多个。在运行时,我想调用这两个服务器上的一些方法来更改变量。我的问题是,我无法通过基类的类方法访问fork的实例变量。此外,我不能在我的基类中使用线程,因为在幕后我正在使用另一个不是线程安全的库。所以我必须将每个服务器派生到它自己的进程。我用类变量试过了,比如@@server。但是当我试图通过基类访问这个变量时,它是nil。我读到在Ruby中不可能在分支之间共享类变量,对吗?那么,还有其他解决办法吗?我考虑过使用单例,但我不确定这是