小编tri*_*ris的帖子

Func <T>()vs Func <T> .Invoke()

我很好奇直接调用Func和使用Invoke()之间的区别.有区别吗?是第一个,语法糖,并在下面调用Invoke()?

public T DoWork<T>(Func<T> method)
{
    return (T)method.Invoke();
}
Run Code Online (Sandbox Code Playgroud)

VS

public T DoWork<T>(Func<T> method)
{
    return (T)method();
}
Run Code Online (Sandbox Code Playgroud)

或者我完全走错了轨道:)谢谢.

c# invoke func

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

AngularJS + Base Href区分大小写?

我很难理解为什么我的基础href似乎区分大小写.我有一个带有基本href的页面,并使用angularjs路由.

HTML:

<html ng-app="app">
    <head>
        <base href="/Foo/"/>
    </head>
    <body>
        <div>Foo</div>
        <div ng-view></div>  
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

JS:

var module = angular.module('app', []);

module.config(function ($routeProvider, $locationProvider) {
    $routeProvider
        .when('/Home/Page1', { templateUrl = 'partials/page1' })
        .otherwise({ redirectTo: '' });

     $locationProvider.html5Mode(true);
     $locationProvider.hashPrefix('!');
});
Run Code Online (Sandbox Code Playgroud)

如果我导航到http://www.example.com/Foo/,那很好.但是当我导航到http://www.example.com/foo/时,我收到一个角度误差:

