我尝试在C#中使用简单的预处理程序指令进行调试和发布模式.
例如:
using System;
public class C {
public void M() {
#if DEBUG
Console.WriteLine("Debug");
#else
Console.WriteLine("Release");
#endif
}
}
Run Code Online (Sandbox Code Playgroud)
这段代码非常简单明了.
但是当我看到IL代码时,我没有关于debug指令的任何内容.
.class private auto ansi '<Module>'
{
} // end of class <Module>
.class public auto ansi beforefieldinit C
extends [mscorlib]System.Object
{
// Methods
.method public hidebysig
instance void M () cil managed
{
// Method begins at RVA 0x2050
// Code size 13 (0xd)
.maxstack 8
IL_0000: nop
IL_0001: ldstr "Release"
IL_0006: call void [mscorlib]System.Console::WriteLine(string)
IL_000b: nop
IL_000c: …Run Code Online (Sandbox Code Playgroud) 我有一个something.csproj项目,像这样:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp1.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Data.SqlClient" Version="4.1.0" />
</ItemGroup>
</Project>
Run Code Online (Sandbox Code Playgroud)
和诸如此类的something.cs程序:
using System;
using System.IO;
using System.Data.SqlClient;
namespace SomethingNS
{
public class Something
{
public static void Main()
{
var a = new SqlCommand();
}
}
}
Run Code Online (Sandbox Code Playgroud)
使用dotnet 2.0,我执行:
dotnet publish
Run Code Online (Sandbox Code Playgroud)
但是我看不到System.Data.SqlClient.dllat文件夹\bin\Debug\netcoreapp1.0\publish\System.Data.SqlClient.dll吗?
这是为什么?
我知道有IServiceCollection接口可以注册我的服务,IServiceProvider并且可以实例化服务.
如何基于使用已注册服务的指定Type实例化一个类?
class MyClass
{
public MyClass(ISomeService someService) { }
}
var serviceCollection = new ServiceCollection();
serviceCollection.AddSingleton<ISomeService, SomeService>()
MyClass instance = CreateInstance(typeof(MyClass));
object CreateIntance(Type type)
{
???
}
Run Code Online (Sandbox Code Playgroud)
例如,ASP.NET Core如何创建控制器实例?
我已经初步实现了激活器,但是在.NET Core中已经没有这样的东西吗?
private static object CreateInstance(Type type, IServiceProvider serviceProvider)
{
var ctor = type.GetConstructors()
.Where(c => c.IsPublic)
.OrderByDescending(c => c.GetParameters().Length)
.FirstOrDefault()
?? throw new InvalidOperationException($"No suitable contructor found on type '{type}'");
var injectionServices = ctor.GetParameters()
.Select(p => serviceProvider.GetRequiredService(p.ParameterType))
.ToArray();
return ctor.Invoke(injectionServices);
}
Run Code Online (Sandbox Code Playgroud)
}
编辑:这是我的情景.我重构了一些实现此接口的遗留代码.
public interface …Run Code Online (Sandbox Code Playgroud) 我刚刚开始研究Apress“使用Visual Studio 2017构建Web应用程序”。我正在开始设置项目。因此,我建立了一个名为SpyStore的解决方案。然后是一个名为SpyStore.DAL的.Net Core控制台应用程序。然后是一个名为SpyStore.Models的.Net标准类库项目。
我的问题是作者为什么会选择.Net标准类库而不是.Net核心类库。有区别吗?
.Net Standard和.Net Core有什么区别。突然之间,他有可能混用Standard吗?
我一直在尝试使用dotnet核心,我写了下面一段代码,它是一个更大的代码:
var account = new Account();
account.FirstName = "Tarik";
account.LastName = "World";
account.Balance = 1000;
pipelineObjects.Select(a => a.Process(account));
Run Code Online (Sandbox Code Playgroud)
pipelineObjects简直就是一个List<IAccountPipeline>.IAccountPipeline定义一个名为的方法void Process(Account account);.
每个pipelineObject在pipelineObjects给定Account对象中的变化列表中.例如,
public void Process(Account account) {
account.Balance = account.Balance - (account.Balance / 10 * 100);
}
Run Code Online (Sandbox Code Playgroud)
但是,当我在顶部运行代码时,帐户对象不会更新.我将它转储到控制台,我得到的是我第一次创建对象时使用的属性值.
但是,当我使用下面的代码代替时.Select(a => a.Process(account)),我开始看到它account已更新:
foreach (var pipelineObject in pipelineObjects)
{
pipelineObject.Process(account);
}
Run Code Online (Sandbox Code Playgroud)
起初,我怀疑lambda表达式中的闭包.但是,account对象的变量点,我在lambda表达式中传递它的引用.我错了吗?CLR是否克隆account对象并将其传递给对象.Process(account)?我认为这与.Select().NET中的闭包没有任何关系.知道为什么会这样吗?
我想准备所有要通过FTP上传的文件.我试过命令dotnet publish -c Release -o ~/Desktop/toFtp.不幸的是,视图文件夹未被复制 有办法吗?
我有一个dotnet核心1.1 web-api,它使用ILogger登录到控制台.如果我在azure控制台中打开监控/诊断日志下的应用程序日志记录(Blob)选项,则会拾取这些日志并将其发送到基于(appname)/ month/day/hour的Blob.这很好用.
但是,我的webjob使用相同的ILogger和相同的配置没有输出到这些mm/dd/hh目录.
如果我打开应用程序日志(文件系统),我会看到我的日志,但我真的希望它们转到相同的blob存储位置,所以我可以在Splunk中选择它们.
我哪里错了?
我想使用Serilog登录.Net core 2.0类库项目.我搜索了很多地方,没有找到任何将Serilog集成到类库项目中的示例.有人可以参考样品吗?
谢谢.
有没有人有将.NET / Angular项目发布到Netlify的经验?我正在使用Angular Microsoft.AspNetCore.SpaTemplates模板。在Netlify上,我得到了一个非零的退出代码,这使我无法发布。这是我的输出:
9:44:44 AM: Build ready to start
9:44:44 AM: Fetching cached dependencies
9:44:44 AM: Starting to download cache of 8.9MB
9:44:45 AM: Finished downloading cache in 225.765972ms
9:44:45 AM: Preparing Git Reference refs/heads/master
9:45:00 AM: Running build command: dotnet restore && dotnet build && dotnet run
9:45:02 AM: Downloading and installing node v6.11.4...
9:45:02 AM: Downloading https://nodejs.org/dist/v6.11.4/node-v6.11.4-linux-x64.tar.xz...
9:45:03 AM:
9:45:03 AM: 0.0%
9:45:03 AM:
1.1%
9:45:03 AM:
### 4.9%
9:45:03 AM:
##############
9:45:03 AM: 19.8%
9:45:03 AM: …Run Code Online (Sandbox Code Playgroud) 我正在编写一个.Net Core C#控制台程序.在测试时,使用项目属性中定义的环境变量很方便:
我写了这样简单的代码:
string rdsDatabase = System.Environment.GetEnvironmentVariable("RDSDatabase");
Run Code Online (Sandbox Code Playgroud)
我刚刚了解到没有.exe创建,你必须使用"DotNet"来运行.net核心控制台程序(https://blogs.msdn.microsoft.com/benjaminperkins/2017/03/07/net-core -application-where-my-exe-how-to-publish /).在那种情况下,我在哪里将这些环境变量放在运行时?我知道有一些替代方法,如本文所述:https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration?tabs=basicconfiguration.我可以保持简单并使用GetEnvironmentVariable吗?