C# 如何对WFC Web套接字进行单元测试
我正在尝试创建一个单元测试,以便逐步通过我的代码访问当我试图通过实体框架访问数据时发生的错误。我在寻找答案的过程中没有找到好的例子。我尝试在代码的第二行创建一个客户机,当我在调试器中检查client.InnerChannel时,得到一个“client.InnerChannel抛出了System.ServiceModel.CommunicationObjectFaultedException类型的异常”。WebSocket在我的客户机中运行良好,只需执行教程中的简单示例,但一旦我尝试通过实体框架访问DB,就会抛出一个我无法检查的错误 我正在使用本教程中提供的代码 我的接口定义如下:C# 如何对WFC Web套接字进行单元测试,c#,wcf,unit-testing,websocket,C#,Wcf,Unit Testing,Websocket,我正在尝试创建一个单元测试,以便逐步通过我的代码访问当我试图通过实体框架访问数据时发生的错误。我在寻找答案的过程中没有找到好的例子。我尝试在代码的第二行创建一个客户机,当我在调试器中检查client.InnerChannel时,得到一个“client.InnerChannel抛出了System.ServiceModel.CommunicationObjectFaultedException类型的异常”。WebSocket在我的客户机中运行良好,只需执行教程中的简单示例,但一旦我尝试通过实体框架访
namespace PriceBlotterWebSocket
{
[ServiceContract(CallbackContract = typeof(IPriceBlotterCallback))]
public interface IPriceBlotterWebSocket
{
[OperationContract(IsOneWay = true, Action = "*")]
Task SendMessageToServer(Message msg);
}
[ServiceContract]
public interface IPriceBlotterCallback
{
[OperationContract(IsOneWay = true, Action = "*")]
Task SendMessageToClient(Message msg);
}
}
以及我的单元测试:
[TestClass]
public class UnitTest1
{
internal class CallBackHandler : IPriceBlotterCallback
{
public async Task SendMessageToClient(Message msg)
{
System.Diagnostics.Debug.Write(msg);
}
}
[TestMethod]
public void TestMethod1()
{
var context = new InstanceContext(new CallBackHandler());
var client = new PriceBlotteWebSocketService.PriceBlotterWebSocketClient("CustomBinding_IPriceBlotterWebSocket");
//Why can't I call client.SendMessageToServer ?
}
}
测试项目客户端配置:
<configuration>
<system.serviceModel>
<bindings>
<customBinding>
<binding name="CustomBinding_IPriceBlotterWebSocket">
<textMessageEncoding messageVersion="Soap12" />
<httpTransport>
<webSocketSettings transportUsage="Always" subProtocol="soap" />
</httpTransport>
</binding>
</customBinding>
</bindings>
<client>
<endpoint address="ws://localhost:54426/Service1.svc" binding="customBinding"
bindingConfiguration="CustomBinding_IPriceBlotterWebSocket"
contract="PriceBlotteWebSocketService.IPriceBlotterWebSocket"
name="CustomBinding_IPriceBlotterWebSocket" />
</client>
</system.serviceModel>
</configuration>
仅用于测试,能否将内部类CallBackHandler
更改为公共类CallBackHandler
?不走运,谢谢,尽管我认为我的方法存在根本性的问题。客户端对象甚至没有SendMessage方法可用。您是如何在客户端中获得IPriceBlotterCallback接口的?使用javascript,ws=newWebSocket(“ws://devserver:1.com:8120/Service1.svc”);仅用于测试,您能否将内部类CallBackHandler
更改为公共类CallBackHandler
?虽然我认为我的方法存在根本性的问题,但运气不好。客户端对象甚至没有SendMessage方法可用。您是如何在客户端中获得IPriceBlotterCallback接口的?使用javascript,ws=newWebSocket(“ws://devserver:1.com:8120/Service1.svc”);