1

尝试在我打开的代理/通道内设置一些变量时出现超时异常。我不确定它是坏的 DataContract 还是我没有正确设置的东西。

这是服务接口:

[ServiceContract]
public interface ICommandBoardService
{
    [OperationContract]
    void Hello();

    [OperationContract]
    Command_Board.States getState();

    [OperationContract]
    void setState(Command_Board.States s);

    [OperationContract]
    string setConnected(int i);

    [OperationContract]
    int getConnected();

}

这是服务类:

public class CommandBoardService : ICommandBoardService
{
    [DataMember]
    public Command_Board.States state;

    [DataMember]
    public int connected = 0;

    public void Hello()
    {
    }

    public Command_Board.States getState()
    {
        return state;
    }

    public void setState(Command_Board.States s)
    {
        state = s;
    }

    public string setConnected(int i){
        connected += i;
        return "Player "+connected+" Connected";
    }

    public int getConnected(){
        return connected;
    }
}

这是我打开主机并调用代理的地方:

            ICommandBoardService proxy;
            using (ServiceHost host = new ServiceHost(typeof(CommandBoardServiceLibrary.CommandBoardService)))
            {
                host.AddServiceEndpoint(typeof(
                    CommandBoardServiceLibrary.ICommandBoardService),
                    new NetTcpBinding(),
                    "net.tcp://localHost:9000/CommandBoardEndPoint");
                host.Open();


                proxy = ChannelFactory<ICommandBoardService>.CreateChannel(
                             new NetTcpBinding(),
                            new EndpointAddress(
                            "net.tcp://localhost:9000/CommandBoardEndPoint"));

                proxy.setConnected(0);
                proxy.setState(state);
            }

当我到达时出现以下错误proxy.setConnected(0),即使我将它们翻转过来,我也会遇到同样的错误proxy.setState(state)

这是错误:

This request operation sent to net.tcp://localhost:9000/CommandBoardEndPoint did not receive a reply within the configured timeout (00:01:00). The time allotted to this operation may have been a portion of a longer timeout. This may be because the service is still processing the operation or because the service was unable to send a reply message. Please consider increasing the operation timeout (by casting the channel/proxy to IContextChannel and setting the OperationTimeout property) and ensure that the service is able to connect to the client.

我能做些什么来修复错误?有人说增加最大缓冲区,但我不知道如何使用 WinForms 来做到这一点。

4

2 回答 2

0

您将在绑定属性下的 app.config 中增加 maxBufferSize

<binding name="blah"
         maxBufferSize="2147483647" -- Max int size
/>
于 2013-11-08T22:42:02.003 回答
0

您正在打开主机并在同一线程上创建代理。如果这行得通,我会感到惊讶;我看不出主机实际上是如何执行任何操作的。客户端将同步等待来自主机的答案——它永远不会给出答案,因为它只能在客户端正在使用的线程上完成这项工作。

我认为,解决方案是使用 .net 中可用的多种不同多线程方法之一在单独的线程中启动主机,以便两者可以同时工作。

于 2013-11-09T01:04:53.687 回答