简体   繁体   English

无法连接到自托管WCF服务

[英]Unable to connect to self-hosted WCF service

I am trying to build a fairly basic WCF SOAP web service self-hosted as a Windows service. 我正在尝试构建作为Windows服务自托管的相当基本的WCF SOAP Web服务。 The Windows service itself is up and running on my machine -- I just can't access it locally via Visual Studio or the web browser. Windows服务本身已在我的计算机上启动并运行-我只是无法通过Visual Studio或Web浏览器在本地访问它。

The relevant C# code is below. 相关的C#代码如下。 Assume MyDummyService implements contract IDummyService : 假设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();
    }
}

The relevant app.config : 相关的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>

When I access 当我访问

http://localhost:8000/DummyAPI

or 要么

http://localhost:8000/DummyAPI/MyDummyService

(or either of those followed by ?wsdl) in the web browser, I get a 404. The obvious first question: what did I botch above? (或其中的任何一个后跟?wsdl),在Web浏览器中,我得到404。显而易见的第一个问题:我在上面说了什么?

The web.config namespaces (or what look like namespaces) have me a little confused. web.config命名空间(或类似命名空间)让我有些困惑。 What can I safely make up on the spot, and what needs to reflect C# class namespaces? 我可以当场安全地组成什么,需要反映C#类名称空间的是什么?

It looks like you are disposing your ServiceHost in Start method which efectively leaves you without running ServiceHost. 看来您要在Start方法中放置ServiceHost,从而有效地使您不运行ServiceHost。

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

A good primer is Configuring Services Using Configuration Files . 一个很好的入门是使用配置文件配置服务

The name attribute on the service element must be the fully qualified name of the type implementing the service. 服务元素上的名称属性必须是实现服务的类型的标准名称。

The contract attribute on the endpoint element must be the fully qualified name of the interface that the service implements. 端点元素上的contract属性必须是服务实现的接口的标准名称。

If these two criteria are met, then the ServiceHost should find and apply the configuration specified in the app config. 如果满足这两个条件,则ServiceHost应该找到并应用app config中指定的配置。

An easy way to get started if you are new to WCF is to use the WCF service configuration editor, available from the tools menu of Visual Studio. 如果您不熟悉WCF,一个简单的入门方法是使用WCF服务配置编辑器,该编辑器可从Visual Studio的工具菜单中获得。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM