小编Tse*_*eng的帖子

在Android中如何断开套接字?

所以我有一个连接到外部Web地址的套接字,当它收到某个消息时,它应该断开连接.我试着打电话,socket.close()socket.isConnected()仍然是真的.没有运气寻找答案

sockets android disconnect

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

ASP.Net Core(MVC6)存储库模式意外处置

当我尝试添加评论时,出现以下错误:

ObjectDisposedException:无法访问已处置的对象.

当代码运行第二行时:

m_context.Comments.Add(comment);
m_context.SaveChanges();
Run Code Online (Sandbox Code Playgroud)

为什么要处理上下文?如果将TryAddComment方法移动到控制器中,它不会提前调用Dispose.

这是我的Controller和Repository类的样子(简化).

CommentsController.cs:

public class CommentsController : Controller
{

    private ICommentRepository m_commentRepository;

    public CommentsController(ICommentRepository commentRepository)
    {
        m_commentRepository = commentRepository;
    }

    // POST: api/Comments
    [HttpPost]
    public async Task<IActionResult> PostComment([FromBody] CommentAddViewModel commentVM)
    {
        Comment comment = new Comment
        {
            ApplicationUserId = User.GetUserId(),
            PostId = commentVM.PostId,
            Text = commentVM.Text
        };

        bool didAdd = m_commentRepository.TryAddComment(comment);

        if (!didAdd)
        {
            return new HttpStatusCodeResult(StatusCodes.Status409Conflict);
        }

        return CreatedAtRoute("GetComment", new { id = comment.CommentId }, comment);
    }

}
Run Code Online (Sandbox Code Playgroud)

CommentRepository.cs:

public class CommentRepository : ICommentRepository, …
Run Code Online (Sandbox Code Playgroud)

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

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

在.NET Core中使用IOptions进行集成测试

我传给IOption<T>了我,CommandBus所以我可以从ServiceBusSetting班上得到设置.我想对我的总线进行集成测试.我不想解决它只是使用new QueueCommandBus,需要传递IOptions给它.

var services = new ServiceCollection().AddOptions();
        services.Configure<ServiceBusAppSettings>(Configuration.GetSection("ServiceBus"));
        var options = services.BuildServiceProvider().GetService<IOptions<ServiceBusAppSettings>>();

        ////Act
        var commandBus = new QueueCommandBus(options);
Run Code Online (Sandbox Code Playgroud)

这工作正常,但感觉非常复杂的代码IOptions<T>从我appsetting.json的测试项目中获取.

任何线索,如果这是唯一的方式或有更好的方法?

asp.net-mvc integration-testing asp.net-core

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

在Identity 3中创建声明标识

Visual Studio 2015脚手架使用UserManager<TUser>不能用于创建的脚手架ClaimsIdentity.有没有人有一个如何做到这一点的工作示例?

VS2015脚手架抛出错误:

public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
    // Note the authenticationType must match the one 
    // defined in CookieAuthenticationOptions.AuthenticationType
    var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);

    // Add custom user claims here
    return userIdentity;
}
Run Code Online (Sandbox Code Playgroud)

注意:我添加了ApplicationUser与之不冲突的属性IdentyUser.

razor entity-framework-6 asp.net-identity-3 asp.net-core

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

部署asp.net核心应用程序时如何处理环境差异?

有没有办法在部署ASP.NET Core应用程序时更改环境设置(比如使用调试/发布版本进行配置文件转换)?

在.NET Core应用程序中维护多个环境设置的最佳方法是什么(类似于<appSettings file="local.config">本地,登台和生产)?

asp.net-core-mvc asp.net-core asp.net-core-1.0

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

如何在ASP.NET Core中以不同方式处理错误(或区分)API调用和MVC(视图)调用

在我基于普通MVC和WebApi的应用程序中,我有两种不同的错误处理路径.

如果在WebApi调用期间发生错误,我会拦截它(使用标准的web api选项)并返回带有相应HTTP状态代码的json消息,以便客户端应用程序可以处理它.

如果错误发生在MVC中,那么我会使用一些标准处理程序,可能会根据状态代码将用户重定向到某个默认错误屏幕.

现在在ASP.NET Core中,两者都加入了相同的框架,所以如果我只是拦截并返回JSON,那么我冒险向用户显示json,因为它可以是一个返回视图的动作.另一方面,如果我使用app.UseExceptionHandler那么我的API调用将从错误页面获取HTML,这在js中是不可用的.

为这两种情况提供单独的错误处理的好方法是什么?或者也许有更好的方法来处理它?

PS我宁愿重用开箱即用的MVC异常处理程序,只添加web api部分.

error-handling asp.net-web-api asp.net-core-mvc asp.net-core

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

如果不指定目标框架,则不支持"发布"目标