Error: Invalid url "http://www.example.com/foo/", missing path prefix "/Foo" !
at Error (<anonymous>)
at Object.LocationUrl.$$parse (http://www.example.com/foo/Scripts/angular.js:4983:13)
at Object.LocationUrl (http://www.example.com/foo/Scripts/angular.js:5014:8)
at $LocationProvider.$get (http://www.example.com/foo/Scripts/angular.js:5387:21)
at Object.invoke (http://www.example.com/foo/Scripts/angular.js:2809:28)
at http://www.example.com/foo/Scripts/angular.js:2647:37
at getService (http://www.example.com/foo/Scripts/angular.js:2769:39)
at Object.invoke (http://www.example.com/foo/Scripts/angular.js:2787:13)
at $CompileProvider.directive (http://www.example.com/foo/Scripts/angular.js:3613:43)
at Array.forEach …
Run Code Online (Sandbox Code Playgroud)

routing base href angularjs

11
推荐指数
3
解决办法
7663
查看次数

生产代码中的简单注入器验证()

调用container.Verify()生产代码是最佳做法吗?我想要搬到:

#IF Debug
    container.Verify();
#ENDIF
Run Code Online (Sandbox Code Playgroud)

我没有任何真正的理由做出改变,只是好奇一般共识/最佳实践是什么.

c# simple-injector

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

WebAPI 2.0发布和删除路由

我在同一个控制器上有两个动作,具有相同的路径,但有不同的HttpMethod要求(POSTvs DELETE).

[AllowAnonymous]
public class TestController : ApiController
{
    [Route("~/api/test")]
    [HttpDelete]
    public IHttpActionResult Endpoint1()
    {
        return this.Ok("endpoint1");
    }

    [Route("~/api/test")]
    [HttpPost]
    public IHttpActionResult Endpoint2()
    {
        return this.Ok("endpoint2");
    }
}
Run Code Online (Sandbox Code Playgroud)

这一切都很好 - 两个端点在切换时都DELETE有效POST.

例如

DELETE /api/test = endpoint1
POST /api/test = endpoint2
Run Code Online (Sandbox Code Playgroud)

如果我将操作分成单独的控制器,它就不再起作用了:

[AllowAnonymous]
public class TestController : ApiController
{
    [Route("~/api/test")]
    [HttpDelete]
    public IHttpActionResult Endpoint1()
    {
        return this.Ok("endpoint1");
    }
}

[AllowAnonymous]
public class TestController2 : ApiController
{
    [Route("~/api/test")]
    [HttpPost]
    public IHttpActionResult Endpoint2()
    { …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-web-api asp.net-web-api-routing

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

WebApi 2.0路由与查询参数不匹配?

我刚刚从AttributeRouting切换到WebApi 2.0 AttributeRouting,并且有一个控制器和动作定义如下:

public class InvitesController : ApiController
{
    [Route("~/api/invites/{email}")]
    [HttpGet]
    [ResponseType(typeof(string))]
    public IHttpActionResult InviteByEmail(string email)
    {
        return this.Ok(string.Empty);
    }
}
Run Code Online (Sandbox Code Playgroud)

示例查询:

GET: http://localhost/api/invites/test@foo.com
Run Code Online (Sandbox Code Playgroud)

我收到的响应是200,内容为空(由于string.Empty).


这一切都很好 - 但我想将email属性更改为查询参数.所以我将控制器更新为:

public class InvitesController : ApiController
{
    [Route("~/api/invites")]
    [HttpGet]
    [ResponseType(typeof(string))]
    public IHttpActionResult InviteByEmail(string email)
    {
        return this.Ok(string.Empty);
    }
}
Run Code Online (Sandbox Code Playgroud)

但现在在查询端点时:

GET: http://localhost/api/invites?email=test@foo.com
Run Code Online (Sandbox Code Playgroud)

我收到的回复是404:

{
"message": "No HTTP resource was found that matches the request URI 'http://localhost/api/invites?email=test@foo.com'.",
"messageDetail": "No route providing a controller name was found to match request URI 'http://localhost/api/invites?email=test@foo.com'"
}
Run Code Online (Sandbox Code Playgroud)

有人知道为什么它与参数交换到查询参数时的路由不匹配,而不是内联网址?


根据要求,WebApiConfig的定义如下: …

c# asp.net-web-api asp.net-web-api-routing

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

SimpleInjector RegisterWebApiRequest vs RegisterPerWebRequest

最新版本的SimpleInjector引入了MVC和WebApi之间的区别.两个请求注册是否为同一个东西的别名?或者是否存在潜在的差异?

谢谢

dependency-injection simple-injector asp.net-web-api

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

refs / pull / * / head-&gt; origin / pr / *出现在git pull上

自大约一个月前以来,每次我发布“ git pull”时,我最终在“ git branch -a”上出现一堆remotes / origin / pr / *分支,这些分支直接映射到具有曾经在此仓库中打开过。进行“ git remote prune origin”会清除它们。

拉前:

C:\experimental [develop]> git branch -a
* develop
  feature/291
  master
  remotes/origin/HEAD -> origin/master
Run Code Online (Sandbox Code Playgroud)

拉 :

C:\experimental [develop]> git pull
From https://github.com/.../experimental
 * [new ref]         refs/pull/1/head -> origin/pr/1
 * [new ref]         refs/pull/10/head -> origin/pr/10
 * [new ref]         refs/pull/100/head -> origin/pr/100
 * [new ref]         refs/pull/101/head -> origin/pr/101
 * [new ref]         refs/pull/102/head -> origin/pr/102
 * [new ref]         refs/pull/103/head -> origin/pr/103
...
 * [new ref]         refs/pull/103/head -> origin/pr/382 …
Run Code Online (Sandbox Code Playgroud)

git github

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

TransactionScope 与 MySQL 和分布式事务

我有一个 ASP.Net WebAPI 实例设置,它使用 MySQL 数据库进行存储。我编写了一个用于在单个端点请求的生命周期内ActionFilter处理创建 a 的方法。TransactionScope

public async Task<HttpResponseMessage> ExecuteActionFilterAsync(
    HttpActionContext actionContext,
    CancellationToken cancellationToken,
    Func<Task<HttpResponseMessage>> continuation)
{
    var transactionScopeOptions = new TransactionOptions { IsolationLevel = IsolationLevel.ReadUncommitted };
    using (var transaction = new TransactionScope(TransactionScopeOption.RequiresNew, transactionScopeOptions, TransactionScopeAsyncFlowOption.Enabled))
    {
        var handledTask = await continuation();

        transaction.Complete();

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

然后,在整个端点中,我有不同的查询/命令,使用autoenlist=true的功能打开/关闭连接DbConnection。一个示例端点可以是:

public async Task<IHttpActionResult> CreateStuffAsync()
{
    var query = this.queryService.RetrieveAsync();

    // logic to do stuff

    var update = this.updateService.Update(query);

    return this.Ok();
}
Run Code Online (Sandbox Code Playgroud)

我不会创建一个DbConnection并从顶部传递它,因为这是一个简单的示例,在实践中传递服务之间的连接需要进行大量重构(尽管如果有必要,可以这样做)。我还读到,最好根据需要打开/关闭连接(即保持它们打开的时间尽可能短)。和 …

c# mysql transactions transactionscope mysql-connector

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