我正在对 WCF 双工服务进行单元测试。下面是实现服务接口和回调合约接口的部分代码。在测试中,ServiceHost.Open 监听来自客户端调用 EmailRequest 方法的传入消息。我需要保存传入的当前上下文以回复而不是我的测试断言。我遇到的问题是在测试中我尝试了许多不同的方法,我无法保存传入消息的当前上下文以发回给他们。它总是为 NullRefrence 抛出异常。它让我发疯。如何在测试中保存 OperationContext.Current?
[ServiceContract]
Name="Email",
CallbackContract=typeof(IEmailCallbackcontract),
SeeionMode=SessionMode.Allowed)]
public interface IEmail
{
[OperationContract(IsOneWay=true, Name="Message")]
Void EmailRequest(string request, int MssgID);
}
[ServiceContract(Name="EmaiCallbackcontract")]
public interface IEmailCallbackcontract
{
[OperationContract(IsOneWay=true, Name="ResMessage")]
void EmailResponse(string response);
}
[ServiceBehavior(InstanceConextMode=InstanceContextMode.Single)]
public class Email:IEmail
{
public void EmailRequest(string message, int MssgID)
{
IEmailCallbackcontract callbak=OperationContect.Current.GetCallbackChannel<IEmailCallbackcontract>();
Console.Write("Message Recieved");
callback.EmailResponse("I got the message");
}
}
我是 NUnit 测试
[Test]
public void TestEmail()
{
//I create a servicehost based on binding information
Servicehost.Open();
OperationContext.Current.GetCallbackChannel<IEmailCallbackcontract>();
//I want to post message back using OperationContext but keep getting Nullrefrence error
//for some reason test wont save the Current context of incoming messages.
}