我正在编写一些.NET 2.0 ASMX服务.我有一个场景,我需要使用WSDL.exe生成一个Web服务代理,但生成的类没有实现一个接口,所以我不能伪造Web服务进行测试.
这是我正在尝试的一种方法,但目前还没有编译:我知道WSDL.exe可以使用该/serverInterface选项生成接口.原则上,我想产生两种接口和代理类,如下所示:
wsdl MyWebService.wsdl/nologo/l:CS /namespace:Juliet.Services /o:C:\projects\JulietService\MyWebServiceInterface.cs/serverInterface
wsdl MyWebService.wsdl/nologo/l:CS /namespace:Juliet.Services /o:C:\projects\JulietService\MyWebServiceProxy.cs
然后在我的项目中包含这两个文件.之后我应该能够从生成的Proxy派生自己的类,并按如下方式实现生成的接口:
public class MockableWebService : MyWebService, IMyWebServiceSoap11Binding
{
// No implementation, the interface is already implicitly implemented
// from the methods in the base class.
}
Run Code Online (Sandbox Code Playgroud)
这应该原则上起作用,除了接口和代理文件将自动生成我的请求/响应消息的相同类定义,导致数百种类型的错误The namespace 'Juliet.Services' already contains a definition for 'ABC'.
我想保持接口/代理生成尽可能自动化 - 这意味着,我想避免不惜一切代价手动修改生成的代码.
任何人都可以建议使用WSDL.exe同时生成接口和代理的方法,或者更好的方法来获得上述结果?
最近我一直在重构我的一些C#代码,我发现了一些双重检查锁定实践.那时我不知道这是一个不好的做法,我真的想摆脱它.
问题是我有一个应该被懒惰地初始化并且经常被许多线程访问的类.我也不想将初始化移动到静态初始化器,因为我计划使用弱引用来保持初始化对象在内存中保持太长时间.但是,如果需要,我想"恢复"对象,确保以线程安全的方式发生这种情况.
我想知道是否在C#中使用ReaderWriterLockSlim并在第一次检查之前输入UpgradeableReadLock,然后如果需要,为初始化输入写锁定将是可接受的解决方案.以下是我的想法:
public class LazyInitialized
{
private readonly ReaderWriterLockSlim _lock = new ReaderWriterLockSlim();
private volatile WeakReference _valueReference = new WeakReference(null);
public MyType Value
{
get
{
MyType value = _valueReference.Target as MyType;
_lock.EnterUpgradeableReadLock();
try
{
if (!_valueReference.IsAlive) // needs initializing
{
_lock.EnterWriteLock();
try
{
if (!_valueReference.IsAlive) // check again
{
// prevent reading the old weak reference
Thread.MemoryBarrier();
_valueReference = new WeakReference(value = InitializeMyType());
}
}
finally
{
_lock.ExitWriteLock();
}
}
}
finally
{
_lock.ExitUpgradeableReadLock();
}
return value;
} …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用MailboxProcessor建模异步作业处理框架.我的要求是启动,停止,暂停和恢复作业处理器.我可以使用MailboxProcessor构建暂停/恢复功能吗?我也应该能够停下来开始吗?我试图在Windows服务后建模.
我有一个C#系统,使用Queue/Threads实现.我正在寻找设计替代品,就在我看到MailboxProcessor的时候.我相信我可以使用它,但无法弄清楚如何处理上述情况.那么有可能实现这个功能吗?
如何将新子项插入C#WinForms中TreeView中的特定节点?
我一直在笨拙地刺穿TreeViews将近一个小时,我想像这样使用C#的TreeView:
treeView.getChildByName("bob").AddChild(new Node("bob's dog"));
Run Code Online (Sandbox Code Playgroud)
这是我上次尝试的(我认为C#应该永远不会让我达到的毛茸茸程度):
tree.Nodes[item.name].Nodes.Add(new TreeNode("thing"));
Run Code Online (Sandbox Code Playgroud)
不用说,它不起作用.
哦,这是一个懒惰的问题:你真的可以在这些节点中存储对象吗?或者TreeNode只支持字符串和诸如此类的东西?(在这种情况下我应该扩展TreeNode .. /叹气)
请帮忙,谢谢!
我的第一个编程工作向我介绍了单元测试和模拟对象的概念,但总有一些问题.
假设我们正在编写一个银行应用程序,并且需要模拟BankAccount对象:
// boilerplate code
public interface IBankAccount {
void Deposit(int amount);
void Withdrawal(int amount);
int getBalance();
int getAccountNumber();
}
public interface IBankAccountFactory {
IBankAccount getAccount(int accountNumber);
}
public class ProductionBankAccountFactory implements IBankAccountFactory {
public IBankAccount getAccount(int accountNumber) {
return new RealBankAccount(accountNumber);
}
}
public class MockBankAccountFactory implements IBankAccountFactory {
public IBankAccount getAccount(int accountNumber) {
return new MockBankAccount(accountNumber);
}
}
public static class BankAccountFactory {
// ewww, singletons!
public static IBankAccountFactory Instance;
}
// finally, my actual business objects
public …Run Code Online (Sandbox Code Playgroud) 我想在C#中为DateTime变量传递null值.该值应作为null存储在数据库中.
我尝试过使用Datetime.Minvalue,但它存储了一个默认值.它必须是数据库中的null.我怎么做?
我想在一个单独的线程上调用重型方法dowork并在超过3秒时将其杀死.以下代码有什么问题吗?
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
Console.WriteLine("starting new thread");
Thread t = new Thread(new ThreadStart(dowork));
t.Start();
DateTime start = DateTime.Now;
TimeSpan span = DateTime.Now.Subtract(start);
bool wait = true;
while (wait == true)
{
if (span.Seconds > 3)
{
t.Abort();
wait = false;
}
span = DateTime.Now.Subtract(start);
}
Console.WriteLine("ending new thread after seconds = {0}", span.Seconds);
Console.WriteLine("all done");
Console.ReadLine();
}
static void dowork()
{
Console.WriteLine("doing …Run Code Online (Sandbox Code Playgroud) 我有自定义对象MyClass,我需要创建它的3个实例.
而不是做一些像哑巴一样的事情:
MyClass instance1 = new MyClass();
MyClass instance2 = new MyClass();
MyClass instance3 = new MyClass();
Run Code Online (Sandbox Code Playgroud)
我不能做这样的事情:
MyClass[] instances = new MyClass();
instances[0].somemethod;
Run Code Online (Sandbox Code Playgroud)
?