【问题标题】:Simple WCF Sample failing简单的 WCF 示例失败
【发布时间】:2012-05-08 18:53:40
【问题描述】:

这是一个非常简单的 WCF 示例,我找不到失败的原因。

这是在 WCF 类库项目中定义的

这是定义合约的接口:

<ServiceContract()> _
Public Interface IService1

    <OperationContract()> _
    Function GetData(ByVal value As Integer) As String

    <OperationContract()> _
    Function Ping() As Boolean

End Interface

这是接口的实现:

<ServiceBehavior(IncludeExceptionDetailInFaults:=True)> _
Public Class Service1
    Implements IService1

    Public Function GetData(ByVal value As Integer) As String Implements IService1.GetData
        Return String.Format("You entered: {0}", value)
    End Function

    Public Function Ping() As Boolean Implements IService1.Ping
        Return True
    End Function
End Class

然后,在一个名为 Server 的控制台应用程序中,我添加了对上面定义的服务的服务引用。我创建服务主机如下:

static void Main(string[] args)
{
   try
   {
      ServiceHost serviceHost = new ServiceHost(typeof(ServiceReference.Service1Client));
      NetTcpBinding netTcpBinding = new NetTcpBinding();

      serviceHost.AddServiceEndpoint(typeof(ServiceReference.IService1), netTcpBinding, "net.tcp://localhost:9007/Service/");

      Console.WriteLine("Service started at net.tcp://localhost:9700/Service");

      serviceHost.Open();

      Console.WriteLine("Host opened");
      Console.ReadLine();
   }
   catch (Exception ex)
   {
      Console.WriteLine(ex.Message);
      Console.Read();
   }
   finally
   {
       Console.WriteLine("Service closed");
   }
}

然后,在另一个名为 Client 的控制台应用程序中,我向该服务添加了一个服务引用并创建了一个代理客户端,如下所示:

static void Main(string[] args)
{
   NetTcpBinding netTcpBinding = new NetTcpBinding();

   try
   {
       var serviceClient = new Client.ServiceReference1.Service1Client(netTcpBinding, new EndpointAddress("net.tcp://localhost:9007/Service/"));
       serviceClient.InnerChannel.OperationTimeout = TimeSpan.FromMinutes(10);

       Console.WriteLine("Pinging server");               

       if (serviceClient.Ping())
       {
          Console.WriteLine("Getting Data");

          var result = serviceClient.GetData(1);

          Console.WriteLine("Result is " + result);
       }
       else
       {
          Console.WriteLine("Server is not responding");
       }
   }
   catch (Exception ex)
   {
       Console.WriteLine(ex.Message);
   }

   Console.ReadLine();
}

当我运行 Server.exe 时,它显示 Service started at net.tcp://localhost:9700/Service

当我运行 Client.exe 时,它显示 Ping 服务器

然后我在 WCF 类库项目中收到一个我无法调试的错误,最后在 Client.exe 中出现异常,说

这个发送到 net.tcp://localhost:9007/Service/ 的请求操作确实 在配置的超时 (00:01:00) 内未收到回复。这 分配给此操作的时间可能是较长时间的一部分 暂停。这可能是因为服务仍在处理 操作或因为服务无法发送回复消息。 请考虑增加操作超时(通过强制转换 通道/代理到 IContextChannel 并设置 OperationTimeout 属性)并确保服务能够连接到 客户。

在我的笔记本电脑上如此简单并在本地运行,我认为超时或类似的问题没有问题。

谁能解释一下...我会很感激的

【问题讨论】:

  • 您的服务托管在端口 9700 上,在客户端中您指的是端口号为 9007。

标签: wcf


【解决方案1】:

你的代码在打开服务器前打印一个静态字符串,然后在打开服务器后打印一个字符串:

Console.WriteLine("Service started at net.tcp://localhost:9700/Service");

serviceHost.Open();

Console.WriteLine("Host opened");

您说您看到了第一条消息,但您从未说过您看到第二条消息。如果没有看到该消息,我认为您对 serviceHost.Open(); 的调用可能有问题

这是在 WCF 中使用 NetTcpBingings 的示例 http://www.dotnetfunda.com/articles/article704-using-nettcpbinding-in-wcf-.aspx

有时从别人的代码开始,然后从那里修改它会有所帮助。

【讨论】:

    猜你喜欢
    • 2019-03-07
    • 1970-01-01
    • 1970-01-01
    • 2016-10-08
    • 2014-11-20
    • 2017-10-17
    • 1970-01-01
    • 1970-01-01
    • 2017-09-19
    相关资源
    最近更新 更多