我试图从URL下载文件,我必须在WebClient和HttpClient之间进行选择.我在互联网上引用了这篇文章和其他几篇文章.无处不在,由于其非常好的异步支持和其他.Net 4.5权限,建议使用HttpClient.但我仍然不完全相信并需要更多的投入.
我使用下面的代码从互联网上下载文件:
Web客户端:
WebClient client = new WebClient();
client.DownloadFile(downloadUrl, filePath);
Run Code Online (Sandbox Code Playgroud)
HttpClient的:
using (HttpClient client = new HttpClient())
{
using (HttpResponseMessage response = await client.GetAsync(url))
using (Stream streamToReadFrom = await response.Content.ReadAsStreamAsync())
{
}
}
Run Code Online (Sandbox Code Playgroud)
从我的角度来看,我只能看到使用WebClient的一个缺点,那就是非异步调用,阻塞调用线程.但是,如果我不担心阻塞线程或使用client.DownloadFileAsync()异步支持,该怎么办?
另一方面,如果我使用HttpClient,是不是我将文件的每个字节加载到内存中然后将其写入本地文件?如果文件太大,内存开销不会很贵吗?如果我们使用WebClient可以避免这种情况,因为它将直接写入本地文件而不会消耗系统内存.
那么,如果性能是我的首要任务,我应该使用哪种方法进行下载?如果我的上述假设是错误的,我想澄清一下,我也愿意接受替代方法.
我有一张表如下:
PersonalDetails
Columns are:
Name
BankName
BranchName
AccountNo
Address
Run Code Online (Sandbox Code Playgroud)
我有另一个包含'Name'和'AccountNo'的列表.我必须从表中找到所有记录,其中"Name"和"AccountNo"分别出现在给定列表中.
任何建议都会有所帮助.
我做了以下但没有太多用处:
var duplicationhecklist = dataAccessdup.MST_FarmerProfile
.Join(lstFarmerProfiles,
t => new { t.Name,t.AccountNo},
t1 => new { t1.Name, t1.AccountNo},
(t, t1) => new { t, t1 })
.Select(x => new {
x.t1.Name,
x.t1.BankName,
x.t1.BranchName,
x.t1.AccountNo
}).ToList();
Run Code Online (Sandbox Code Playgroud)
lstFarmerProfiles列表在哪里.
我有一个带有表单的反应组件。我想对表单是否使用正确的数据提交进行单元测试(使用 jest 和 RTL)。这是我的组件和单元测试方法:
成分:
class AddDeviceModal extends Component {
handleOnSave(event) {
const { deviceName } = event.target;
const formData = {
deviceName: deviceName.value,
};
this.props.onSave(formData);
}
render() {
return (
<Form onSubmit={this.handleOnSave}>
<Form.Label>Device Name</Form.Label>
<Form.Control name="deviceName" placeholder="Device Name" required />
<Button type="submit">Save Device</Button>
</Form>
);
}
}
Run Code Online (Sandbox Code Playgroud)
单元测试:
it("Test form submit and validation", () => {
const handleSave = jest.fn();
const props = {
onSave: handleSave,
};
render(<AddDeviceModal {...props} />);
const deviceNameInput = screen.getByPlaceholderText(/device name/i);
fireEvent.change(deviceNameInput, { target: { …Run Code Online (Sandbox Code Playgroud) 我必须使用http代理连接到Git服务器.我可以通过Git Bash设置它并通过以下命令使用它:
git config --global http.proxy http://proxyuser:proxypwd@proxy.server.com:8080
Run Code Online (Sandbox Code Playgroud)
但是,我正在使用Microsoft Git Provider与Visual Studio集成.我无法在任何地方设置代理连接到Git服务器.有没有办法可以在Visual Studio中保存Microsoft Git Provider的代理详细信息?
我正在尝试使用Microsoft OneDrive API/SDK实现客户端分页.为此,我需要项目的总计数作为API的响应,并且基于传递给API的跳过和最大限制值,应该获取响应.
在List Items链接中,提到我们可以使用此处提供的查询字符串来实现此目的.基于这个假设,我正在构建API调用的URL,如下所示:
string.Format("https://graph.microsoft.com/v1.0/me/drive/root/children?$skip={0}&$top={1}&$count=true",topValue*page, topValue)
Run Code Online (Sandbox Code Playgroud)
根据上面提到的URL中的信息,一切似乎都很好,但我从服务器收到"错误请求",并显示错误消息,如下所示:
{
"error": {
"code": "",
"message": "The query specified in the URI is not valid. Query option 'Skip' is not allowed. To allow it, set the 'AllowedQueryOptions' property on EnableQueryAttribute or QueryValidationSettings.",
"innerError": {
"request-id": "384693d7-65bd-4dc6-8d60-afde68e01555",
"date": "2017-04-25T10:28:15"
}
}
}
{
"error": {
"code": "",
"message": "The query specified in the URI is not valid. Query option 'Count' is not allowed. To allow it, set the 'AllowedQueryOptions' …Run Code Online (Sandbox Code Playgroud) 我使用 OAuth 机制为 SharePoint Online 服务器生成了访问令牌。我正在使用此令牌使用 CSOM 创建 ClientContext。虽然我能够无缝访问所有站点、库和文件夹,但出现错误
远程服务器返回错误:(401) 未经授权。
从 SharePoint Online 下载文件时。以下是我用于文件下载的代码:
var clientContext = TokenHelper.GetClientContextWithAccessToken("https://adventurer.sharepoint.com/Subsite1", accessToken);
var list = clientContext.Web.Lists.GetByTitle("SubSite 1 Library 1");
string vquery = @"<View Scope='RecursiveAll'><Query><Where><Eq><FieldRef Name='UniqueId' /><Value Type='Lookup'>" + "6718053d-a785-489c-877f-5a4b88dcb2a7" + "</Value></Eq></Where></Query></View>";
CamlQuery query = new CamlQuery();
query.ViewXml = vquery;
var listItems = list.GetItems(query);
clientContext.Load(listItems, items => items.Take(1).Include(item => item.File));
clientContext.ExecuteQuery();
var fileRef = listItems[0].File.ServerRelativeUrl;
var fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(clientContext, fileRef);
Run Code Online (Sandbox Code Playgroud)
我不明白此错误的根本原因,因为我正在使用正确的访问令牌传递客户端上下文。我想知道 OpenBinaryDirect 是否有使用访问令牌的限制?如果没有,上面的代码有什么问题?是否有其他替代方法可用于使用访问令牌进行下载?
我正在尝试使用以下 Microsoft Graph 调用从 OneDrive 下载文件:
using (var strm = await client.Drives[RemoteDriveId].Items[Id].Content.Request().GetAsync())
{
byte[] byteBuffer = new byte[4096];
filePath = System.IO.Path.Combine(folderPath, filename);
using (System.IO.FileStream output = new FileStream(filePath, FileMode.Create))
{
int bytesRead = 0;
do
{
bytesRead = contentStream.Read(byteBuffer, 0, byteBuffer.Length);
if (bytesRead > 0)
{
output.Write(byteBuffer, 0, bytesRead);
}
}
while (bytesRead > 0);
}
}
Run Code Online (Sandbox Code Playgroud)
上述代码的问题是,如果文件较大或网络较慢,则在文件完全下载之前,SDK 中会抛出请求超时异常。我想分块下载文件或增加超时时间。如何使用 Microsoft 图形 SDK 实现此目的?
我在 .Net Core 2.2 中有一个 Web API,如下所示:
[Authorize]
[Route("api/[controller]")]
[ApiController]
public class SomeController : ControllerBase
{
[HttpPost]
public async Task<string> SomeMethodPost()
{
string returnUrl = $"{this.Request.Scheme}://{this.Request.Host}{this.Request.PathBase}/some/redirect";
//Some Third Part Service Call
return serviceResult;
}
}
Run Code Online (Sandbox Code Playgroud)
我想在我的单元测试中为我的控制器操作模拟属性“Scheme”、“Host”和“PathBase”。我设法在我的单元测试方法中编写了以下代码:
var request = new Mock<HttpRequest>(MockBehavior.Strict);
request.Setup(x => x.Scheme).Returns("http");
request.Setup(x => x.Host).Returns(HostString.FromUriComponent("http://localhost:8080"));
request.Setup(x => x.PathBase).Returns(PathString.FromUriComponent("/api"));
var mockHttp = new Mock<ControllerBase>(MockBehavior.Strict);
mockHttp.SetupGet(x => x.Request).Returns(request.Object);
Run Code Online (Sandbox Code Playgroud)
但是,最后一行中的模拟会抛出异常,因为“ Request”的“ ControllerBase”是不可覆盖的。我理解抽象类的非虚拟属性的限制。有什么解决方法吗?
最小起订量版本为 4.13.0。
我知道我们可以显示小数到一定数量的地方(如果没有固定的地方).例如,我们可以使用String.Format以下方式显示最多2个位置:
String.Format("{0:0.00}", 123.4567);
Run Code Online (Sandbox Code Playgroud)
但我们的要求是,我们必须从数据库中取不到小数位,并将十进制值显示到该位置.例如:
int n=no of decimal places
Run Code Online (Sandbox Code Playgroud)
我想写一些类似的东西:
String.Format("{0:0.n}", 123.4567);
Run Code Online (Sandbox Code Playgroud)
任何建议都会有很大帮助.
添加注意:String.Format将数字四舍五入.我正在寻找省略剩余数字的东西.
我正在使用 Microsoft Graph SDK 在 OneDrive 中分块上传文件。我正在使用以下代码上传文件:
try
{
GraphServiceClient graphClient = this.GetGraphServiceClient(accessToken);
string fileName = Path.GetFileName(srcFilePath);
using (var fileContentStream = System.IO.File.Open(srcFilePath, System.IO.FileMode.Open))
{
var uploadSession = await graphClient.Me.Drive.Root.ItemWithPath(fileName).CreateUploadSession().Request().PostAsync();
var maxChunkSize = 5 * 1024 * 1024;
var provider = new ChunkedUploadProvider(uploadSession, graphClient, fileContentStream, maxChunkSize);
var chunkRequests = provider.GetUploadChunkRequests();
var readBuffer = new byte[maxChunkSize];
var trackedExceptions = new List<Exception>();
Microsoft.Graph.DriveItem itemResult = null;
foreach (var request in chunkRequests)
{
var result = await provider.GetChunkRequestResponseAsync(request, readBuffer, trackedExceptions);
if (result.UploadSucceeded)
{
itemResult …Run Code Online (Sandbox Code Playgroud)