【问题标题】:WCF service not opening in the browser?WCF 服务未在浏览器中打开?
【发布时间】:2016-09-02 06:28:46
【问题描述】:

我正在学习 WCF,我开始创建一个非常基本的主机应用程序,它使用如下方法定义一个类:

 [ServiceContract()]
    public interface IMath
    {
        [OperationContract]
        int Add(int a, int b);
    }
    public class MathCalcs : IMath
    {
        public MathCalcs()
        {
            Console.WriteLine("Service await two numbers...");
        }
        public int Add(int a, int b)
        {
            return a + b;
        }
    }

这就是我配置 App.config 文件的方式:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <services>
      <service name="ConsoleHost.MathCalcs" behaviorConfiguration="MathServiceMEXBehavior">
        <endpoint address="http://localhost:8080/MathCalcs"
                  binding="basicHttpBinding"
                  contract="ConsoleHost.IMath"/>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8080/MathCalcs"/>
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="MathServiceMEXBehavior">
          <serviceMetadata httpGetEnabled="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
  </startup>
</configuration>

然后我从Main调用服务

 using (ServiceHost host = new ServiceHost(typeof(MathCalcs)))
            {
                host.Open();
                Console.WriteLine("***The service is ready***");
            }
            Console.ReadLine();

但它无法通过 URI http://localhost:8080/MathCalcs 查看服务的元数据,我确信我正在按照我正在阅读的书的步骤操作,并且前面的示例工作正常,唯一的区别是我没有在一个独立的类库中分离服务逻辑(接口和类)。 我错过了什么?

【问题讨论】:

    标签: c# wcf


    【解决方案1】:

    以下代码行

    Console.ReadLine();
    

    必须在 using 子句的大括号内! 完成后,重试查找 WSDL 元数据。

    【讨论】:

      【解决方案2】:

      这是一个相关的帖子,我认为它可能会引导你走向正确的方向。

      WCF service showing blank in browser

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-02-13
        • 2014-02-04
        • 2017-02-05
        • 2017-07-14
        • 2011-06-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多