当激活以下服务时,我得到以下异常(在相同的合同中不能有两个操作,具有相同的名称,方法ExecuteAsync和Execute).
[ServiceContract]
public interface IMyService
{
[OperationContract]
byte[] Execute(MyRequest request);
[OperationContract]
Task<byte[]> ExecuteAsync(MyRequest request);
}
Run Code Online (Sandbox Code Playgroud)
我想如果您使用svcutil.exe来创建服务引用,这是有道理的,因为基于任务的操作是自动为您创建的.但是,我不想添加服务引用,而只是使用标准ChannelFactory来创建WCF通道.如果不将异步方法重命名为其他方法,还有其他方法吗?或者我必须在Task.Run中将sync方法包装在客户端上吗?
我目前正在使用 Microsoft Fakes 框架编写单元测试。我注意到,如果我不为给定的属性提供委托,则会返回默认值。但是,在初始化我的 shim 时,我将此行为更改为 NotImplemented。
var myShim = new ShimMyClass
{
InstanceBehavior = ShimBehaviors.NotImplemented
}
Run Code Online (Sandbox Code Playgroud)
这是我定义的所有垫片都想要的给定行为。我希望能够在更全局的级别设置此默认值,而不必记住为我创建的每个垫片都这样做。这可能吗?
我正在写一个装饰器来实现缓存.该对象将由我的DI容器注册为单例.因为我知道我将对象注册为单例,所以表示我的缓存的字段不是静态的.我不确定这是否是最佳做法,但我试图不惜一切代价避免锁定.我的缓存是懒惰的初始化,只有一次昂贵/运行.我的问题是我需要担心运行缓存初始化逻辑的多个线程吗?我的直觉告诉我"是的,我确实需要担心",但我听到其他开发人员说"如果它不是静止的话就没有锁定点".
//SimpleInjector DI Container configuration
public static class Bootstrapper
{
public static void ConfigureContainer(Container container)
{
container.Register<IQueryHandler<GetFoos, Foo[]>, GetFoosHandler>(Lifestyle.Singleton);
container.RegisterDecorator<IQueryHandler<GetFoos, Foo[]>, GetFoosCachingHandler>(Lifestyle.Singleton);
}
}
public class Foo
{
public int Id;
public string FooTypeCode;
public string Name;
}
public class GetFoos : IQuery<Foo[]>
{
public string FooTypeCode;
}
public class GetFoosCachingHandler : IQueryHandler<GetFoos, Foo[]>
{
private Lazy<Dictionary<string, Foo[]>> _cache;
private readonly IQueryHandler<GetFoos, Foo[]> _queryHandler;
public GetFoosCachingHandler(IQueryHandler<GetFoos, Foo[]> queryHandler)
{
_queryHandler = queryHandler;
_cache = new Lazy<Dictionary<string, Foo[]>>(() =>
{
//expensive …Run Code Online (Sandbox Code Playgroud)