2

我有一个非常简单的 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);
}

应用程序配置

<?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时,只是得到一个空页面而不是数字。

4

0 回答 0