我将反序列化的json文件的结构如下所示;
{
"id" : "1lad07",
"text" : "test",
"url" : "http:\/\/twitpic.com\/1lacuz",
"width" : 220,
"height" : 84,
"size" : 8722,
"type" : "png",
"timestamp" : "Wed, 05 May 2010 16:11:48 +0000",
"user" : {
"id" : 12345,
"screen_name" : "twitpicuser"
}
}
Run Code Online (Sandbox Code Playgroud)
我创建了一个类,其字段名称为JavaScriptSerializer的属性.我将用于反序列化json的代码如下:
using (var reader = new StreamReader(twitpicResponse.GetResponseStream())) {
var responseBody = reader.ReadToEnd();
var deserializer = new JavaScriptSerializer();
var results = deserializer.Deserialize<Response>(responseBody);
}
Run Code Online (Sandbox Code Playgroud)
我的问题是我如何读取json文件上的用户字段.这就像下面;
"user" : {
"id" : 12345,
"screen_name" : "twitpicuser"
}
Run Code Online (Sandbox Code Playgroud)
它有子属性和值.我怎样才能在我的Response类中命名它们.我的回复课现在看起来像这样;
public class Response { …Run Code Online (Sandbox Code Playgroud) 我有包装问题.我正在生成一些hexencoding加密,输出太长了像;
827938828ey823876te37257e5t328er6367r5erd663275e65r532r6s3624e5645376er563rdr753624e544341763r567r4e56r326r5632r65sr32dr32udr56r634r57rd63725
它继续.当我把它放在div中时,即使我指定一个特定的宽度它也不会包裹它,因为它们都在一起.如果div不足以支持一行,我希望它从下一行继续.
我怎样才能做到这一点?
有一些文章指出异步数据库调用在.NET中是个坏主意.
在C#Async CTP上,有一个System.Data.SqlClient.SqlCommand名为的扩展名ExecuteReaderAsync.我在现有代码上有如下操作:
var connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["hubConnectionString"].ConnectionString;
using (var conn = new SqlConnection(connectionString)) {
using (var cmd = new SqlCommand()) {
cmd.Connection = conn;
cmd.CommandText = "sp$DetailsTagsGetAllFromApprovedPropsWithCount";
cmd.CommandType = System.Data.CommandType.StoredProcedure;
conn.Open();
var reader = cmd.ExecuteReader();
while (reader.Read()) {
//do the reading
}
conn.Close();
}
}
Run Code Online (Sandbox Code Playgroud)
在我的代码上有几个这样的操作.所以,我正在考虑将这些转换为异步.
但另一方面,我并没有看到这种方法有多大的吸引力(也许我没有看到正确的方向,谁知道!).
那么,这里使用这种新的异步编程模型有什么缺点吗?
编辑:
假设我重构代码如下:
public async Task<IEnumerable<Foo>> GetDataAsync() {
List<Foo> foos = new List<Foo>();
var connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["hubConnectionString"].ConnectionString;
using (var conn = new SqlConnection(connectionString)) {
using (var cmd …Run Code Online (Sandbox Code Playgroud) 注意:我使用的是Entity Framework第5版
在我的通用库,我有Add,Edit和Delete下面的方法:
public class EntityRepository<T> : IEntityRepository<T>
where T : class, IEntity, new() {
readonly DbContext _entitiesContext;
public EntityRepository(DbContext entitiesContext) {
if (entitiesContext == null) {
throw new ArgumentNullException("entitiesContext");
}
_entitiesContext = entitiesContext;
}
//...
public virtual void Add(T entity) {
DbEntityEntry dbEntityEntry = _entitiesContext.Entry<T>(entity);
if (dbEntityEntry.State != EntityState.Detached) {
dbEntityEntry.State = EntityState.Added;
}
else {
_entitiesContext.Set<T>().Add(entity);
}
}
public virtual void Edit(T entity) {
DbEntityEntry dbEntityEntry = _entitiesContext.Entry<T>(entity);
if (dbEntityEntry.State == …Run Code Online (Sandbox Code Playgroud) 在我的一个应用程序中,我收到了webrequest的回复.该服务是Restful服务,将返回类似于以下JSON格式的结果:
{
"id" : "1lad07",
"text" : "test",
"url" : "http:\/\/twitpic.com\/1lacuz",
"width" : 220,
"height" : 84,
"size" : 8722,
"type" : "png",
"timestamp" : "Wed, 05 May 2010 16:11:48 +0000",
"user" : {
"id" : 12345,
"screen_name" : "twitpicuser"
}
}
Run Code Online (Sandbox Code Playgroud)
这是我目前的代码:
byte[] bytes = Encoding.GetEncoding(contentEncoding).GetBytes(contents.ToString());
request.ContentLength = bytes.Length;
using (var requestStream = request.GetRequestStream()) {
requestStream.Write(bytes, 0, bytes.Length);
using (var twitpicResponse = (HttpWebResponse)request.GetResponse()) {
using (var reader = new StreamReader(twitpicResponse.GetResponseStream())) {
//What should I do here?
}
}
} …Run Code Online (Sandbox Code Playgroud) 是否可以通过AWS ECS(EC2容器服务)在一个EC2实例中运行多个docker容器?
我有一个使用ASP.NET MVC3开发的网站.
我现在想要公开一个REST API供其他人使用,它将公开与网站相同的功能.
在网站中,一旦用户登录并且针对数据库验证了凭证,该会话将管理用户的登录状态.
我如何使用REST API执行等效操作,其中许多公开的方法要求用户登录(或至少具有有效的用户名和密码)?
除此之外,网站的最佳方法是使用REST API(假设API涵盖了网站所需的所有功能)吗?
ASP.NET MVC3的适用性如何 - 当然考虑到该站点已经存在使用此框架?
我在try-catch块中记录了Elmah的错误.这是代码;
try {
DateTime.Parse("poo");
} catch (Exception err) {
Elmah.ErrorSignal.FromCurrentContext().Raise(err);
}
Run Code Online (Sandbox Code Playgroud)
我想记录一些特定信息,例如可以从先前方法或属性在同一上下文中检索的一些信息,但Exception属性是只读的.最好的方法是什么?
我的主要目标是能够做类似下面的事情;
} catch (Exception err) {
err.Message += "poo";
Elmah.ErrorSignal.FromCurrentContext().Raise(err);
}
Run Code Online (Sandbox Code Playgroud) 我的机器中有一个新的mongodb服务器(2.6.0),我使用以下配置文件启动了mongod实例:
dbpath = c:\mongo\data\db
port = 27017
logpath = c:\mongo\data\logs\mongo.log
auth = true
Run Code Online (Sandbox Code Playgroud)
后来,我通过mongo shell连接到这个mongod实例并创建了一个admin用户:
use admin
db.createUser(
{
user: "tugberk",
pwd: "12345678",
roles:
[
{
role: "userAdminAnyDatabase",
db: "admin"
}
]
}
)
Run Code Online (Sandbox Code Playgroud)
然后,我从shell注销并使用以下命令重新连接:
mongo --host localhost --port 27017 -u tugberk -p 12345678 --authenticationDatabase admin
Run Code Online (Sandbox Code Playgroud)
然后,我创建了一个具有root访问权限的用户:
use admin
db.createUser(
{
user: "tugberkRoot",
pwd: "12345678",
roles: [ "root" ]
}
)
Run Code Online (Sandbox Code Playgroud)
这里不需要最后一步,但现在应该完全禁用匿名访问.但是,我仍然可以通过mongo shell匿名连接它(即使我没有任何访问权限):

我该怎么做才能阻止任何匿名连接?
c# ×3
asp.net ×2
json ×2
rest ×2
.net ×1
amazon-ec2 ×1
amazon-ecs ×1
asp.net-mvc ×1
async-await ×1
css ×1
dbcontext ×1
elmah ×1
exception ×1
go ×1
html ×1
mongodb ×1
sql-server ×1