小编Mar*_*kus的帖子

为什么Enum.Parse会创建未定义的条目?

class Program
{
    static void Main(string[] args)
    {
        string value = "12345";
        Type enumType = typeof(Fruits);
        Fruits fruit = Fruits.Apple;
        try
        {
            fruit = (Fruits) Enum.Parse(enumType, value);
        }
        catch (ArgumentException)
        {
            Console.WriteLine(String.Format("{0} is no healthy food.", value));
        }
        Console.WriteLine(String.Format("You should eat at least one {0} per day.", fruit));
        Console.ReadKey();
    }

    public enum Fruits
    {
        Apple,
        Banana,
        Orange
    }
}
Run Code Online (Sandbox Code Playgroud)

如果您执行上面的代码,结果显示:

你应该每天至少吃一个12345.

我真的希望在传递未知名称(字符串)时抛出ArgumentException.仔细查看Enum.Parse定义可以看出:

摘要:
将一个或多个枚举常量的名称或数值的字符串表示形式转换为等效的枚举对象.

异常:
ArgumentException:enumType不是枚举. - 或 - 值是空字符串或仅包含空格.- 或 - value是一个名称,但不是为枚举定义的命名常量之一.

即,如果传递整数的字符串表示,则创建新的枚举值,并且现在设计引发异常.这有意义吗?

至少我现在知道在Enum.IsDefined(enumType, …

.net c# enums behavior

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

如何编写一个需要请求和响应Dtos的ServiceStack插件

我需要服务本地化数据.本地化的所有响应Dtos共享相同的属性.即我定义了一个接口(ILocalizedDto)来标记那些Dtos.在请求方面,有一个ILocalizedRequest要求本地化的请求.

使用IPlugin我已经设法实现所需的功能.但是我很确定实现不是线程安全的,另外我不知道我是否可以使用IHttpRequest.GetHashCode()作为一个请求/响应周期的标识符.

实现一个使用请求和响应Dto的ServiceStack插件的正确方法是什么?即是否存在一些IHttpRequest.Context来存储数据或者是否可以在响应时获取请求?

internal class LocalizationFeature : IPlugin
{
    public static bool Enabled { private set; get; }

    /// <summary>
    ///     Activate the localization mechanism, so every response Dto which is a <see cref="ILocalizedDto" />
    ///     will be translated.
    /// </summary>
    /// <param name="appHost">The app host</param>
    public void Register(IAppHost appHost)
    {
        if (Enabled)
        {
            return;
        }
        Enabled = true;
        var filter = new LocalizationFilter();
        appHost.RequestFilters.Add(filter.RequestFilter);
        appHost.ResponseFilters.Add(filter.ResponseFilter);
    }
}

// My request/response filter
public class …
Run Code Online (Sandbox Code Playgroud)

c# servicestack

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

我应该将哪个ORM与ServiceStack和现有数据库一起使用

我目前正在开发一个Web服务,它提供对业务对象的基本CRUD操作.该服务将由当前使用直接数据库访问的遗留应用程序使用.

由于ServiceStacks的优秀架构,我决定使用ServiceStack而不是WCF.

但是知道我正在尝试使用OrmLite,nHibernate或Entity Framework来访问现有的遗留数据库.

ORM的要求如下

  • 支持连接
  • 支持存储过程

我已经尝试过OrmLite(因为它很快并且已经包含在ServiceStack中).我设法加入两个表的唯一方法是使用SQL(不是一个选项).有没有更好的方法?

// @stackoverflow: This is my POCO DTO
public class Country
{
    public long Id { get; set; }

    public string Alpha2 { get; set; }

    public string Alpha3 { get; set; }

    public string ShortText { get; set; }

    public string LongText { get; set; }
}

public class CountryRepository : ICountryRepository
{
    // @stackoverflow: This is the query to join countries with translated names stored in another table 
    private const string …
Run Code Online (Sandbox Code Playgroud)

c# nhibernate entity-framework servicestack ormlite-servicestack

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

为什么 MemoryCache 中的滑动过期行为如此奇怪?

我不明白在 System.Runtime.Caching.MemoryCache 和 .NET 4.0 中滑动过期应该如何工作。

根据文档,到期时间跨度是“在缓存条目从缓存中逐出之前必须访问缓存条目的时间跨度”。

但是,以下单元测试失败:

private const string AnyKey = "key";
private const string AnyValue = "value";

private readonly TimeSpan timeout = TimeSpan.FromSeconds(0.5);

private void WaitSixtyPercentOfTheTimeout()
{
    Thread.Sleep(TimeSpan.FromSeconds(timeout.TotalSeconds*0.6));
}

[Test]
public void Get_RefreshesTimeoutOfSlidingExpiration()
{
    var cache = MemoryCache.Default;
    cache.Set(AnyKey, AnyValue, new CacheItemPolicy {SlidingExpiration = timeout});

    WaitSixtyPercentOfTheTimeout();
    cache[AnyKey].Should().Be(AnyValue);
    WaitSixtyPercentOfTheTimeout();

    cache[AnyKey].Should().Be(AnyValue);
}

private void UpdateCallback(CacheEntryUpdateArguments arguments)
{
}
Run Code Online (Sandbox Code Playgroud)

巧合的是,我做了一个小改动来解决这个问题。但是,如果这是一个错误或功能,现在有人吗?

一旦设置了 UpdateCallBack,到期将按预期工作:

// [...]

[Test]
public void Get_RefreshesTimeoutOfSlidingExpiration()
{
    var cache = MemoryCache.Default;
    cache.Set(AnyKey, AnyValue, new CacheItemPolicy {SlidingExpiration = timeout, …
Run Code Online (Sandbox Code Playgroud)

.net c# caching memorycache

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