我正在尝试管理我的Web应用程序中的文件.有时,我必须在文件夹中创建一个文件(使用File.Copy):
File.Copy(@oldPath, @newPath);
Run Code Online (Sandbox Code Playgroud)
几秒钟后,该文件可能会被删除:
if (File.Exists(@newPath)) {
File.Delete(@newPath);
}
Run Code Online (Sandbox Code Playgroud)
但是,我不知道为什么新文件在File.Copy之后仍然被服务器进程(IIS,w3wp.exe)阻止.在File.Delete之后我得到了异常:
"该进程无法访问该文件,因为它正由另一个进程使用."
根据Api,File.Copy不会阻止文件,是吗?
我试图释放资源,但它没有奏效.我该如何解决这个问题?
更新: 确实,使用Process Explorer,IIS进程阻止了该文件.我试图实现复制代码以手动释放资源,但问题仍然存在:
public void copy(String oldPath, String newPath)
{
FileStream input = null;
FileStream output = null;
try
{
input = new FileStream(oldPath, FileMode.Open);
output = new FileStream(newPath, FileMode.Create, FileAccess.ReadWrite);
byte[] buffer = new byte[32768];
int read;
while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
{
output.Write(buffer, 0, read);
}
}
catch (Exception e)
{
}
finally
{
input.Close();
input.Dispose();
output.Close();
output.Dispose();
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个角度服务负责加载config.json文件.我想在我的运行阶段调用它,所以我在我的$ rootContext中设置了json ,因此,它将来可供所有人使用.
基本上,这就是我所拥有的:
angular.module('app.core', []).run(function(CoreRun) {
CoreRun.run();
});
Run Code Online (Sandbox Code Playgroud)
我的CoreRun服务在哪里:
angular.module('app.core').factory('CoreRun', CoreRun);
CoreRun.$inject = ['$rootScope', 'config'];
function CoreRun($rootScope, config) {
function run() {
config.load().then(function(response) {
$rootScope.config = response.data;
});
}
return {
run: run
};
}
Run Code Online (Sandbox Code Playgroud)
这工作正常,当我尝试测试它时出现问题.我想监视我的配置服务,所以它返回一个假的承诺.但是,我无法做到,因为在我的测试的配置阶段,服务不可用,我不能注入$ q.
据我所知,在配置阶段,我必须模拟我的配置服务,因为它是由运行块调用的.
到目前为止我找到的唯一方法是使用jQuery产生承诺,我真的不喜欢.
beforeEach(module('app.core'));
var configSample;
beforeEach(module(function ($provide) {
config = jasmine.createSpyObj('config', [ 'load' ]);
config.load.and.callFake(function() {
configSample = { baseUrl: 'someurl' };
return jQuery.Deferred().resolve({data: configSample}).promise();
});
provide.value('config', config);
}));
it('Should load configuration using the …Run Code Online (Sandbox Code Playgroud) 我正在开发一个.NET Web应用程序,我必须使用javascript函数setInterval()来执行ajax请求以刷新某些页面的信息.
对于每个ajax请求,我收到大约68 KB的xml响应,我设法通过jQuery在html中进行可视化更改.我将间隔设置为2000毫秒,但我想,或者更确切地说,我需要将其减少到1000毫秒.
不幸的是,随着每个请求,CPU的内存消耗增加,这引起浏览器被阻止,并且除非他重新加载页面,否则用户不能使用它.我已经在Firefox,Internet Explorer和Chrome中对此进行了测试,但结果始终相同.如果我不做setInvertal(),问题就会消失.
此外,我一直在测试我所有的javascript变量的范围,但我没有发现任何错误,我想Javascript有一个高效的垃圾收集器来清理它们.
我希望我已经解释清楚了这个问题.有人有想法解决它吗?
修改:我正在使用jQuery框架.我的代码是:
var tim;
$(document).ready(function () {
tim = window.setInterval("refresh()", 2000);
});
function refresh() {
$.post("procedures.aspx", $("#formID").serializeArray(), function (data) {
if (data != ""){
var xml = $.parseXML(data);
... (read and process xml to do changes in the html)
}
}
}
Run Code Online (Sandbox Code Playgroud) 我正在使用材料设计构建一个角度应用程序.我正在使用$ mdSidenav服务来玩sidenav,它应该能够随着用户的需求打开和关闭.
我已经围绕它创建了一个包装器服务,如下所示:
(function () {
'use strict';
angular
.module('app.layout')
.factory('navigationService', navigationService);
navigationService.$inject = ['$mdSidenav'];
function navigationService($mdSidenav) {
var factory = {};
factory.toggle = toggle;
return factory;
//////////
function toggle() {
$mdSidenav('left').toggle();
}
}
}());
Run Code Online (Sandbox Code Playgroud)
到目前为止这么好,它工作正常.当我尝试使用Jasmine为它编写单元测试时出现问题.我通常会创建存根或间谍来模拟我的依赖关系,但由于奇怪的使用方法,我无法用这个$ mdSidenav完成它:$ mdSidenav(sidenav-id).
通常,使用Jasmine来窥探你需要和对象以及你想要模拟的功能来监视它.运气好的话,我尝试了几种不同的可能性.
我的目标是类似于:
beforeEach(module(function ($provide) {
mdSidenav = {};
mdSidenav.toggle = jasmine.createSpy();
$provide.value('$mdSidenav', mdSidenav);
}));
beforeEach(inject(function(_navigationService_) {
navigationService = _navigationService_;
}));
it('Should call $mdSidenav toggle when required', function() {
// act
navigationService.toggle();
// assert
expect(mdSidenav.toggle).toHaveBeenCalled();
});
Run Code Online (Sandbox Code Playgroud)
有没有办法测试这个?
我有一个使用express 4的node.js应用程序,这是我的控制器:
var service = require('./category.service');
module.exports = {
findAll: (request, response) => {
service.findAll().then((categories) => {
response.status(200).send(categories);
}, (error) => {
response.status(error.statusCode || 500).json(error);
});
}
};
Run Code Online (Sandbox Code Playgroud)
它调用我的服务,它返回一个promise.一切正常但我在尝试进行单元测试时遇到了麻烦.
基本上,我想确保根据我的服务返回的内容,我使用正确的状态代码和正文刷新响应.
因此,对于mocha和sinon,它看起来像:
it('Should call service to find all the categories', (done) => {
// Arrange
var expectedCategories = ['foo', 'bar'];
var findAllStub = sandbox.stub(service, 'findAll');
findAllStub.resolves(expectedCategories);
var response = {
status: () => { return response; },
send: () => {}
};
sandbox.spy(response, 'status');
sandbox.spy(response, 'send');
// Act
controller.findAll({}, response);
// …Run Code Online (Sandbox Code Playgroud) 我有一个使用 React 的 AWS Amplify 应用程序。我希望TaskList只有在用户成功登录时才能加载(或重新加载)组件。但是,当页面加载时以及用户填写表单并注册时,组件从一开始就呈现出来重新加载。我一直在尝试多种解决方法,但我看不到如何使我的组件依赖于成功登录。我依靠默认的 Amplify 身份验证器功能来让用户登录 Cognito。
const App = () => (
<AmplifyAuthenticator>
<div>
My App
<AmplifySignOut />
<TaskList />
</div>
</AmplifyAuthenticator>
);
Run Code Online (Sandbox Code Playgroud) javascript ×3
unit-testing ×3
angularjs ×2
jasmine ×2
promise ×2
amplifyjs ×1
aws-amplify ×1
c# ×1
cpu ×1
file-io ×1
iis ×1
node.js ×1
reactjs ×1
setinterval ×1
sinon ×1