在Windows服务下使WCF服务工作的问题

时间:2013-03-08 21:30:39

标签: wcf

我正在开发一个需要在Windows服务下工作的wcf服务。 wcf服务可以根据请求以SOAP或Rest方式处理请求。目前,我仍然坚持让Windows服务下的wcf服务工作。我搜索过,我发现大多数文章描述了使用tcp.net在Windows服务中运行wcf服务。但是,我无法让它发挥作用。

1)我想知道在wcf项目属性中,我应该使用哪个选项(ASP.NET开发服务器,使用本地IIS Express或使用自定义Web服务器)用于“Web”选项卡下的“服务器”部分为了在窗口服务下运行wcf服务?

2)我知道我需要将以下xml内容从我的wcf项目复制到Windows服务项目,但应该从app.config文件中进行一些更改。我想知道我需要改变什么才能让它在Windows服务下工作这是我在wcf服务项目中的app.config文件:

    <?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5">
      <assemblies>
        <add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      </assemblies>
    </compilation>
    <httpRuntime targetFramework="4.5" />
  </system.web>
  <system.serviceModel>
    <services>
      <service behaviorConfiguration="ServBehave" name="iPhysio_Web_Service.Service1">
        <endpoint address="soapService" binding="basicHttpBinding" contract="iPhysio_Web_Service.IService" />
        <endpoint address="XMLService" behaviorConfiguration="restPoxBehavior"
          binding="webHttpBinding" contract="iPhysio_Web_Service.IService" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:10917/iPhysio_Web_Service" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ServBehave">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <!--Behavior for the REST endpoint for Help enability-->
        <behavior name="restPoxBehavior">
          <webHttp helpEnabled="true" />
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
    <!--
        To browse web app root directory during debugging, set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
      -->
    <directoryBrowse enabled="true" />
  </system.webServer>
  <connectionStrings>
    <add name="iPhysioAndroidDBEntities" connectionString="metadata=res://*/Model.csdl|res://*/Model.ssdl|res://*/Model.msl;provider=System.Data.SQLite;provider connection string='data source=&quot;C:\Users\Siu W\Desktop\iPhysio_Git\IPhysio\iPhysio_Web_Service\data\iPhysioAndroidDB&quot;'" providerName="System.Data.EntityClient" />
  </connectionStrings>
</configuration>

这是我的Windows服务代码:

public partial class Service1 : ServiceBase
{
    private ServiceHost myHost = null;
    private BackgroundWorker worker;
    public Service1()
    {
        InitializeComponent();
        this.ServiceName = "iPhysio_Web_Service";
    }


    protected override void OnStart(string[] args)
    {
        worker = new BackgroundWorker();
       worker.DoWork += new DoWorkEventHandler(worker_DoWork);

    }
    void worker_DoWork(object sender, DoWorkEventArgs e)
    {
        if (myHost != null)
        {
            myHost.Close();
        }

        myHost = new ServiceHost(typeof(iPhysio_Web_Service.Service1));
        myHost.Open();
    }
    protected override void OnStop()
    {
        if (myHost != null)
        {
            myHost.Close();
            myHost = null;
        }
    }
}

提前感谢您的任何帮助。

查尔斯

0 个答案:

没有答案