小编Tro*_*sen的帖子

正则表达式 - 逃脱转义字符

我的问题很复杂,但可以归结为一个简单的例子.

我正在编写一种自定义查询语言,用户可以在其中输入我解析为LinQ Expressions的字符串.

我想要做的是按*字符分割字符串,除非它被正确转义.

Input         Output                          Query Description
"*\\*"    --> { "*", "\\", "*" }       -- contains a '\'
"*\\\**"  --> { "*", "\\\*", "*" }     -- contains '\*'
"*\**"    --> { "*", "\*", "*" }       -- contains '*' (works now)
Run Code Online (Sandbox Code Playgroud)

我不介意Regex.Split返回空字符串,但我最终得到了这个:

Regex.Split(@"*\\*", @"(?<!\\)(\*)")  --> {"", "*", "\\*"}
Run Code Online (Sandbox Code Playgroud)

正如你所看到的,我尝试过负面的lookbehind,它适用于我的所有情况,除了这个.我也试过了Regex.Escape,但没有运气.

显然,我的问题是我在找\*,哪个\\*匹配.但在这种情况下, \\是另一个转义序列.

任何解决方案都不一定要涉及正则表达式.

c# regex

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

内容类型 text/xml;响应消息的 charset="utf-8" 与绑定的内容类型不匹配 (text/xml; charset=utf-8)

请注意,此问题与 WCF 连接服务的 .NET Core 实现有关。

我正在将一个普通的 .NET WCF 客户端移植到 .NET Core,但我遇到了这个问题:

The content type text/xml; charset="utf-8" of the response message does
not match the content type of the binding (text/xml; charset=utf-8).

If using a custom encoder, be sure that the IsContentTypeSupported method is
implemented properly. The first 1024 bytes of the response were: 
'<?xml version='1.0' encoding='UTF-8'?> [...]
Run Code Online (Sandbox Code Playgroud)

响应确实包含引号:

HTTP/1.1 200 Ok
content-type: text/xml; charset="utf-8"
Run Code Online (Sandbox Code Playgroud)

我从来没有做任何特别的事情来在 WCF 中正确处理这个问题。这是 .NET Core 版本中的错误,还是仅针对内容类型(utf-8 与“utf-8”)进行了具体说明?

如何更改预期的内容类型以匹配我正在调用的服务?(我无法控制它,但如果需要,我可以复制和更改 WSDL)。

我正在使用 svcutil 生成的客户端。(连接服务)

c# wcf http .net-core

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

三次尝试后,Topshelf暂停服务恢复

我正在使用Topshelf创建Windows服务。该服务将尝试恢复前3个故障,但此后将不再起作用。

在主机上的“服务”中检查服务将显示:

First Failure:          Restart the Service
Second Failure:         Restart the Service
Subsequent Failures:    Restart the Service
Reset fail count after: 1 days
Restart service after:  2 minutes
Run Code Online (Sandbox Code Playgroud)

服务恢复代码如下所示:

f.EnableServiceRecovery(r =>
{
    r.RestartService(2);
    r.RestartService(5);
    r.RestartService(5);
    r.OnCrashOnly();
    r.SetResetPeriod(1);
});
Run Code Online (Sandbox Code Playgroud)

恢复失败后,检查事件日志将显示以下消息:

The MyService service terminated unexpectedly.  It has done this 1 time(s).  The following corrective action will be taken in 120000 milliseconds: Restart the service.
The MyService service terminated unexpectedly.  It has done this 2 time(s).  The following corrective action will be …
Run Code Online (Sandbox Code Playgroud)

c# windows-services topshelf

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

UWP - 在类库中加载图像

我有一个通用Windows应用程序,主机主菜单.我想要一个插件架构,其中菜单项是从类库中添加的.

但我似乎无法正确加载图像.我无法ms-appx:///正常工作,当我尝试将图像添加为嵌入式资源时,它会挂起:

