在OSX上使用make(Using make on OSX)
我有一个macbook我正在做一些开发。 我有一个程序,我想建立,当我去使用
make
来构建它,我有一个“命令未找到”的错误。 我做了一些谷歌和SO搜索,它看起来不像这是一个常见的问题。 为什么我没有安装,我该如何得到它? 我非常困惑,因为我知道我在这台笔记本电脑上最近使用它(在过去一个月左右)。I have a macbook I'm trying to do some development on. I have a program I want to build, and when I went to use
make
to build it I got a "command not found" error. I did some google and SO searches and it doesn't look like this is a common problem. Why don't I have make installed and how do I get it? I'm extra confused because I know I used it relatively recently (in the past month or so) when I was on this laptop.
原文:https://stackoverflow.com/questions/1469994
最满意答案
我终于想出了一个解决方案。 这不是我会喜欢的(这将是返回特定的对象类型,并以某种方式指示WCF使用Json.Net序列化程序,而不是DataContractJsonSerializer),但它的工作非常好,它很简单明了。
使用这个新的解决方案来扩展我的例子:
[WebGet(UriTemplate = "hello")] public void SayHello() { SimpleMessage message = new SimpleMessage() {Message = "Hello World"}; string json = JsonConvert.Serialize(message); HttpContext.Current.Response.ContentType = "application/json; charset=utf-8"; HttpContext.Current.Response.Write(json); }
注意
void
的返回类型。 我们不返回任何东西,因为它将被DataContractJsonSerializer序列化。 相反,我直接写入响应输出流。 由于返回类型为void,因此处理流水线未将content-type设置为默认类型“application / json”,因此我明确设置。因为这使用了
HttpContext
,所以我猜测它只有在您的服务类上有[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
才能工作,因为这将强制对服务的请求通过ASP.NET管道。 没有asp.net兼容性,HttpContext将不可用,因为wcf主机应该是主机不可知的。使用这种方法,结果在GET请求的firebug中看起来很完美。 正确的内容类型,正确的内容长度和raw json,不包含在引号中。 而且,我正在使用Json.Net来获得序列化。 两全其美。
当我的服务方法具有[DataContract]对象类型作为输入参数时,我并不是100%肯定了我可能遇到的关于序列化的障碍。 我假设DataContractJsonSerializer也将被用于。 当我来到它时,会穿过这座桥,如果它造成了一个问题。 迄今为止,我的简单DTO还没有。
更新请参阅Oleg的答案(UPDATE2部分)。 他将服务方法的返回类型从void更改为
System.ServiceModel.Channels.Message
,而不是使用HttpContext.Current.Response.Write()
,他使用:return WebOperationContext.Current.CreateTextResponse (json, "application/json; charset=utf-8", Encoding.UTF8);
这确实是一个更好的解决方案。 谢谢奥列格
更新2还有另一种方法来实现这一点。 将您的服务的返回类型从消息更改为流,然后返回:
WebOperationContext.Current.OutgoingResponse.ContentType = "application/json; charset=utf-8"; return new MemoryStream(System.Text.Encoding.UTF8.GetBytes(json));
我没有做任何具体的测试,但是这可能是可能返回大量数据的方法的更好的选择。 我不知道这是非常重要的非二进制数据。 无论如何,一个想法。
I finally figured out a solution to this. It's not what I would have preferred (which would be to return the specific object type, and somehow instruct WCF to use a Json.Net serializer, instead of the DataContractJsonSerializer), but it is working great, and it's simple and clear.
Extending my contrived example using this new solution:
[WebGet(UriTemplate = "hello")] public void SayHello() { SimpleMessage message = new SimpleMessage() {Message = "Hello World"}; string json = JsonConvert.Serialize(message); HttpContext.Current.Response.ContentType = "application/json; charset=utf-8"; HttpContext.Current.Response.Write(json); }
Note the return type of
void
. We do not return anything, since it would be serialized with DataContractJsonSerializer. Instead, I write directly to the response output stream. Since the return type is void, the processing pipeline doesn't set the content-type to the default type of "application/json", so I set it explicitly.Because this uses
HttpContext
, I'm guessing it will only work if you have[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
on your service class, since that will force requests to the service to go through the ASP.NET pipeline. Without the asp.net compatibility, the HttpContext will not be available, since wcf hosting is supposed to be host agnostic.Using this method, the results look perfect in firebug for GET requests. Correct content-type, correct content length, and raw json, not wrapped in quotes. And, I'm getting the serialization I want using Json.Net. Best of both worlds.
I'm not 100% positive of what obstacles I might run into regarding deserialization, when my service methods have [DataContract] object types as input parameters. I'm assuming the DataContractJsonSerializer will be used for that too. Will cross that bridge when I come to it...if it creates a problem. It hasn't so far, with my simple DTOs.
UPDATE See Oleg's answer (the UPDATE2 part). He changes the return type of the service method from void to
System.ServiceModel.Channels.Message
, and rather than usingHttpContext.Current.Response.Write()
, he uses:return WebOperationContext.Current.CreateTextResponse (json, "application/json; charset=utf-8", Encoding.UTF8);
Which is indeed a better solution. Thank you Oleg.
UPDATE 2 There is yet another way of accomplishing this. Change your service's return type from Message to Stream, and return this:
WebOperationContext.Current.OutgoingResponse.ContentType = "application/json; charset=utf-8"; return new MemoryStream(System.Text.Encoding.UTF8.GetBytes(json));
I haven't done any specific tests, but it's possible that this would be a better choice for methods that could potentially return large amounts of data. I don't know if that matters for non-binary data though. Anyway, a thought.
相关问答
更多-
我想到了答案后,我感到很沮丧。 有时会发生:)。 我不得不将配置添加到RouteTable。 以下是Global.asax中的代码 public class Global : HttpApplication { void Application_Start(object sender, EventArgs e) { RegisterRoutes(); } private void RegisterRoutes() { // Creat ...
-
c#wcf service使用json.net反序列化json数组数组(c# wcf service deserialize json array of arrays using json.net)[2021-09-16]
假设您的顶级类看起来如下(如果您没有,那么如果努力减少则创建一个,否则我们需要更好的解决方案) internal class TopClass { [JsonProperty("DADOSBI")] public IListVOBiList { get; set; } [JsonProperty("DADOSBO")] public IList VOBOList { get; set; } } 然后简单地反序列化使用 var topClass ... -
而不是使用JSON.NET序列化然后调用Json() ,为什么不重写控制器中的Json()方法(或者可能是基本控制器来提高其可重用性)? 这是从这个博客文章中拉出来的 。 在您的控制器(或基本控制器)中: protected override JsonResult Json(object data, string contentType, System.Text.Encoding contentEncoding, JsonRequestBehavior behavior) { return new ...
-
我终于想出了一个解决方案。 这不是我会喜欢的(这将是返回特定的对象类型,并以某种方式指示WCF使用Json.Net序列化程序,而不是DataContractJsonSerializer),但它的工作非常好,它很简单明了。 使用这个新的解决方案来扩展我的例子: [WebGet(UriTemplate = "hello")] public void SayHello() { SimpleMessage message = new SimpleMessage() {Message = "Hello Wor ...
-
您能否提供一些有关您尝试使用JSON.NET的更多详细信息? 我不确定您要尝试处理的场景 - 但我会向您介绍我对JSON.NET和WCF RESTful服务的一般经验。 我已经构建了许多WCF RESTful服务,主要由我的客户端应用程序使用jQuery的AJAX功能调用。 通常,我不必使用JSON.NET,因为JsonSerializer可以很好地序列化我的返回值(单值或List
集合),并且可以毫无问题地浏览对象。 我必须使用JSON.NET的时候是我在发送一个JSON对象作为我使用JSON2.js ... -
它看起来像HttpUtility.JavaScriptStringEncode可能会解决您的问题。 HttpUtility.JavaScriptStringEncode(JsonConvert.SerializeObject(yourObject)) It looks like HttpUtility.JavaScriptStringEncode might solve your issue. HttpUtility.JavaScriptStringEncode(JsonConvert.Serialize ...
-
我的解决方案 class MyContractResolver : DefaultContractResolver { private JsonObjectContract objectContract = null; public override JsonContract ResolveContract(Type type) { JsonContract contract = base.ResolveContract(type); objectC ...
-
使用JSON.NET与ExpandableObjectConverter的问题(Problems using JSON.NET with ExpandableObjectConverter)[2022-12-25]
虽然我很欣赏Rivers的回答,但我真的在寻找一种自动忽略所有可扩展对象转换器的解决方案(比如DataContractJsonSerializer),而不是为每个违规类构建一个自定义JsonConverter。 我找到了以下两种解决方案: 使用内置的DataContractJsonSerializer(以牺牲JSON.NET的其他一些便利为代价)。 使用自定义ExpandableObjectConverter(参见下文)。 由于默认的ExpandableObjectConverter支持转换为/从字符串转换 ... -
使用JSON.Net的WCF RESTful Web服务:避免双序列化(WCF RESTful web service with JSON.Net: avoid double serialization)[2023-10-27]
据我所知,您正在从头开始创建服务,并且对REST服务的实现方式没有任何限制。 然后我建议你使用ASP.NET WebApi http://www.asp.net/web-api 不要使用传统的Web服务技术,因为在新版本中已经为您完成了许多样板代码。 使用web api,您可以轻松替换或添加任何序列化器/格式化程序。 如何做到如下文所述: http://www.asp.net/web-api/overview/formats-and-model-binding/media-formatters http:/ ... -
我正在使用Newtonsoft序列化日期格式以保持ISODate格式。 我能够解决我的问题: test.cs中: [DataContract] public class Test { public DateTime xaaaaaa { get; set; } [DataMember(Name = "xaaaaaa")] private string HiredForSerialization { get; set; } [OnSerializing] void O ...