我在.Net Framework 4.5上运行的ASP.Net MVC 4 Web Api项目中使用HttpClient调用外部服务
示例代码如下(忽略返回值,因为这是测试调用外部服务的示例代码):
public class ValuesController : ApiController
{
static string _address = "http://api.worldbank.org/countries?format=json";
private string result;
// GET api/values
public IEnumerable<string> Get()
{
GetResponse();
return new string[] { result, "value2" };
}
private async void GetResponse()
{
var client = new HttpClient();
HttpResponseMessage response = await client.GetAsync(_address);
response.EnsureSuccessStatusCode();
result = await response.Content.ReadAsStringAsync();
}
}
Run Code Online (Sandbox Code Playgroud)
虽然私有方法中的代码确实可以解决我的问题,但是Controller Get()调用了GetResponse(),但是它没有等待结果,而是立即执行带有result = null的返回.
我也尝试过使用WebClient进行更简单的同步调用,如下所示:
// GET api/values
public IEnumerable<string> Get()
{
//GetResponse();
var client = new WebClient();
result = …Run Code Online (Sandbox Code Playgroud) 我编写简单的WorkerRole,将测试数据添加到表中.插入代码是这样的.
var TableClient = this.StorageAccount.CreateCloudTableClient();
TableClient.CreateTableIfNotExist(TableName);
var Context = TableClient.GetDataServiceContext();
this.Context.AddObject(TableName, obj);
this.Context.SaveChanges();
Run Code Online (Sandbox Code Playgroud)
此代码针对每个客户端请求运行.我用1-30个客户端线程进行测试.我有很多不同大小的实例.我不知道我做错了什么,但我无法达到每秒10次插入.如果有人知道如何提高速度请告诉我.谢谢
UPDATE
我正在尝试使用RestKit的RKObjectManager将JSON数据反序列化到我的iPhone应用程序中.
我目前的问题是应用程序崩溃了:
*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<Course 0x6e71b10> valueForUndefinedKey:]: this class is not key value coding-compliant for the key id.'
Run Code Online (Sandbox Code Playgroud)
我打电话的时候:
[manager loadObjectsAtResourcePath:@"/courses" delegate:nil];
Run Code Online (Sandbox Code Playgroud)
我的域类 - Course.h看起来像
#import <Foundation/Foundation.h>
@interface Course : NSObject {
}
@property(nonatomic) NSInteger *id;
@property(nonatomic, retain) NSString *name;
-(id)initWithIdAndName: (NSInteger *)inId inName:(NSString *)inName;
@end
Run Code Online (Sandbox Code Playgroud)
而Course.m看起来像
#import "Course.h"
#import "NSDictionary+RKAdditions.h"
@implementation Course {
}
@synthesize name = _name;
@synthesize id = _id;
- (id)initWithIdAndName:(NSInteger *)inId inName:(NSString *)inName {
_name = inName;
_id …Run Code Online (Sandbox Code Playgroud) 我想问一个关于正则表达式的简单问题:
我怎样才能比较字符串的开头?
例如:Car [200; 200; 200],自行车[20]
if (item.Name == "Car*") { textBox.Text = "Car"; }
Run Code Online (Sandbox Code Playgroud)
我不知道我必须键入什么而不是"*"来使字符串的其余部分无关紧要.
c# ×4
azure ×1
coding-style ×1
ios5 ×1
objective-c ×1
private ×1
regex ×1
restkit ×1
string ×1
xcode4.2 ×1