我正在进行单元测试WCF双工服务。下面是实现服务接口和callbackcontract接口的代码的一部分。在测试中,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.
}