【问题标题】:Unable to connect to self-hosted WCF service无法连接到自托管 WCF 服务
【发布时间】:2013-04-12 19:12:34
【问题描述】:

我正在尝试构建一个相当基本的 WCF SOAP Web 服务,作为 Windows 服务自托管。 Windows 服务本身已在我的机器上启动并运行——我只是无法通过 Visual Studio 或 Web 浏览器在本地访问它。

相关的 C# 代码如下。假设MyDummyService 执行合约IDummyService

public class Program : ServiceBase
{
    private ServiceHost host = null;
    private readonly Uri baseAddress = new Uri("http://localhost:8000/DummyAPI");

    public static readonly ILog log = LogManager.GetLogger(typeof(Program));
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    public static void Main(string[] args)
    {
        ServiceBase.Run(new Program());
    }

    public Program()
    {
        this.ServiceName = "DummyService";
    }

    protected override void OnStart(string[] args)
    {
        log.Info("Starting service");

        try
        {
            base.OnStart(args);

            host = new ServiceHost(typeof(MyDummyService), baseAddress);

            host.Open();
        }
        catch (Exception ex)
        {
            log.Error(ex.ToString());
        }
        finally
        {
            if (host != null)
                ((IDisposable)host).Dispose();
        }
    }

    protected override void OnStop()
    {
        log.Info("Stopping service");
        base.OnStop();
        host.Close();
    }
}

相关app.config

<system.serviceModel>
    <services>
        <service name="DummyAPI.MyDummyService" 
                 behaviorConfiguration="MyDummyBehavior">
           <endpoint 
               address="" 
               binding="basicHttpBinding" 
               contract="DummyAPI.IDummyService" />
           <endpoint address="mex" binding="mexHttpBinding" 
                     contract="IMetadataExchange" />
        </service>
    </services>
    <behaviors>
        <serviceBehaviors>
            <behavior name="MyDummyBehavior">
                <serviceMetadata httpGetEnabled="True" policyVersion="Policy15"/>
                <serviceDebug includeExceptionDetailInFaults="True"/>
            </behavior>
        </serviceBehaviors>
    </behaviors>
</system.serviceModel>

当我访问时

http://localhost:8000/DummyAPI

http://localhost:8000/DummyAPI/MyDummyService

(或后面跟着 ?wsdl 的任何一个)在网络浏览器中,我得到一个 404。显而易见的第一个问题:我在上面搞砸了什么?

web.config 命名空间(或看起来像命名空间的东西)让我有些困惑。我可以在现场安全地弥补什么,以及什么需要反映 C# 类命名空间?

【问题讨论】:

标签: c# wcf soap windows-services


【解决方案1】:

看起来您在 Start 方法中处理您的 ServiceHost,这实际上让您没有运行 ServiceHost。

finally
{
    if (host != null)
        ((IDisposable)host).Dispose();
}

【讨论】:

  • 你明白了 ;) 开发时多集中一点表示赞赏
  • 呃,就是这样。注意力就在那里,它只是指向了所有错误的地方。愚蠢的愚蠢。谢谢。
【解决方案2】:

一个好的入门是Configuring Services Using Configuration Files

service 元素上的 name 属性必须是实现该服务的类型的完全限定名称。

endpoint 元素上的 contract 属性必须是服务实现的接口的完全限定名称。

如果满足这两个条件,则 ServiceHost 应该找到并应用应用配置中指定的配置。

如果您是 WCF 新手,一个简单的入门方法是使用 WCF 服务配置编辑器,该编辑器可从 Visual Studio 的工具菜单中获得。

【讨论】:

    猜你喜欢
    • 2013-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多