相关疑难解决方法(0)

在C#中使用AppDomain

App #mains在C#中最重要的用途是什么?

c# remoting appdomain

72
推荐指数
4
解决办法
2万
查看次数

访问非静态成员需要对象引用

我出现了这个错误,我不知道为什么......我试图查找它,人们说要创建类的对象或创建方法为静态...但我是不确定如何.

这是我的代码如下:

public class SoundManager : MonoBehaviour {

public List<AudioSource> audioSounds = new List<AudioSource>();
public double minTime = 0.5;

public static void playSound(AudioClip sourceSound, Vector3 objectPosition, int volume, float audioPitch, int dopplerLevel)
{
    bool playsound = false;
    foreach (AudioSource sound in audioSounds) // Loop through List with foreach
    {
        if (sourceSound.name != sound.name && sound.time <= minTime)
        {
            playsound = true;
        }
    }

    if(playsound) {
        AudioSource.PlayClipAtPoint(sourceSound, objectPosition);
    }
}
}
Run Code Online (Sandbox Code Playgroud)

.net c# oop static member

22
推荐指数
2
解决办法
13万
查看次数

解决错误 - 无法解析"Serilog.ILogger"类型的服务

我试图在ASP.NET核心应用程序(.NET框架)中实现SeriLog

以下是我目前执行的步骤 -

1)在Project.json中添加了以下引用

"Serilog": "2.2.0",
"Serilog.Extensions.Logging": "1.2.0",
"Serilog.Sinks.RollingFile": "2.0.0",
"Serilog.Sinks.File": "3.0.0"
Run Code Online (Sandbox Code Playgroud)

2)在Startup类的构造函数中添加以下行 -

Log.Logger = new LoggerConfiguration()
           .MinimumLevel.Debug()
           .WriteTo.RollingFile(Path.Combine(env.ContentRootPath, "log-{Date}.txt"))
           .CreateLogger();
Run Code Online (Sandbox Code Playgroud)

3)在Startup类的configure方法中添加以下行 -

loggerFactory.AddSerilog();
Run Code Online (Sandbox Code Playgroud)

4)将记录器注入HomeController,如下所示 -

ILogger logger;

    public HomeController(ILogger logger)
    {
        this.logger = logger;
    }
Run Code Online (Sandbox Code Playgroud)

5)在About操作中,尝试记录此类异常 -

public IActionResult About()
    {
        ViewData["Message"] = "Your application description page.";

        try
        {
            throw new Exception("Serilog Testing");
        }
        catch (System.Exception ex)
        {
            this.logger.Error(ex.Message);
        }

        return View();
    }
Run Code Online (Sandbox Code Playgroud)

在运行我的应用程序时,我收到以下错误 -

System.InvalidOperationException:尝试激活'AspNetCore_SeriLog_trial1.Controllers.HomeController'时,无法解析类型'Serilog.ILogger'的服务.在Microsoft.AspNetCore.Mvc.Internal.TypeActivatorCache.CreateInstance
[TInstance]的lambda_method(Closure,IServiceProvider,Object [])上的Microsoft.Extensions.Internal.ActivatorUtilities.GetService(IServiceProvider sp,Type type,Type requiredBy,Boolean isDefaultParameterRequired)IServiceProvider serviceProvider,Type implementationType),位于Microsoft.AspNetCore.Mvc.Inv.Inter.ControllerActionInvoker.d__26的Microsoft.AspNetCore.Mvc.Controllers.DefaultControllerFactory.CreateController(ControllerContext context)中的Microsoft.AspNetCore.Mvc.Controllers.DefaultControllerActivator.Create(ControllerContext controllerContext). .MoveNext()

有人可以帮我这个吗?Serilog缺少任何配置?

serilog asp.net-core-mvc asp.net-core

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

C# 超出范围的对象没有被收集

我正在尝试构建我的第一个单元测试,并且有一个类可以分别在其构造函数和析构函数中递增和递减实例计数器。我有一个测试来确保它有效但它失败了,似乎我的其他测试中的其他类实例在超出范围时没有调用它们的析构函数。

public class Customer
{
    public Customer()
    {
        ++InstanceCount;
    }

    public Customer(int customerId)
    {
        CustomerId = customerId;
        ++InstanceCount;
    }

    ~Customer()
    {
        --InstanceCount;
    }

    public static int InstanceCount { get; private set; }
}
Run Code Online (Sandbox Code Playgroud)
[TestClass]
public class CustomerTest
    {
    [TestMethod]
    public void FullNameLastNameEmpty()
    {
        //--Arrange
        Customer customer = new Customer {FirstName = "John"};
        const string expected = "John";

        //--Act
        string actual = customer.FullName;

        //--Assert
        Assert.AreEqual(expected, actual);
    }

    [TestMethod]
    public void StaticTest()
    {
        //--Arrange
        Customer[] customers =
        {
            new Customer {FirstName = …
Run Code Online (Sandbox Code Playgroud)

c# garbage-collection unit-testing destructor mstest

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