【问题标题】:How to host WCF service in console apps如何在控制台应用程序中托管 WCF 服务
【发布时间】:2012-10-26 04:10:14
【问题描述】:

我正在学习 wcf。所以我创建了 wcf 项目,它有一个类。代码如下

namespace TestWcfService1
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
public class Service1 : IService1
{
    public string GetData(string value)
    {
        return string.Format("You entered: {0}", "Welcome " + value);
    }

}
}

现在,当我尝试将 wcf 服务引用添加到我的控制台应用程序时,例如添加服务引用和像这样的服务 url http://localhost:21541/Service1.svc 然后我得到名为元数据的错误包含无法解析的引用:'http://localhost:21541/Service1.svc'。

所以我只是无法实现我的目标。我知道有些地方我遗漏了一些东西,这就是我出错的原因。所以请指导我如何将服务引用添加到控制台应用程序。 app.config 将自动更新或者我需要在那里写任何东西。请帮忙。谢谢

【问题讨论】:

  • 您的服务正在运行吗?见How to Consume a Web Service
  • 我不知道如何检查服务是否正在运行。指导我
  • 我检查服务是否正在运行,是否出现错误“无法添加服务。服务元数据可能无法访问。确保您的服务正在运行并公开元数据。”

标签: wcf host wcf-hosting


【解决方案1】:

在配置中仔细检查服务行为是否设置为允许服务元数据:

<serviceMetadata httpGetEnabled="true"/>

并在服务部分添加元数据端点

<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>

编辑:示例配置

<system.serviceModel>
  <services>
    <service behaviorConfiguration="BehaviorConfig"
      name="[ServiceNameGoesHere]">
      <endpoint address="" binding="wsHttpBinding" contract="[ServiceContractHere]">
      </endpoint>
      <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
    </service>
  </services>
  <behaviors>
    <serviceBehaviors>
      <behavior name="BehaviorConfig">
        <serviceMetadata httpGetEnabled="True"/>
        <!-- To receive exception details in faults for debugging purposes, 
        set the value below to true.  Set to false before deployment 
        to avoid disclosing exception information -->
        <serviceDebug includeExceptionDetailInFaults="False" />
      </behavior>
    </serviceBehaviors>
  </behaviors>
</system.serviceModel>

在 Dan Rigsby 的博客WCF Metadata 上有一篇相当不错的文章,其中更详细地解释了有关设置 MEX 端点(添加服务引用工作所必需的)的详细信息。

【讨论】:

  • 你能给我示例 web.config wcf 服务的所有条目吗
  • 我认为 web.config 相关的所有条目都会自动添加,但我需要自己手动编码。
【解决方案2】:

如果您的解决方案中有 WCF 服务项目,您可以右键单击控制台应用程序并说“添加服务引用”,然后单击“发现”按钮,它应该会为您找到它。
是的,配置文件需要使用行为和端点等进行更新。

看:WCF Metadata contains a reference that cannot be resolved

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-02
    • 1970-01-01
    • 1970-01-01
    • 2012-10-21
    • 2014-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多