【发布时间】:2014-07-16 08:16:54
【问题描述】:
我想在 Winforms 应用程序中创建一个非常简单的 WebService (WCF)。为了测试这一点,我在我的 Winforms 应用程序中创建了一个名为 Calculator 的 WCF 服务。另一方面,我创建了一个简单的 Winforms 应用程序,其中包含一个用于检索服务结果的按钮。
我已添加 Web 服务作为对 testproject 的引用。它可以识别两种实现的方法,一切都很好。问题是我没有收到来自 web 服务的响应(冻结我的测试应用程序)。我很可能忘记了 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
}
这就是我打开 Web 服务的方式。
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>
如果我在网络浏览器中输入 URL (http://localhost:8731/Solution/Calculator/),我会看到这个屏幕:
【问题讨论】:
-
你想把它全部托管在一起吗? ...如果是这样,有两件事。 1) 是否有任何代码可以用
ServiceHost启动服务实例? 2)这里也没有客户端配置部分。 -
啊,是的。我忘记了ServiceHost。看起来我的 Web 服务在线,我可以连接到它。但是我不能使用任何方法,或者我没有从它们那里得到结果。
-
你可以在
http://localhost:8731/Solution/Calculator/浏览它吗? -
添加了一些缺失的信息。
-
我的 app.config 可能有问题吗?