使用WCF的TimeoutException

时间:2013-11-08 22:18:48

标签: c# wcf exception timeoutexception

我在我打开的代理/通道中设置一些变量时遇到超时异常。 我不确定它是不好的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。

2 个答案:

答案 0 :(得分:0)

您将在绑定属性

下的app.config中增加maxBufferSize
<binding name="blah"
         maxBufferSize="2147483647" -- Max int size
/>

答案 1 :(得分:0)

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

我认为,解决方案是使用.net中提供的许多不同的多线程方法之一来在一个单独的线程中启动主机,这样两者都可以同时完成它们的工作。