var assembly = typeof(CookModule).GetTypeInfo().Assembly;
using (var imageStream = assembly.GetManifestResourceStream("My.Namespace.Folder.ImageName-100.png"))
using (var raStream = imageStream.AsRandomAccessStream())
{
    var bitmap = new BitmapImage();
    bitmap.SetSource(raStream); //<- Hangs here
Run Code Online (Sandbox Code Playgroud)

我没有例外,输出中的错误或任何东西.它只是挂在那里,应用程序根本不加载页面.

我也尝试过:

var bitmap = new BitmapImage(new Uri("/Folder/ImageName-100.png"));
Run Code Online (Sandbox Code Playgroud)

我遗漏了类似于WPF包uri的东西,我可以说明从哪个程序集加载图像.

从类库中向图像添加图像资源的正确(和工作)方式是什么?(或者有没有人有ms-appx的工作示例,其中图像在类库中)

c# xaml uwp

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

UWP - 将TextBox.Text绑定到Nullable <int>

它是否正确无法绑定到Nullable<T>Universal XAML应用程序中的任何应用程序?

我从2013年发现了这个链接:

https://social.msdn.microsoft.com/Forums/en-US/befb9603-b8d6-468d-ad36-ef82a9e29749/textbox-text-binding-on-nullable-types?forum=winappswithcsharp

说明:

Windows 8商店应用程序不支持绑定到可空值.它只是没有进入这个版本.我们已经在v.Next上遇到了这种行为的错误.

但它真的可以解决这个问题吗?

我的约束力:

<TextBox Text="{Binding Serves, Mode=TwoWay}" Header="Serves"/>
Run Code Online (Sandbox Code Playgroud)

我的财产:

public int? Serves
{
    get { return _serves; ; }
    set
    {
        _serves = value;
        OnPropertyChanged();
    }
}
Run Code Online (Sandbox Code Playgroud)

我在输出中得到的错误:

Error: Cannot save value from target back to source. 
BindingExpression: 
    Path='Serves' 
    DataItem='MyAssembly.MyNamespace.RecipeViewModel, MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'; target element is 'Windows.UI.Xaml.Controls.TextBox' (Name='null'); target property is 'Text' (type 'String').
Run Code Online (Sandbox Code Playgroud)

c# data-binding uwp

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

如何在 Raspberry Pi 上的 .NET 6 中安全地写入 SerialPort

我试图在 .NET 6 中为串行通信添加一些安全性,但是当没有任何东西连接到端口(在我的测试台上)时,我无法正确处理端口,因为任何调用SerialPort.Close()SerialPort.Dispose()挂起。

我已设法将其简化为以下 MRE,我在带有 Raspberry Pi 操作系统的 Raspberry Pi 4 上运行该 MRE。我正在将我的应用程序构建为依赖于框架且可移植的应用程序。

using System.IO.Ports;

var sp = new SerialPort("/dev/ttyAMA0", 38400)
{
    DataBits = 8,
    Parity = Parity.None,
    StopBits = StopBits.One,
    WriteTimeout = TimeSpan.FromSeconds(3).Seconds,
    ReadTimeout = TimeSpan.FromMilliseconds(30).Seconds
};

try
{
    sp.Open();
    sp.WriteLine("123");
}
catch (Exception) { }
finally
{
    Console.WriteLine("Closing");
    sp.Close(); //Hangs here
    Console.WriteLine("Closed");

    Console.ReadLine();
}
Run Code Online (Sandbox Code Playgroud)

我可以采取什么措施来避免应用程序挂起?

.net c# serial-port raspberry-pi

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

在ASP.NET Identity中向AspNetUserClaims添加列

Microsoft.AspNet.Identity.Core 2.2.1在解决方案中使用。我需要将此与另一个应自动添加声明的系统集成。

为了跟踪哪些索偿是手动添加的,哪些索偿是由外部系统创建的,我想在AspNetUserClaims表格中添加另一列:

ExternalSystem varchar(64) null
Run Code Online (Sandbox Code Playgroud)

鉴于我的解决方案中实际上没有Claims类,我该如何使用EF迁移(或在没有必要时不使用)进行此操作?

c# asp.net entity-framework asp.net-identity

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

NuGet缺少.nuget文件夹ic:\ Users \ MyUserName

我终于在VS2015上安装了VS2015,以避免为单独安装必要的组件而烦恼,但是现在在构建时遇到了一个新错误。

在我的解决方案上运行MSBuild会产生以下错误:

 "C:\Users\Bamboo\bamboo-home\xml-data\build-dir\IMP-COOK-JOB1\Imp.Cook.Api\Imp.Cook.Api.sln" (default target) (1) ->
"C:\Users\Bamboo\bamboo-home\xml-data\build-dir\IMP-COOK-JOB1\Imp.Cook.Api\Imp.Cook.Api\Imp.Cook.Api.csproj" (default target) (2) ->
"C:\Users\Bamboo\bamboo-home\xml-data\build-dir\IMP-COOK-JOB1\Imp.Cook.Api\Imp.Cook.Models\Imp.Cook.Models.csproj" (default target) (3) ->
(ResolveNuGetPackageAssets target) ->
C:\Program Files (x86)\MSBuild\Microsoft\NuGet\Microsoft.NuGet.targets(89,5):
error : The package Microsoft.CSharp with version 4.0.0 could not be found in 
C:\Users\Bamboo\.nuget\packages. 
Run a NuGet package restore to download the package.
Run Code Online (Sandbox Code Playgroud)

现在,我可以在Bamboo用户目录中手动创建一个.nuget文件夹,这会有所帮助(在下一个包中仍然失败Microsoft.NETCore.Portable.Compatibility)。

但是我在这里错过了什么难题呢?我nuget restore在运行msbuild之前先运行,它获取直接在项目中使用的软件包。

Imp.Cook.Models 是针对Windows 10通用应用程序和.NET 4.6的可移植类库。

生成服务器正在运行Windows Server 2012。

msbuild nuget portable-class-library nuget-package-restore

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