服务器代码
public class RemoteSink : MarshalByRefObject, RemotingAppender.IRemoteLoggingSink
{
public void LogEvents(LoggingEvent[] events)
{
foreach (var loggingEvent in events)
{
LoggingEventData logData = loggingEvent.GetLoggingEventData();
logData.Message = "[" + logData.Domain + "] " + logData.Message;
log4net.LogManager.GetRepository().Log(new LoggingEvent(logData));
}
}
}
private static void SetRemoteLoggingServer()
{
TcpChannel channel = new TcpChannel(15642);
ChannelServices.RegisterChannel(channel, false);
RemotingConfiguration.RegisterWellKnownServiceType(new WellKnownServiceTypeEntry(typeof(RemoteSink), "LoggingSinkInConsoleDaemon", WellKnownObjectMode.SingleCall));
}
Run Code Online (Sandbox Code Playgroud)
和服务器有一个rollingFileAppender
RollingFileAppender rollingFileAppender = new RollingFileAppender();
rollingFileAppender.DatePattern = @"yyyy-MM-dd.lo\g.\tx\t";
rollingFileAppender.RollingStyle = log4net.Appender.RollingFileAppender.RollingMode.Date;
rollingFileAppender.AppendToFile = true;
rollingFileAppender.File = logPath;
rollingFileAppender.Threshold = Level.All;
rollingFileAppender.StaticLogFileName = false;
rollingFileAppender.Layout …Run Code Online (Sandbox Code Playgroud) 我为我的MVC WebAPI项目设置了常规的OWIN OAuth设置 - 来自前端开发人员的一个请求是能够确定令牌是否因为过期而被拒绝,或者它只是一个无效的令牌.
据我所知,默认情况下,OAuthAuthorizationServerProvider位于中间,并且神奇地拦截查看承载令牌的Authorization标头的请求,并确定是否已对此请求授权或发送401/Authorization.
是否可以自定义此行为/是否有任何其他方法来确定授权被拒绝的原因?
我可以看到存在以下方法:
public virtual Task ValidateAuthorizeRequest(OAuthValidateAuthorizeRequestContext context);
Run Code Online (Sandbox Code Playgroud)
但是我不确定默认的实现是什么,看起来是什么,或者这是否是我应该去定制提供程序以实现我所追求的目标.
我在Startup.cs中的OAuthOptions:
OAuthOptions = new OAuthAuthorizationServerOptions
{
TokenEndpointPath = new PathString("/Token"),
Provider = new ApplicationOAuthProvider(PublicClientId, UserManagerFactory),
AuthorizeEndpointPath = new PathString("/api/Account/ExternalLogin"),
AccessTokenExpireTimeSpan = TimeSpan.FromMinutes(60),
AllowInsecureHttp = true
};
Run Code Online (Sandbox Code Playgroud) 我做了一个topshelf windows服务,启动三个任务.但是,由于可能会发生其中一个任务可能崩溃(是的,我知道EnableServiceRecovery),最好使用一个程序创建具有不同名称的 3个服务并使用命令行参数安装它们.
所以在理论上代码看起来像:
static void Main(string[] args)
{
// *********************Below is a TopShelf code*****************************//
HostFactory.Run(hostConfigurator =>
{
hostConfigurator.Service<MyService>(serviceConfigurator =>
{
serviceConfigurator.ConstructUsing(() => new MyService(args[0])); //what service we are using
serviceConfigurator.WhenStarted(myService => myService.Start()); //what to run on start
serviceConfigurator.WhenStopped(myService => myService.Stop()); // and on stop
});
hostConfigurator.RunAsLocalSystem();
//****************Change those names for other services*******************************************//
hostConfigurator.SetDisplayName("CallForwardService"+args[0]);
hostConfigurator.SetDescription("CallForward using Topshelf"+args[0]);
hostConfigurator.SetServiceName("CallForwardService"+args[0]);
hostConfigurator.SetInstanceName(args[0]);
});
}
Run Code Online (Sandbox Code Playgroud)
但当然它不会,因为(从我读过的)你不能简单地使用,args[]但显然你可以使用类似的东西
Callforward.exe install --servicename:CallForward --instancename:Workshop
Run Code Online (Sandbox Code Playgroud)
我仍然不确定如何传递稍后在程序中使用的参数(在上面的例子中你可以看到它new MyService(args[0])) - 这将是我的问题编号1.问题二(如标题中所示)将是"可以我使用单个参数来设置所有三个元素(名称,实例和内部使用)?
编辑:使用此答案的帮助 …
我正在使用Sprache monadic解析器来解析DSL.
这是我的语法片段:
public static readonly Parser<IExpression> TerminatedStatement =
from exp in Parse.Ref(() => Expression)
from _ in Parse.Char(';').Token()
select exp;
public static readonly Parser<IExpression> Statement =
Parse.Ref(() => TerminatedStatement)
.Or(Parse.Ref(() => InvocationStatement));
public static readonly Parser<Statements> Statements =
from statements in Statement.Many()
select new Statements(statements);
Run Code Online (Sandbox Code Playgroud)
如果我然后使用它,Statements.Parse(" ")我得到一个例外,说输入意外结束.
如何Statements使用Many运算符,AFAIK产生0-n结果.
" "应该返回一个Statements包含0个语句的实例.
那么解析器如何抱怨意外结束输入呢?难道它只是得出结论,那里没有陈述?(不管构成语句的不同表达方式有什么时髦的东西)
我试图通过c#中的TLSharp库在电报上的订阅频道上检测编辑或删除的消息.
1- while(true)循环我得到最新的更新.
2-当我删除或编辑测试消息时,我只收到TLUpdateChannelTooLong.
3-然后我使用client.GetHistoryAsync函数来获取频道消息,并检查他们的EditDate.
但是我不知道我应该在历史中深入了解多少,而且我很难用这段代码找到已删除的消息.
有没有解决方案可以轻松安全地找到已删除/已编辑的邮件?
我的部分代码:
state = await client.SendRequestAsync<TLState>(new TLRequestGetState());
while (true)
{
await Task.Delay(1000);
var req = new TLRequestGetDifference() { Date = state.Date, Pts = state.Pts, Qts = state.Qts };
TLDifference diff = null;
try
{
diff = await client.SendRequestAsync<TLAbsDifference>(req) as TLDifference;
}
catch (Exception ex)
{
HandleThisException(ex);
}
//--
if (diff != null)
{
state = await client.SendRequestAsync<TLState>(new TLRequestGetState());
foreach (var upd in diff.OtherUpdates.OfType<TLUpdateNewChannelMessage>())
{
var tm = (upd.Message as TLMessage);
if …Run Code Online (Sandbox Code Playgroud) 刚刚在网上进行测验,准备面试C#。我读到这个问题:
using System;
// ...
public class Program
{
static void Main(string[] args)
{
Derived d = new Derived();
int i = 10;
d.Func(i);
}
}
public class Base
{
public virtual void Func(int x)
{
Console.WriteLine("Base.Func(int)");
}
}
public class Derived : Base
{
public override void Func(int x)
{
Console.WriteLine("Derived.Func(int)");
}
public void Func(object o)
{
Console.WriteLine("Derived.Func(object)");
}
}
Run Code Online (Sandbox Code Playgroud)
它说输出实际上应该是Derived.Fun(Object)。您能帮我理解为什么会这样做吗?我以为它将调用Func以整数作为参数的命令。
我正在考虑将一些用 VB6 编写的遗留 COM 代码迁移到 .NET,但这需要生成一个与原始代码相当接近的类型库。
在早期绑定到其他 VB6 组件时,我遇到了传递参数数组的问题。在最初的 VB6 中,签名如下所示:
Public Function ExecSPReturnRS(ByVal strProcAs String, _
ParamArray varParams() As Variant) As Recordset
Run Code Online (Sandbox Code Playgroud)
并生成如下所示的 MIDL:
[id(0x60030009), vararg]
HRESULT ExecSPReturnRS([in] BSTR strProc,
[in, out] SAFEARRAY(VARIANT)* varParams,
[out, retval] _Recordset** );
Run Code Online (Sandbox Code Playgroud)
使用 C#,我无法确定生成相同 MIDL 的正确声明。我要么缺少vararg声明,要么 varParams 参数被声明为而SAFEARRAY(VARIANT)不是SAFEARRAY(VARIANT)*.
因此,如果在 C# 中我声明为:
Recordset ExecSPReturnRS(string storedProc, ref object[] arguments);
Run Code Online (Sandbox Code Playgroud)
...我明白了SAFEARRAY(VARIANT)*,但没有vararg。但是如果我声明为
Recordset ExecSPReturnRS(string storedProc, params object[] arguments);
Run Code Online (Sandbox Code Playgroud)
...然后我得到了vararg但 SAFEARRAY 未声明为引用。
我希望这MarshalAsAttribute可能是可行的方法,但到目前为止我能想到的最好方法是:
Recordset …Run Code Online (Sandbox Code Playgroud) 给定以下字符串列表:
string[] Itens = new string[] { "hi", " hi ", "HI", "hí", " Hî", "hi hi", " hí hí ", "olá", "OLÁ", " olá ", "", "ola", "hola", " holà ", "aaaa", "áâàa", " aâàa ", "áaàa", "áâaa ", "aaaa ", "áâaa", "áâaa", };
Run Code Online (Sandbox Code Playgroud)
Distinct操作的结果应为:
hi, hi hi, olá, , hola, aaaa
Run Code Online (Sandbox Code Playgroud)
IEnumerable可用的C#的Distinct操作接受IEqualityComparer作为参数,因此我们可以个性化比较。
以下实现可以完成工作
class LengthHash : IEqualityComparer<string>
{
public bool Equals(string x, string y)
{
if (x == null || y == null) return x == y;
var …Run Code Online (Sandbox Code Playgroud) 我正在尝试.Net Core 3修剪单文件发布,创建一个简单的控制台应用程序并使用它来发布:
dotnet publish -r win-x64 -c Release /p:PublishSingleFile=true /p:PublishTrimmed=true
Run Code Online (Sandbox Code Playgroud)
配置是使用标准appsettings.json和ConfigurationBuilder来自Microsoft.Extensions.Configuration.Json.
问题是配置文件与整个应用程序打包在一起,因此不可用 - 它可以在运行程序时创建的临时目录中访问,但这对普通用户来说不是很有用。
我发现您可以通过编辑 csproj 文件或使用自定义发布配置文件从打包过程中排除文件
<ItemGroup>
<None Update="appsettings.json">
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
</None>
</ItemGroup>
Run Code Online (Sandbox Code Playgroud)
这确实在发布目录中输出 appsettings 文件,但是在运行应用程序时不使用它 - 创建并使用一个空的配置文件。有什么办法可以“正确地”做到这一点,因为文档在这个问题上有点缺乏,或者我应该手动解析我希望位于起始目录中的文件(这本身很难找到,因为Assembly.Get###Assembly都返回了临时目录) ?
我无法使用c# version 8.0 中引入的一些代码。
考虑这段代码。
属性模式
class Address
{
public string State { get; set; }
}
public static decimal ComputeSalesTax(Address location, decimal salePrice) =>
location switch
{
{ State: "WA" } => salePrice * 0.06M,
{ State: "MN" } => salePrice * 0.75M,
{ State: "MI" } => salePrice * 0.05M,
// other cases removed for brevity...
_ => 0M
};
Run Code Online (Sandbox Code Playgroud)
错误:
请帮我解决这个问题,事实上我使用的是最新版本的 vs 代码。
c# ×10
.net ×1
.net-core ×1
asp.net-mvc ×1
c#-8.0 ×1
com-interop ×1
inheritance ×1
log4net ×1
oauth ×1
overloading ×1
overriding ×1
owin ×1
service ×1
sprache ×1
telegram ×1
topshelf ×1
unicode ×1
vb6 ×1