小编McG*_*V10的帖子

T4使用DTE打开生成的文件时,Visual Studio序列化错误

我们有一个名为GenerateProxies.tt的C#T4文件,该文件调用了几个命令行代码生成实用程序。使用System.Diagnostics Process类,我们将标准输出重定向到T4输出文本文件(GenerateProxies.txt),以便我们可以查看命令行输出中的错误。

我在T4的末尾添加了以下简单代码,以便Visual Studio将打开生成的文本文件作为该过程的最后一步(该workingDirectory变量在模板的前面进行了声明和填充)。确实可以,但是会引发序列化错误。可以避免此错误吗?

<#@ assembly name="EnvDTE" #>
<#@ import namespace="EnvDTE" #>
<#
    IServiceProvider vssp = (IServiceProvider)this.Host;
    DTE dte = vssp.GetService(typeof(DTE)) as DTE;
    dte.ItemOperations.OpenFile(
        string.Format(@"{0}\GenerateProxies.txt", workingDirectory),
        Constants.vsViewKindTextView
    );
#>
Run Code Online (Sandbox Code Playgroud)

再次,这确实起作用,它打开了文本文件,但是会产生此错误:

Running transformation: System.Runtime.Serialization.SerializationException:
Type 'Microsoft.VisualStudio.Platform.WindowManagement.DTE.WindowBase' in
Assembly 'Microsoft.VisualStudio.Platform.WindowManagement'
is not marked as serializable.
Run Code Online (Sandbox Code Playgroud)

c# t4 envdte visual-studio-2015

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

如何使路由只能从本地主机访问?

我有一条这样的路线:

[Route("api/elasticsearch/resync/products")]
[HttpGet]
public async Task<string> ResyncProducts()
{
}
Run Code Online (Sandbox Code Playgroud)

如何使它只能从本地主机访问?

c# asp.net-core

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

简单的 ASP.NET Core 中间件项目中的“响应已开始”异常

我的问题基本上是这样的 - 鉴于此处显示的代码,管道中还运行什么来尝试启动对客户端的响应?我知道有关该异常的其他问题,但在我的中间件导致异常之后运行的管道中似乎有一些东西引起了异常——它不是由我的中间件引起的,我认为这是我的场景中的差异。

这是一个简单的 ASP.NET Core 3.0 WebSocket 回显服务器——没有 SignalR、没有 MVC、没有路由、没有静态页面支持等。除了处理套接字之外,当中间件看到一个请求时,它会发回一个简单的text/html请求page(硬编码字符串)作为回显客户端。

The browser receives the content just fine, and my exception handler is not triggered (a crucial point), but after my middleware is done processing the request, ASP.NET Core logs the exception:

StatusCode cannot be set because the response has already started.

The code is quite minimal:

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddTransient<WebSocketMiddleware>();
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        var …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-core asp.net-core-middleware

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

ASP.NET Core 2.0:找不到静态文件(site.min.js)

我想通过在Visual Studio 2017中发布配置文件来部署ASP.NET Core Web应用程序.一切都被复制到目标文件夹并且工作正常,但"site.js"文件除外.在目标文件夹中,我在wwwroot/js文件夹中看到site.js和site.min.js,但后者为空.我在浏览器中也遇到控制台错误:

Failed to load resource: the server responded with a status of 404 (Not Found) site.min.js

在页面源中,我看到文件正确加载:

<script src="/wwwroot/js/site.min.js"></script>

Program.cs文件:

public class Program
{
    public static void Main(string[] args)
    {
        var host = new WebHostBuilder()
             .UseKestrel()
             .UseContentRoot(Directory.GetCurrentDirectory())
             .UseIISIntegration()
             .UseStartup<Startup>()
             .Build();

        host.Run();
    }
}
Run Code Online (Sandbox Code Playgroud)

Startup.cs中的服务配置:

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    app.UseStaticFiles();

    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
        app.UseBrowserLink();
    }
    else
    {
        app.UseExceptionHandler("/Home/Error");
    }

    app.UseMvc(routes =>
    {
        routes.MapRoute(
            name: "default",
            template: …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-core-mvc asp.net-core-2.0

3
推荐指数
2
解决办法
7147
查看次数

实体框架4.1在SaveChanges之后不加载关系?

我是EF的新手.在调用SaveChanges后,似乎无法获得相关的表数据.

假设我有一个包含以下表格的数据库:User,Feature和FeatureUser.FeatureUser将用户和功能记录绑定在一起.我有一个接受Feature对象的方法,它使用基于FeatureUser表的User.name值填充列表.这很好用:

using (var db = new projectModel())
{
  feature f = db.features.First();
  populateList(f);
}

private void populateList(feature f)
{
  foreach(featureuser fu in f.featureusers)
    addToList(fu.user.name);
}
Run Code Online (Sandbox Code Playgroud)

上面代码中的重点是,我通过FeatureUser表的内置链接返回到User.name值,返回到User表.什么是行不通的是创建新的FeatureUser记录,然后保存它们,然后尝试遵循相同的过程:

using (var db = new projectModel())
{
  feature f = db.features.First();
  foreach(int newuserid in somearray)
  {
    featureuser nfu = new featureuser();
    nfu.featureid = f.id;
    nfu.userid = newuserid;
    f.featureusers.add(nfu);
  }
  db.SaveChanges();
  populateList(f); // null reference error on fu.user.name
}
Run Code Online (Sandbox Code Playgroud)

有没有办法在我建立并保存创建关系所需的外键后告诉EF解决所有这些方便的内置关系集合?

entity-framework entity-framework-4.1

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

无法使用.net core 2.0中创建的用户登录

我正在使用.net core 2.0全新项目。在使用身份构建我的角色时,我还创建了一个用户。要创建用户,我有以下代码。

var roleManager = serviceProvider.GetRequiredService<RoleManager<IdentityRole>>();
        var userManager = serviceProvider.GetRequiredService<UserManager<ApplicationUser>>();
        string[] roleNames = { "SuperAdmin", "Admin", "Support", "User" };

        IdentityResult roleResult;

        foreach (var roleName in roleNames)
        {
            var roleExist = await roleManager.RoleExistsAsync(roleName);
            if (!roleExist)
            {
                //create the roles and seed them to the database: Question 1
                roleResult = await roleManager.CreateAsync(new IdentityRole(roleName));
            }
        }

        // administrator
        var user = new ApplicationUser
        {
            UserName = "Administrator",
            Email = "something@something.com",
            EmailConfirmed = true
        };
        var i = await userManager.FindByEmailAsync(user.Email);
        if (i == …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-core

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

创建从另一个控制器到控制器的 url 链接

我在 ASP.NET Core 2.0 中开发了一个 WebApi RESTful。从控制器中的动作方法 AI 想要为位于不同控制器 B 中的动作方法创建 URL 地址:

[Route("api/applications/{idbcon}/bcontrollers")]
public class BController : Controller{
    [HttpGet({idbcon}), Name = "ActionB"]
    public IActionResult ActionB(int idbcon, [FromHeader(Name = "Accept")] string mediatype){
        return OK();
    }
}
Run Code Online (Sandbox Code Playgroud)
[Route("api/applications/{idapp}/bcontrollers/{idbcon}/acontrollers")]
public class AController: Controller{
    [HttpGet(), Name = "GetURLBController"]
    public IActionResult GetURLBController()
    {
        var url = /* Here I would like to get the link to "ActionB" that belong to a Controller "BController" */;
        return Ok(url);
    }
}
Run Code Online (Sandbox Code Playgroud)

有什么建议吗???

c# asp.net-core

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