在WinForm中托管的WCF服务无法正常工作

时间:2014-05-27 12:08:31

标签: c# winforms wcf .net-3.5

我想在Winforms应用程序中创建一个非常简单的WebService(WCF)。为了测试这个,我在Winforms应用程序中创建了一个名为Calculator的WCF服务。另一方面,我创建了一个简单的Winforms应用程序,其中包含一个用于检索服务结果的按钮。

我已将Webservice添加为testproject的参考。它识别两种实现的方法,一切都很好。问题是我没有收到来自webservice的响应(冻结我的testapplication)。我很可能在Webservice应用程序上忘了一些东西。它可能是什么?

这是网络服务:

[ServiceContract] 
public interface ICalculator 
{

[OperationContract]
double AddNumbers(double number1, double number2);

[OperationContract]
string TestMethod(); 
     

}

public class Calculator : ICalculator
{
    #region ICalculator Member

    public double AddNumbers(double number1, double number2)
    {
        return number1 + number2;
    }

    public string TestMethod()
    {
        return "HELLO WORLD!";
    }

    #endregion
}

这就是我打开Webservice的方式。

this.m_WebService = new ServiceHost(typeof(Calculator));
this.m_WebService.Open();

这是App.config

<system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="Solution.Server.CalculatorBehavior">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service behaviorConfiguration="Solution.Server.CalculatorBehavior"
        name="Solution.Server.Calculator">
        <endpoint address="" binding="wsHttpBinding" contract="Solution.Server.ICalculator">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8731/Solution/Calculator/" />
          </baseAddresses>
        </host>
      </service>
    </services>
  </system.serviceModel>

如果我在Webbrowser中输入URL(http://localhost:8731/Solution/Calculator/),我会看到这个屏幕:

Image of the Website

1 个答案:

答案 0 :(得分:0)

使用自托管WCF服务,要检查的主要事项是:

  • 您是否在允许启动httplistener的安全上下文中运行?
  • 您使用的端口是否已被阻止
  • 配置问题

尝试使用WCF测试客户端进行测试。 http://msdn.microsoft.com/en-us/library/bb552364(v=vs.110).aspx

相关问题