我有课ImportProvider
,我想为Import方法编写单元测试.
但这应该是单元测试,所以我不想从文件到流读.任何的想法?
public class ImportProvider : IImportProvider
{
public bool Import(Stream stream)
{
//Do import
return isImported;
}
}
public interface IImportProvider
{
bool Import(Stream input);
}
Run Code Online (Sandbox Code Playgroud)
这是单元测试:
[TestMethod]
public void ImportProvider_Test()
{
// Arrange
var importRepository = new Mock<IImportRepository>();
var imp = new ImportProvider(importRepository.Object);
//Do setup...
// Act
var test_Stream = ?????????????
// This working but not option:
//test_Stream = File.Open("C:/ExcelFile.xls", FileMode.Open, FileAccess.Read);
var result = imp.Import(test_Stream);
// Assert
Assert.IsTrue(result);
}
Run Code Online (Sandbox Code Playgroud) 如何在power shell中使用?
这是C#中的工作示例
using (var conn = new SqlConnection(connString))
{
Console.WriteLine("InUsing");
}
Run Code Online (Sandbox Code Playgroud)
我在Powershell中需要相同(不工作):
Using-Object ($conn = New-Object System.Data.SqlClient.SqlConnection($connString)) {
Write-Warning -Message 'In Using';
}
Run Code Online (Sandbox Code Playgroud)
它不使用以下工作:
$conn = New-Object System.Data.SqlClient.SqlConnection($connString)
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助.
我有一个Web API项目,使用UseJwtBearerAuthentication到我的身份服务器.启动时的配置方法如下所示:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseJwtBearerAuthentication(options =>
{
options.AutomaticAuthentication = true;
options.Authority = "http://localhost:54540/";
options.Audience = "http://localhost:54540/";
});
// Configure the HTTP request pipeline.
app.UseStaticFiles();
// Add MVC to the request pipeline.
app.UseMvc();
}
Run Code Online (Sandbox Code Playgroud)
这是有效的,我想在MVC5项目中做同样的事情.我试着这样做:
Web api:
public class SecuredController : ApiController
{
[HttpGet]
[Authorize]
public IEnumerable<Tuple<string, string>> Get()
{
var claimsList = new List<Tuple<string, string>>();
var identity = (ClaimsIdentity)User.Identity;
foreach (var claim in identity.Claims)
{
claimsList.Add(new Tuple<string, string>(claim.Type, claim.Value));
}
claimsList.Add(new Tuple<string, string>("aaa", "bbb"));
return …
Run Code Online (Sandbox Code Playgroud) c# authentication asp.net-web-api asp.net-mvc-5 asp.net-core-mvc
我正在调查IdentityServer 3如何工作,我仍然有完全理解的问题.
一般来说,概念对我来说很清楚,但我仍然不确定如何在实际项目中实现这一点.
这是我尝试在我的案例中实现的基本示例:link
我有web api项目,我想从任何客户端调用我的api方法(mvc,wpf,phone ...)所以我需要适合所有客户端的实现.
如果我理解得很好(可能我完全不理解),我应该有3个项目:
我的问题是:
编辑:我认为我需要 资源所有者流程.我将该资源用于查看用户输入用户名和密码的位置.
c# security authentication thinktecture-ident-server asp.net-web-api2
是否可以将所有迁移文件合并为一个?
我创建了初始迁移.
dotnet ef migrations add InitialMigration
Run Code Online (Sandbox Code Playgroud)
当我有一些模型更改时,我创建了新的迁移更新.
但是现在我有太多的迁移更新文件.是否可以将所有迁移文件合并为一个?
当然丢弃数据库不是一个选项,我必须保存数据!
我有一个新的ASP.net 5 dnx类库,我用于实体框架.我需要针对EF 6,因为我需要的一些功能不在EF 7中.
首先,EF工具(如启用迁移)不存在.我添加了一个旧的样式类库并安装了EF 6,现在命令就在那里.
当我运行启用迁移时,我收到此错误:
PM>启用 - 迁移
异常调用 "SetData的" 与 "2" 的参数(一个或多个):在装配"类型 'Microsoft.VisualStudio.ProjectSystem.VS.Implementation.Package.Automation.OAProject'"Microsoft.VisualStudio.ProjectSystem.VS.Implementation,版本= 14.1. 0.0,Culture = neutral,PublicKeyToken = b03f5f7f11d50a3a'未标记为可序列化." 在d:\项目\卤面\ FMS \代码\电流\ FMSSupport\FMSSupport \包\ EntityFramework.6.1.3\TOOLS\EntityFramework.psm1:720字符:5 + $ domain.SetData( 'startUpProject',$ startUpProject)+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo:NotSpecified:(:) [],MethodInvocationException + FullyQualifiedErrorId:SerializationException System.NullReferenceException:未将对象引用设置为对象的实例.在System.Data.Entity.Migrations.Extensions.ProjectExtensions.GetProjectTypes(工程项目的Int32 shellVersion)在System.Data.Entity.Migrations.Extensions.ProjectExtensions.IsWebProject(工程项目)在System.Data.Entity.Migrations.MigrationsDomainCommand. GetFacade(字符串configurationTypeName,布尔useContextWorkingDirectory)在System.Data.Entity.Migrations.EnableMigrationsCommand.FindContextToEnable(字符串contextTypeName)在System.Data.Entity.Migrations.EnableMigrationsCommand.<> c__DisplayClass2.<构造函数> b__0()在System.Data .Entity.Migrations.MigrationsDomainCommand.Execute(Action command)对象引用未设置为对象的实例.
我尝试了不同的启用迁移选项,例如指定启动项目或contextTypeName.
有任何想法吗?
如果不存在,我尝试向注册表添加密钥.虽然我调试一切都很好.代码应该工作.但我在注册表编辑器中找不到密钥.你有什么主意吗?
public void ConfigureWindowsRegistry()
{
var reg = Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Office\\Outlook\\FormRegions\\tesssst", true);
if (reg == null)
{
reg = Registry.LocalMachine.CreateSubKey("Software\\Microsoft\\Office\\Outlook\\FormRegions\\tesssst");
}
if (reg.GetValue("someKey") == null)
{
reg.SetValue("someKey", "someValue");
}
}
Run Code Online (Sandbox Code Playgroud) 我正在开始使用实体框架.问题是我的OnModelCreating方法永远不会被调用.
这是我的上下文类:
public class TestContext : DbContext
{
public TestContext()
: base("name=TestDBConnectionString")
{ }
public DbSet<WorkItem> WorkItems { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
}
Run Code Online (Sandbox Code Playgroud)
连接字符串
<add name="TestDBConnectionString"
connectionString="Data Source=(LocalDB)\v11.0;Initial Catalog=TestDB;Integrated Security=true"
providerName="System.Data.SqlClient"/>
Run Code Online (Sandbox Code Playgroud)
我打算在打电话时进入OnModelCreating:
using (var context = new TestContext())
{
}
Run Code Online (Sandbox Code Playgroud)
我犯错误的地方?
我是怎么做到这一点的:
1-启动时设置过滤器:
public IServiceProvider ConfigureServices(IServiceCollection services)
{
//...
services.AddMvc();
services.Configure<MvcOptions>(options =>
{
options.Filters.Add(new RequireHttpsAttribute());
});
Run Code Online (Sandbox Code Playgroud)
2-在cotroler中设置[RequireHttps]
[RequireHttps]
public class HomeController : BaseController
{
public ViewResult Index()
{
return View();
}
}
Run Code Online (Sandbox Code Playgroud)
3-在project.json中添加
"kestrel": "Microsoft.AspNet.Hosting --server=Microsoft.AspNet.Server.Kestrel --server.urls=https://localhost:1234"
Run Code Online (Sandbox Code Playgroud)
仍然没有工作.我做错了什么?
我有Hash Generator类.这应该工作,但我在Visual Studio 2015中有问题.我得到错误
找不到类型或命名空间名称'RNGCryptoServiceProvider'(您是否缺少using指令或程序集引用?)
找不到类型或命名空间名称"SHA256Managed"(您是否缺少using指令或程序集引用?)
问题是这存在于DNX 4.5.1中,但不存在于DNX Core 5.0中
public class HashGenerator : IHashGenerator
{
public string GenerateHash(string input, String salt)
{
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(input + salt);
SHA256Managed sha256hashstring = new SHA256Managed();
byte[] hash = sha256hashstring.ComputeHash(bytes);
return Convert.ToBase64String(bytes);
}
public string CreateSalt(int size)
{
var rng = new RNGCryptoServiceProvider();
var buff = new byte[size];
rng.GetBytes(buff);
return Convert.ToBase64String(buff);
}
}
Run Code Online (Sandbox Code Playgroud)
如何在Visual Studio 2015中的两个框架中使这个工作?
我的project.json文件
"dependencies": {
"EntityFramework.Commands": "7.0.0-beta8-15718",
"4tecture.DataAccess.Common": "1.0.0-*",
"4tecture.DataAccess.EntityFramework": "1.0.0-*",
"EntityFramework.Relational": "7.0.0-beta8-15723",
"EntityFramework.SqlServer": "7.0.0-beta8-15797",
"Microsoft.Framework.ConfigurationModel": "1.0.0-beta5-11337"
}, …
Run Code Online (Sandbox Code Playgroud) c# ×10
asp.net-core ×2
dnx ×2
.net ×1
https ×1
powershell ×1
project.json ×1
registry ×1
security ×1
stream ×1
unit-testing ×1