小编Flo*_*ser的帖子

使用 PHPUnit 模拟 Slim 端点 POST 请求

我想使用 PHPUnit 测试 Slim 应用程序的端点。我正在努力模拟 POST 请求,因为请求正文始终为空。

  • 我已经尝试过此处描述的方法:Slim Framework 端点单元测试。(添加环境变量slim-input
  • 我尝试过直接写入php://input,但发现php://input是只读的(困难的方法)

环境模拟工作正常,例如REQUEST_URI始终如预期。我发现请求的正文是Slim\Http\RequestBodyphp://input中读出的。

笔记:

  • 我想避免直接调用控制器方法,这样我就可以测试所有内容,包括端点。
  • 我想避免,guzzle因为它发送了实际的请求。我不想在测试应用程序时运行服务器。

到目前为止我的测试代码:

//inherits from Slim/App
$this->app = new SyncApiApp(); 

// write json to //temp, does not work
$tmp_handle = fopen('php://temp', 'w+');
fwrite($tmp_handle, $json);
rewind($tmp_handle);
fclose($tmp_handle);

//override environment
$this->app->container["environment"] =
    Environment::mock(
        [
            'REQUEST_METHOD' => 'POST',
            'REQUEST_URI' => '/1.0/' . $relativeLink,
            'slim.input' => $json,
            'SERVER_NAME' => 'localhost',
            'CONTENT_TYPE' => 'application/json;charset=utf8'
        ]
    ); …
Run Code Online (Sandbox Code Playgroud)

php slim

6
推荐指数
1
解决办法
4699
查看次数

尝试使用 Microsoft.Office.Interop.Excel.Workbooks.Open() 打开 Excel 时出现异常 HResult 0x800a03ec

如果我尝试在客户端计算机上打开 Excel 文件,则会引发以下异常:

Exception from HRESULT: 0x800A03EC
Run Code Online (Sandbox Code Playgroud)

内部异常:(空)

堆栈跟踪:

   at Microsoft.Office.Interop.Excel.Workbooks.Open(String Filename, Object UpdateLinks, Object ReadOnly, Object Format, Object Password, Object WriteResPassword, Object IgnoreReadOnlyRecommended, Object Origin, Object Delimiter, Object Editable, Object Notify, Object Converter, Object AddToMru, Object Local, Object CorruptLoad)
   at (own assembly)
Run Code Online (Sandbox Code Playgroud)

这个 HResult 是一个非常通用的错误,我找不到任何有用的信息。

我的设置:

在 Windows 服务中运行的 WCF 服务。完全相同的安装程序正在其他三台机器上运行。

我可以排除的事情:

  • 错误的路径
  • 文件不存在
  • 文件已损坏
  • 文件被写保护

我做过的事情:

  • 创建桌面文件夹,如下所示(第二个答案)https://social.msdn.microsoft.com/Forums/vstudio/en-US/4d6c383a-94eb-4898-9d22-aa4bb69be25b/
  • 授予桌面文件夹“Everyone”或“Jeder”权限(“Jeder”相当于德语中的“Everyone”)
  • 使用当前活动用户启动服务
  • 按照 Heidi2 的建议更改 Excel 的 DCOM 配置(请参阅上面的链接)
  • 从 Office 365 更改为 Office Professional Plus
  • 尝试打开文件时区域设置设置为 en-US
  • 手动打开应该打开的文件:没有错误/警告/来自 excel …

excel wcf interop windows-services

5
推荐指数
1
解决办法
1万
查看次数

UWP ComboBox SelectedItem在弹出窗口中未按预期绑定

我没有使用该SelectedItem属性Combobox.项目已正确绑定并显示,但无法更改为其他项目.如果尝试选择其他项目,则项目列表将正确关闭,但SelectedItem不会被调用(也不会设置为setter或getter),并且不会更改显示的所选项目.

我的XAML如下:

<ComboBox
    ItemsSource="{Binding PasswordTypes}"
    ItemTemplate="{StaticResource PasswordTypeTemplate}"
    SelectedItem="{Binding SelectedPasswordType, Mode=TwoWay}"
    />
Run Code Online (Sandbox Code Playgroud)

相关ViewModel代码:

public MyViewModel()
{
    //these are the only two assignments in code of those two properties
    _passwordTypes = new ObservableCollection<PasswordType>(nonEmptyList);
    _selectedPasswordType = PasswordTypes.First();
}

private PasswordType _selectedPasswordType;
public PasswordType SelectedPasswordType
{
    get => _selectedPasswordType;
    set => Set(ref _selectedPasswordType, value);
}

private ObservableCollection<PasswordType> _passwordTypes;
public ObservableCollection<PasswordType> PasswordTypes
{
    get => _passwordTypes;
    set => Set(ref _passwordTypes, value);
}
Run Code Online (Sandbox Code Playgroud)

两个属性的调用如下:

  1. get PasswordTypes 起源于 this.InitializeComponent() …

uwp uwp-xaml

5
推荐指数
1
解决办法
518
查看次数

使用 Windows Hello 存储密码

我想使用 存储和检索密码Windows Hello。用户可以在登录时选择是否要手动输入密码,或者是否要使用Windows Hello解锁(然后检索上次使用的密码,并为用户填写)。

如果Windows Hello设置正确,文档中有两个用例。
只需解锁即可:

UserConsentVerificationResult consentResult = await UserConsentVerifier.RequestVerificationAsync("userMessage");
if (consentResult.Equals(UserConsentVerificationResult.Verified))
{
   // continue
}
Run Code Online (Sandbox Code Playgroud)

以及对来自服务器的消息进行签名的方法:

var openKeyResult = await KeyCredentialManager.OpenAsync(AccountId);
if (openKeyResult.Status == KeyCredentialStatus.Success)
{
    var userKey = openKeyResult.Credential;
    var publicKey = userKey.RetrievePublicKey();
    //the message is the challenge from the server
    var signResult = await userKey.RequestSignAsync(message);

    if (signResult.Status == KeyCredentialStatus.Success)
    {
        //the with the private key of the user signed message        
        return signResult.Result;
    }
}
Run Code Online (Sandbox Code Playgroud)

两者对于我的用例来说都不是很有用:我想要一种对称的方式来存储和检索密码。

简而言之,我的问题是:
有没有办法对称存储数据Windows Hello …

uwp windows-hello

4
推荐指数
1
解决办法
1608
查看次数

通用类型的强制转换失败

我无法将泛型类型转换为另一种泛型类型,除了转换应该是有效的

我想要归档的是简短的(用于MyModel实现IModelMyImplementation实现IImplementation):

IImplementation<IModel> implementation = new MyImplementation<MyModel>();
Assert.IsNull(implementation as IImplementation<IModel>);
Run Code Online (Sandbox Code Playgroud)

这有点令人困惑,因为类型应该是有效的.

完整的概念模型:

interface IModel {}

class MyModel : IModel {}

interface IImplementation<TModel> where TModel : IModel { }

class MyImplementation<TModel> : IImplementation<TModel>
    where TModel : IModel { }

public void CallRegister()
{
    var implementation = new MyImplementation<MyModel>();
    var instance = CastModel(implementation);
    Assert.IsNotNull(instance); //this assert fails!
}

private object CastModel<TModel>(IImplementation<TModel> implementation) where TModel : IModel
{
    return implementation as IImplementation<IModel>;
}
Run Code Online (Sandbox Code Playgroud)

我需要这个强制转换才能让我将多个IImplementation …

c# generics casting

3
推荐指数
2
解决办法
496
查看次数

在XAML中的ItemsControl中拉伸项目

能够达到预期效果的最短代码对我来说:

<ItemsControl ItemsSource="{Binding GeneralBoolSettings}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Grid>
                <ToggleSwitch IsOn="{Binding IsOn, Mode=TwoWay}" OffContent="{Binding OffContent}" OnContent="{Binding OnContent}" />
            </Grid>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
    <ItemsControl.ItemContainerStyle>
        <Style TargetType="ContentPresenter">
            <Setter Property="HorizontalAlignment" Value="Stretch"/>
        </Style>
    </ItemsControl.ItemContainerStyle>
</ItemsControl>
Run Code Online (Sandbox Code Playgroud)

原始问题

我遇到的问题就像拉伸ItemControl水平物品一样简单.当我在使用XAML时,我没有像SharedSizeGroupWPF那样的东西.

这里介绍的解决方案:不幸的是,ItemsControl中的水平拉伸内容对我不起作用.我的代码:

<ItemsControl ItemsSource="{Binding GeneralBoolSettings}" HorizontalContentAlignment="Stretch">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Grid>
                <ToggleSwitch IsOn="{Binding IsOn, Mode=TwoWay}" OffContent="{Binding OffContent}" OnContent="{Binding OnContent}" />
            </Grid>
        </DataTemplate>
        </ItemsControl.ItemTemplate>
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Vertical" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
</ItemsControl>
Run Code Online (Sandbox Code Playgroud)

Designer的一些截图:

ItemControl具有正确的(拉伸)大小 ItemControl具有正确的大小

DataTemplate已经太小了 在此输入图像描述

我想避免绑定到父宽度; 在我以前的尝试中,宽度有时(重新)设置为0,我将不得不删除绑定并再次添加它.另外:请不要使用代码和/或事件捕手,必须有一个优雅的解决方案来解决这个相当基本的问题!

老实说,我有点惊讶我不能让这个工作.Mabye你可以推荐一本好书/网站来学习XAML基础知识的系统方法(当我们在这里时)?

xaml windows-phone-8.1

2
推荐指数
1
解决办法
1545
查看次数

c#从txt文件中读取'n'个随机行

我试图从包含大约200个条目(行)的文本文件中读取(n)大量随机行,并填充名为"recSongs"的列表框.我提供了一些简单的代码,从文本文件中检索一个随机行,但我想要检索n金额.

这是我的代码.

var lines = File.ReadAllLines(@"file.txt");
var r = new Random();
var randomLineNumber = r.Next(0, lines.Length - 1);
var line = lines[randomLineNumber];
recSongs.Items.Add(line);
Run Code Online (Sandbox Code Playgroud)

c# winforms

1
推荐指数
1
解决办法
316
查看次数

设计时的 UWP 资源字符串 (.resw)

我按照https://learn.microsoft.com/en-us/windows/uwp/globalizing/put-ui-strings-into-resources中的说明创建了一个 .resw 文件。虽然本地化在运行时运行良好,但在设计时不会显示任何文本。

到目前为止提出的唯一解决方案似乎只适用于 Windows 8.1 应用程序:设计时的 Windows 应用商店应用程序 ResourceLoader

uwp uwp-xaml

1
推荐指数
1
解决办法
2116
查看次数