Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何对WFC Web套接字进行单元测试_C#_Wcf_Unit Testing_Websocket - Fatal编程技术网

C# 如何对WFC Web套接字进行单元测试

C# 如何对WFC Web套接字进行单元测试,c#,wcf,unit-testing,websocket,C#,Wcf,Unit Testing,Websocket,我正在尝试创建一个单元测试,以便逐步通过我的代码访问当我试图通过实体框架访问数据时发生的错误。我在寻找答案的过程中没有找到好的例子。我尝试在代码的第二行创建一个客户机,当我在调试器中检查client.InnerChannel时,得到一个“client.InnerChannel抛出了System.ServiceModel.CommunicationObjectFaultedException类型的异常”。WebSocket在我的客户机中运行良好,只需执行教程中的简单示例,但一旦我尝试通过实体框架访

我正在尝试创建一个单元测试,以便逐步通过我的代码访问当我试图通过实体框架访问数据时发生的错误。我在寻找答案的过程中没有找到好的例子。我尝试在代码的第二行创建一个客户机,当我在调试器中检查client.InnerChannel时,得到一个“client.InnerChannel抛出了System.ServiceModel.CommunicationObjectFaultedException类型的异常”。WebSocket在我的客户机中运行良好,只需执行教程中的简单示例,但一旦我尝试通过实体框架访问DB,就会抛出一个我无法检查的错误

我正在使用本教程中提供的代码

我的接口定义如下:

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”);