WCF异常:ServiceHost仅支持类服务类型 - 从Windows服务运行时

时间:2014-07-25 12:52:30

标签: wcf windows-services

我是wcf的新手。我正面临这个错误ServiceHost only supports class service types

这里我会说我正在做&运行我的胜利服务& wcf在一起。 我添加了Windows服务项目,并在win服务项目中为wcf添加了一些像System.ServiceModel这样的引用。当我试图从win服务运行wcf服务然后我收到错误称为 ServiceHost仅支持类服务类型

我搜索&得到了许多答案,如

ServiceHost host = new ServiceHost(
                       typeof(subservice.ISubService), new Uri("someuri"));

如果这是您的用法,请将其更改为使用已实施的服务类类型ISubService

ServiceHost host = new ServiceHost(
                       typeof(subservice.SubService), new Uri("someuri"));

如果在.svc中配置服务,则:

<%@ServiceHost Service="subservice.SubService"%>

同样在您的配置文件中,将服务名称更改为服务而不是服务合同:

   <services>
          <service name="subservice.SubService">
          ...

其他搜索结果也说了很多类似的东西来摆脱这个问题。

我的wcf服务没有svc文件。我只有一个文件,我有合同和服务类。我也有配置文件。

在这里,我将简要介绍我的服务代码

namespace SageDataImportWCF
{
    [ServiceContract]
        public interface ISagePart
        {

            [OperationContract]
            string SageInsertionProcess(string SQLConnectionString, string CountryCode);

            // TODO: Add your service operations here
        }

     public class SagePartInsertion : ISagePart
     {
            public string SageInsertionProcess(string SQLConnectionString, string CountryCode)
            {
            }
     }
}

这里我给出了我试图从win服务

运行的代码
namespace SageDataImportWCF
{
    public partial class SageDateInsertionService : ServiceBase
    {
        #region Local Variables
        ServiceHost serviceHost;
        #endregion

        #region Constructor
        public SageDateInsertionService()
        {
            InitializeComponent();
            serviceHost = null;
            ServiceName = "Sage DataInsertion Service";
        }
        #endregion

        protected override void OnStart(string[] args)
        {
            string strAdrHTTP = "http://192.168.6.2:11000/SagePartInsertion";
            if (serviceHost != null)
            {
                serviceHost.Close();
            }
            serviceHost = new ServiceHost(typeof(SageDataImportWCF.SagePartInsertion));
            serviceHost.AddServiceEndpoint(typeof(SageDataImportWCF.ISagePart), new BasicHttpBinding(), strAdrHTTP);
            ServiceMetadataBehavior behaviour = new ServiceMetadataBehavior();
            behaviour.HttpGetEnabled = true;
            serviceHost.Description.Behaviors.Add(behaviour);
            serviceHost.Open();
        }

        protected override void OnStop()
        {
            if (serviceHost != null)
            {
                serviceHost.Close();
                serviceHost = null;
            }
        }
    }
}

这是我的wcf服务的配置条目

<configuration>
    <system.serviceModel>
        <services>
            <service name="SageDataImportWCF.SagePartInsertion" behaviorConfiguration="SageBehavior">
                <endpoint address="http://localhost:9001/SagePartInsertion"  contract="SageDataImportWCF.ISagePart" binding="basicHttpBinding"/>
            </service>
        </services>
        <behaviors>
            <serviceBehaviors>
                <behavior name="SageBehavior">
                    <!-- 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="true"/>
                </behavior>
            </serviceBehaviors>
        </behaviors>
        <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
    </system.serviceModel>
    <system.webServer>
        <modules runAllManagedModulesForAllRequests="true"/>
    </system.webServer>
</configuration>

这里我已经粘贴了所有相关代码,我想要请一些代码请查看我的代码并告诉我为什么我在尝试从Windows服务运行时收到错误消息ServiceHost only supports class service types。我错过了代码吗?

我应该有一个单独的项目用于wcf类库和另一个单独的项目用于Windows服务,因为我有一个项目那里我有wcf&amp;文件的文件Windows服务两者。

所以寻找像我需要在代码中纠正的建议,因为win服务可以启动wcf服务。请帮忙。

1 个答案:

答案 0 :(得分:0)

检查标记中服务的定义:

右键单击SagePartInsertion.svc文件,然后选择“查看标记”。

确保服务是接口的实现,如下所示:

<%@ ServiceHost Language="C#" Debug="true" Service="SageDataImportWCF.SagePartInsertion" CodeBehind="SagePartInsertion.svc.cs" %>

过去它失败了,因为我正在引用该接口。