WCF服务自托管麻烦

时间:2009-10-02 12:22:00

标签: .net windows-services wcf self-hosting

我正在尝试在Windows服务中托管WCF服务,我通过控制台应用程序启动它。每个服务都是自己的项目,控制台应用程序也是如此。我已将app.config从WCF服务库复制到控制台应用程序的app.config中,但我不断收到“服务没有应用程序端点......”。我在几个地方读过这个错误意味着我的类型引用不是完全合格的,但是我已经检查了两次(三倍,四倍......)。而且我很确定我有一个app.config。我的调试目录中有3个exes:Console App,Console App vshost,Win Service。 Win服务没有app.config,所以我尝试复制它的app.config,以防它正在寻找它,但没有运气。我还检查了以确保配置正确命名(< program> .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>

2 个答案:

答案 0 :(得分:0)

您的配置文件是否已命名为

  • 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">

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

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

答案 1 :(得分:0)

我从来没有深究这一点。配置/项目中略有偏差。我重建了解决方案,问题就消失了。

相关问题