首页 新闻 赞助 找找看

.Net Core 2.1 委托报错 delegate Operation is not supported on this platform

0
悬赏园豆:10 [已解决问题] 解决于 2018-11-23 18:06

各位大神:

  我想问下.Net Core 2.1委托调用方法是报错,以下是保存信息和源码,求各位大神指导一下,没有用第三方框架。

System.PlatformNotSupportedException: Operation is not supported on this platform.
at Uwl.Common.LogsMethod.Log.dgWriteLog.BeginInvoke(Logs logs, AsyncCallback callback, Object object)
at Uwl.Common.LogsMethod.Log.Add(Logs logs) in F:\Project\Uwl.WebApi\I_Sfront.WebApi\Uwl.Common\LogsMethod\Log.cs:line 33
at Uwl.Common.LogsMethod.Log.Add(String Title, String Contents, Types types, String oldXML, String newXML) in F:\Project\Uwl.WebApi\I_Sfront.WebApi\Uwl.Common\LogsMethod\Log.cs:line 60
at Iview.WebApi.Controllers.AuthorizeController.Token(LoginViewModel loginViewModel) in F:\Project\Uwl.WebApi\I_Sfront.WebApi\Iview.WebApi\Controllers\AuthorizeController.cs:line 62

九两白菜粥的主页 九两白菜粥 | 初学一级 | 园豆:122
提问于:2018-11-23 16:09
< >
分享
最佳答案
0

委托调用建议使用Func<>这个方法,更清晰呦

收获园豆:10
chester·chen | 小虾三级 |园豆:507 | 2018-11-23 17:42

大佬 可以给写一个demo嘛?刚毕业的学生呢!不太会用,

九两白菜粥 | 园豆:122 (初学一级) | 2018-11-23 17:44

@梦梦宝宝: 你直接调用add方法不行吗?干啥还要加委托?func的用法百度一下就行,我给你找了一个https://www.cnblogs.com/LipeiNet/p/4694225.html

chester·chen | 园豆:507 (小虾三级) | 2018-11-23 17:48

@梦梦宝宝:

    public static void Add(Logs logs)
    {
        dgWriteLog writeLog = new dgWriteLog(add);
        writeLog.Invoke(logs);

    }

改成这样就可以了

chester·chen | 园豆:507 (小虾三级) | 2018-11-23 17:58

@老六代码: 好的 蟹蟹 大佬

九两白菜粥 | 园豆:122 (初学一级) | 2018-11-23 18:00

@梦梦宝宝: 采纳一下哈

chester·chen | 园豆:507 (小虾三级) | 2018-11-23 18:01

@老六代码: 嗯嗯 好的

九两白菜粥 | 园豆:122 (初学一级) | 2018-11-23 18:01

@梦梦宝宝: 大佬 你帮忙看下 我知道那个问题为什么报出来了,注入没有成功

九两白菜粥 | 园豆:122 (初学一级) | 2018-11-23 18:07

@梦梦宝宝:
你有在services里添加注入吗?

chester·chen | 园豆:507 (小虾三级) | 2018-11-23 18:10

@老六代码: 有注入的

九两白菜粥 | 园豆:122 (初学一级) | 2018-11-23 18:13

@梦梦宝宝:

代码贴出来

chester·chen | 园豆:507 (小虾三级) | 2018-11-23 18:13

@老六代码: 18790997531方便加个微信吗?我晚上回到家了给你截图过去

九两白菜粥 | 园豆:122 (初学一级) | 2018-11-23 18:15

@梦梦宝宝: 注入的代码贴出来

chester·chen | 园豆:507 (小虾三级) | 2018-11-23 18:15

@老六代码: 好的,老哥,我晚上回去截图给你贴出来

九两白菜粥 | 园豆:122 (初学一级) | 2018-11-23 18:18

@梦梦宝宝:

嗯,不过还是提示你一下,跟你你的代码,注入应该是这样的

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddTransient<Log>();
        services.AddTransient<ILogsServer,LogsServer>();
        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
    }

还有,既然你用了依赖注入,Log类中就不该有静态方法,静态变量了,你应该把Log,注入到控制器中,像这样:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;

namespace ServerProvider.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
private Log _log;
public ValuesController(Log log)
{
_log = log;
}
// GET api/values
[HttpGet]
public ActionResult<IEnumerable<string>> Get()
{
_log.Add(new Logs { Id = "123", Contents = "123" });
return new string[] { "sucess" };
}

}
public class Log
{
    private delegate void dgWriteLog(Logs logs);
    public  void add(Logs logs)
    {
        //_LogsServer.Inserver(logs);
    }
    public  void Add(Logs logs)
    {
        dgWriteLog writeLog = new dgWriteLog(add);
        writeLog.Invoke(logs);
    }
}


public class Logs
{
    public string Id { get; set; }
    public string Contents { get; set; }
}

}

chester·chen | 园豆:507 (小虾三级) | 2018-11-23 18:21

@梦梦宝宝: 依赖注入的思想就是,哪儿用就在哪儿注入

chester·chen | 园豆:507 (小虾三级) | 2018-11-23 18:22
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册