我可以在一个窗口服务承载WCF服务? [英] Can I host a WCF Service in a windows service?

查看:162
本文介绍了我可以在一个窗口服务承载WCF服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个WCF项目通过去添加新项目 - > WCF服务库,当我在开发环境中运行它,它开辟了WCF测试客户端。如何安装,不安装Visual Studio中的服务器上运行此服务(我想没有承载它在IIS)。我应该写一个新的Windows服务?

I created a WCF project by going to Add New Project -> WCF service library and when I run it on the development environment, it opens up the WCF test client. How do I install this service on a server that doesn't have Visual Studio installed (I'd like to not host it on IIS). Should I write a new windows service?

推荐答案

创建一个Windows服务项目。

Create a Windows Service project.

添加您的WCF服务这个项目。

Add your WCF Service to this project.

在Windows主服务类(默认为Service1.cs),添加成员:

In the main Windows Service class (defaults to Service1.cs), add a member:

internal static ServiceHost myServiceHost = null;

修改的OnStart()来启动一个新的ServiceHost与WCF服务类型:

Modify OnStart() to start a new ServiceHost with your WCF Service type:

 protected override void OnStart(string[] args)
    {
        if (myServiceHost != null)
        {
            myServiceHost.Close();
        }

        myServiceHost = new ServiceHost(typeof(MyService));
        myServiceHost.Open();
    }

修改的onStop():

Modify OnStop():

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

添加一个安装和部署项目(安装项目)添加到您的解决方案。该项目的设置输出为Windows服务项目的主要输出。当您生成安装和部署项目,你会看到一个Setup.exe文件,你可以用它来安装服务。

Add a Setup and Deployment project (Setup Project) to your solution. Set output of that project to be the main output of the Windows Service project. When you build the Setup and Deployment project, you should see a Setup.exe file that you can use to install the service.

请记住,您还需要设置你的端点和绑定。考虑使用NetTcpBinding的这个设置。

Keep in mind you still need to setup your endpoints and bindings. Look into using nettcpbinding for this setup.

最后一点,参考:<一href="http://stackoverflow.com/questions/4267051/error-5-access-denied-when-starting-windows-service/5207840#5207840">Error 5:访问启动Windows服务的时候,如果你在安装后启动Windows服务时遇到问题,被拒绝

As a final note, reference: Error 5 : Access Denied when starting windows service if you are experiencing problems when starting the Windows Service after installation.

这篇关于我可以在一个窗口服务承载WCF服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