【发布时间】:2014-08-29 13:57:35
【问题描述】:
我已经准备了一个带有 WCF 服务库和 Windows 服务的应用程序。我在 Code Project 的 this link 的帮助下完成了整个过程。
在创建WCF的所有功能后,它构建成功并且还创建了WCF DLL文件。现在,我在同一个解决方案资源管理器中创建了 Windows 服务项目,用于托管服务 DLL。在OnStart 方法中,我编写了以下代码:
namespace WindowsService1
{
public partial class Service1 : ServiceBase
{
ServiceHost sHost;
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
sHost = new ServiceHost(typeof(WcfServiceLibrary1.Service1));
sHost.Open();
}
protected override void OnStop()
{
sHost.Close();
}
}
}
为了添加WcfServiceLibrary1,我在此服务中添加了WCF DLL 的引用。为此,请右键单击 Solution Explorer 中的项目并选择 Add Reference。要在引用中添加 DLL,请在出现的窗口中选择浏览选项卡,然后导航到 WCF 服务库所在的文件夹。在该位置,可以在“bin\Release”文件夹中找到 DLL。
添加引用后没有错误,但是当我要构建解决方案时,它会给出以下错误:
找不到类型或命名空间名称“WcfServiceLibrary1”(您是否缺少 using 指令或程序集引用?)
我再次添加了引用,错误消失了,然后我再次构建了解决方案。出现了同样的错误信息。
我不明白为什么会这样。即使我已经彻底按照链接并从头开始再次准备解决方案,但每次都会出现相同的错误消息。
现在按照 toadflakz 的回答解决了上述问题。 现在,我的服务已正确安装,但在 OnStart 时出现以下异常。
Service cannot be started. System.InvalidOperationException: Service 'WcfServiceLibrary1.WCFService' has zero application (non-infrastructure) endpoints. This might be because no configuration file was found for your application, or because no service element matching the service name could be found in the configuration file, or because no endpoints were defined in the service element.
at System.ServiceModel.Description.DispatcherBuilder.EnsureThereAreApplicationEndpoints(ServiceDescription description)
at System.ServiceModel.Description.DispatcherBuilder.InitializeServiceHost(ServiceDescription description, ServiceHostBase serviceHost)
at System.ServiceModel.ServiceHostBase.InitializeRuntime()
at System.ServiceModel.ServiceHostBase.OnOpen(TimeSpan timeout)
at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
at System.ServiceProcess.ServiceBase.ServiceQueuedMainCallback(Object state)
我尝试了很多选择,但我真的很挣扎。请帮我解决这个问题,以便我可以成功启动我的服务。
【问题讨论】:
-
编译后的 wcf dll 是否在可执行文件的 bin 目录中?
-
尝试添加对您的库项目的引用。
标签: c# .net windows wcf service