使用Visual Studio 2017,我使用.NET Framework创建了一个ASP.NET Core站点.(我没有project.json,我有一个VS2017项目.cproj)

我的目标是x64 Windows 2008R2.我.cproj看起来像下面的开头:

<Project Sdk="Microsoft.NET.Sdk.Web" ToolsVersion="15.0">
  <PropertyGroup>
    <TargetFrameworks>net462</TargetFrameworks>
    <RuntimeIdentifier>win7-x64</RuntimeIdentifier>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
    <PlatformTarget>x64</PlatformTarget>
    <OutputPath>bin\Debug</OutputPath>
    <DefineConstants>TRACE;DEBUG</DefineConstants>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
    <PlatformTarget>x64</PlatformTarget>
    <OutputPath>bin\Release</OutputPath>
    <Optimize>True</Optimize>
  </PropertyGroup>
  ...
Run Code Online (Sandbox Code Playgroud)

但是,虽然我只针对.NET 4.6.2,但当我尝试发布时,我收到此错误

C:\ Program Files(x86)\ Microsoft Visual Studio\2017\Enterprise\MSBuild\Sdks\Microsoft.NET.Sdk\buildCrossTargeting\Microsoft.NET.Sdk.targets(31,5):错误:'发布'目标是不指定目标框架就不受支持.当前项目针对多个框架,请指定已发布应用程序的框架.

在网上寻找解决方案的同时,我遇到了遇到同样问题的人,但他们确实有很多目标,但是,在我的情况下,我不确定为什么我会得到它.

有任何想法吗?

msbuild publish asp.net-core .net-4.6.2 visual-studio-2017

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

检查操作筛选器中的属性

在MVC 5中,您可以在其中执行类似的操作IActionFilter,以检查是否已在当前操作(或控制器范围)上声明属性

public void OnActionExecuting(ActionExecutingContext filterContext)
{
    // Stolen from System.Web.Mvc.AuthorizeAttribute
    var isAttributeDefined = filterContext.ActionDescriptor.IsDefined(typeof(CustomAttribute), true) ||
                             filterContext.ActionDescriptor.ControllerDescriptor.IsDefined(typeof(CustomAttribute), true);

}
Run Code Online (Sandbox Code Playgroud)

因此,如果你的控制器像这样定义属性,这是有效的.

[CustomAttribute]
public ActionResult Everything()
{ .. }
Run Code Online (Sandbox Code Playgroud)

是否可以在ASP.NET Core MVC(内部IActionFiler)中执行相同的操作?

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

9
推荐指数
2
解决办法
2248
查看次数

找不到HttpContextBase名称空间

   public string GetCartId(HttpContextBase context)
    {
        if (context.Session[CartSessionKey] == null)
        {
            if (!string.IsNullOrWhiteSpace(context.User.Identity.Name))
            {
                context.Session[CartSessionKey] =
                    context.User.Identity.Name;
            }
            else
            {
                // Generate a new random GUID using System.Guid class
                Guid tempCartId = Guid.NewGuid();
                // Send tempCartId back to client as a cookie
                context.Session[CartSessionKey] = tempCartId.ToString();
            }
        }
        return context.Session[CartSessionKey].ToString();
Run Code Online (Sandbox Code Playgroud)

有关在asp.net核心中使用HttpContextBase的任何帮助吗?上面是我的示例代码我正在努力创建一个购物车.

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

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

Asp.net Core CORS适用于GET,但不适用于POST

我正在使用带有Asp核心web api的Angular 4应用程序,我在locahost上测试了不同的端口.我的WebApi需要Windows身份验证(需要获取登录用户名).所有使用GET的调用工作,但不幸的是,我无法让POST工作.我有CORS的WebApi设置:

        public void ConfigureServices(IServiceCollection services)
    {
        // Add framework services.
        services.AddApplicationInsightsTelemetry(Configuration);
        services.AddDbContext<CatalogContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
        services.AddCors(options =>
        {
            options.AddPolicy("AllowAll", builder =>
            {
                builder.AllowAnyOrigin()
                    .AllowAnyHeader()
                    .AllowAnyMethod()
                    .AllowCredentials();
            });

        });
        services.AddMvc();
    }
Run Code Online (Sandbox Code Playgroud)

而angular 4客户端正试图发布文件

fileChange(event) {
let fileList: FileList = event.target.files;
if (fileList.length > 0) {
  let file: File = fileList[0];
  let formData: FormData = new FormData();
  formData.append('uploadFile', file, file.name);
  let headers = new Headers();
  /** No need to include Content-Type in Angular 4 */
  headers.append('Content-Type', 'multipart/form-data');
  headers.append('Accept', 'application/json');

  let options …
Run Code Online (Sandbox Code Playgroud)

cors asp.net-core-mvc asp.net-core

9
推荐指数
2
解决办法
3789
查看次数