我有一个非常简单的WCF服务,它托管在一个Windows应用程序中。我想通过 GET 方法访问该服务,但只是获取一个空页面。我猜配置有问题,但我无法弄清楚是什么。这是代码:
WCFService.cs
namespace WCFServiceApp
{
[ServiceContract]
public class WCFService
{
[OperationContract]
[WebGet]
public int GetNumbers()
{
return 12345;
}
}
}
Form1.cs的
private static Uri baseAddress = new Uri("http://localhost:8090");
private ServiceHost host = new ServiceHost(typeof(WCFService), baseAddress);
protected override void OnLoad(EventArgs e)
{
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
host.Description.Behaviors.Add(smb);
host.Open();
base.OnLoad(e);
}
的App.config
<?xml version="1.0"?>
<configuration>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="WCFServiceBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="WebBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service name="WCFService" behaviorConfiguration="WCFServiceBehavior">
<endpoint address="ws" binding="wsHttpBinding" contract="WCFService">
</endpoint>
<endpoint address="" behaviorConfiguration="WebBehavior" binding="webHttpBinding" contract="WCFService">
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
</system.serviceModel>
</configuration>
当我使用浏览器点击 localhost:8090 时,我会看到WCF欢迎页面。当我点击 localhost:8090 / GetNumbers 时,只是获取一个空页而不是数字。