WCF 服务自托管问题

发布于 2024-08-07 02:24:07 字数 3648 浏览 5 评论 0原文

我正在尝试在 Windows 服务中托管 WCF 服务,我通过控制台应用程序启动该服务。每个服务都是它自己的项目,控制台应用程序也是如此。我已将 WCF 服务库中的 app.config 复制到控制台应用程序的 app.config 中,但我不断收到“服务具有零应用程序端点...”。我在一些地方读到该错误意味着我的类型引用不完全合格,但我已经双重(三重、四重......)检查了这一点。我很确定我有一个app.config。我的调试目录中有3个exe:Console App、Console App vshost、Win Service。 Win 服务没有 app.config,因此我尝试复制其 app.config 以防它正在寻找它,但没有成功。我还检查以确保配置命名正确(.exe.config)。

这是我正在使用的。我的控制台应用创建一个 JobSchdeuler 实例并调用 JobSchedulerConsoleStart

主机代码:

public partial class JobScheduler : ServiceBase
{
    ServiceHost jobServiceHost = null;

    public JobScheduler()
    {
        ServiceName = "JobSchedulerService";
        InitializeComponent();
    }

    #region Service Init/Uninit

    /// <summary>
    /// OnStart
    /// </summary>
    /// <param name="args"></param>
    protected override void OnStart(string[] args)
    {
        if (jobServiceHost != null)
        {
            jobServiceHost.Close();
        }

        jobServiceHost = new ServiceHost(typeof(JobSchedulerWCF.JobService));

        jobServiceHost.Open();
    }

    /// <summary>
    /// OnStop
    /// </summary>
    protected override void OnStop()
    {
        if (jobServiceHost != null)
        {
            jobServiceHost.Close();
            jobServiceHost = null;
        }
    }

    #endregion

    #region Debugging

    public void JobSchedulerConsoleStart()
    {
        this.OnStart(null);
        Console.WriteLine("Service Started.");

        ProcessInput();

        Console.WriteLine("Service Stopped.");
        this.OnStop();
    }

    private void ProcessInput()
    {
        Console.WriteLine("Press any key to quit...");
        Console.ReadKey();
    }

    #endregion
}

app.config

<?xml version="1.0"?>
<configuration>
    <system.serviceModel>
        <services>
            <service behaviorConfiguration="JobSchedulerWCF.Service1Behavior" name="JobSchedulerWCF.JobService, JobSchedulerWCF">
                <endpoint address="" binding="wsHttpBinding" contract="JobSchedulerWCF.IJobServiceController, JobSchedulerWCF">
                    <identity>
                        <dns value="localhost" />
                    </identity>
                </endpoint>
                <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
                <host>
                    <baseAddresses>
                        <add baseAddress="http://localhost:12345/jobService"/>
                    </baseAddresses>
                </host>
            </service>
        </services>
        <behaviors>
            <serviceBehaviors>
                <behavior name="JobSchedulerWCF.Service1Behavior">
                    <!-- To avoid disclosing metadata information, 
                        set the value below to false and remove the metadata endpoint above before deployment -->
                    <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>          
</configuration>

I'm trying to host a WCF Service inside a Windows Service which I am starting through a console app. Each service is its own project as is the console app. I've copied the app.config from the WCF Service library into the app.config of the console app, but I keep getting "Service has zero application endpoints...". I've read in a few places that the error means my type references are not fully qualified, but I've double (triple, quadruple...) checked that. And I'm pretty sure I have an app.config. There are 3 exes in my debug directory: Console App, Console App vshost, Win Service. The Win Service didn't have an app.config, so I tried copying its app.config in case it was looking for it, but no luck. I also checked to make sure the configs were named correctly (<program>.exe.config).

Here's what I'm using. My console app creates an instance of JobSchdeuler and calls JobSchedulerConsoleStart.

Host Code:

public partial class JobScheduler : ServiceBase
{
    ServiceHost jobServiceHost = null;

    public JobScheduler()
    {
        ServiceName = "JobSchedulerService";
        InitializeComponent();
    }

    #region Service Init/Uninit

    /// <summary>
    /// OnStart
    /// </summary>
    /// <param name="args"></param>
    protected override void OnStart(string[] args)
    {
        if (jobServiceHost != null)
        {
            jobServiceHost.Close();
        }

        jobServiceHost = new ServiceHost(typeof(JobSchedulerWCF.JobService));

        jobServiceHost.Open();
    }

    /// <summary>
    /// OnStop
    /// </summary>
    protected override void OnStop()
    {
        if (jobServiceHost != null)
        {
            jobServiceHost.Close();
            jobServiceHost = null;
        }
    }

    #endregion

    #region Debugging

    public void JobSchedulerConsoleStart()
    {
        this.OnStart(null);
        Console.WriteLine("Service Started.");

        ProcessInput();

        Console.WriteLine("Service Stopped.");
        this.OnStop();
    }

    private void ProcessInput()
    {
        Console.WriteLine("Press any key to quit...");
        Console.ReadKey();
    }

    #endregion
}

app.config

<?xml version="1.0"?>
<configuration>
    <system.serviceModel>
        <services>
            <service behaviorConfiguration="JobSchedulerWCF.Service1Behavior" name="JobSchedulerWCF.JobService, JobSchedulerWCF">
                <endpoint address="" binding="wsHttpBinding" contract="JobSchedulerWCF.IJobServiceController, JobSchedulerWCF">
                    <identity>
                        <dns value="localhost" />
                    </identity>
                </endpoint>
                <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
                <host>
                    <baseAddresses>
                        <add baseAddress="http://localhost:12345/jobService"/>
                    </baseAddresses>
                </host>
            </service>
        </services>
        <behaviors>
            <serviceBehaviors>
                <behavior name="JobSchedulerWCF.Service1Behavior">
                    <!-- To avoid disclosing metadata information, 
                        set the value below to false and remove the metadata endpoint above before deployment -->
                    <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>          
</configuration>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

仲春光 2024-08-14 02:24:07

您的配置文件是否名为

  • Console.App.exe.config
  • Win.Service.exe.config

编辑:如果我没记错的话,WCF 和服务名称的 Beta 版本存在问题。

尝试更改

<service behaviorConfiguration="JobSchedulerWCF.Service1Behavior" name="JobSchedulerWCF.JobService, JobSchedulerWCF">

<service behaviorConfiguration="JobSchedulerWCF.Service1Behavior" name="JobSchedulerWCF.JobService">

删除配置文件中的程序集名称。

如果这能解决问题,请告诉我。我没有绑它。

Do your configuration files are named

  • Console.App.exe.config
  • Win.Service.exe.config

?

EDIT: If I remember correctly there was an issue with the Beta version of WCF and Service names.

Try to change

<service behaviorConfiguration="JobSchedulerWCF.Service1Behavior" name="JobSchedulerWCF.JobService, JobSchedulerWCF">

to

<service behaviorConfiguration="JobSchedulerWCF.Service1Behavior" name="JobSchedulerWCF.JobService">

Remove the assembly name in your config files.

Please let me know if that solves the problem. I didn't tied it.

晚雾 2024-08-14 02:24:07

我一直没有弄清楚这件事的真相。配置/项目中有些问题。我重建了解决方案,问题就消失了。

I never got to the bottom of this. There was something slightly off in the config/project. I rebuilt the solution and the problem went away.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文