方法必须具有返回类型WCF服务

时间:2013-10-16 10:43:11

标签: c# wcf tcp windows-services

我正在使用WCF服务库创建Windows服务,以便在PIC32微控制器和Windows平台之间开发基于TCP的通信来发送和接收数据。

我的app.config文件是

  <?xml version="1.0" encoding="utf-8" ?>
<configuration>

  <system.web>
    <compilation debug="true" />
  </system.web>
  <!-- When deploying the service library project, the content of the config file must be added to the host's 
  app.config file. System.Configuration does not support config files for libraries. -->
  <system.serviceModel>
    <services>
      <service behaviorConfiguration="WcfServiceLibrary1.Service1Behavior" name="WcfServiceLibrary1.Service1">
        <endpoint address="" binding="netTcpBinding" bindingConfiguration=""
          contract="WcfServiceLibrary1.IService1">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexTcpBinding" bindingConfiguration=""
          contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="net.tcp://localhost:8523/Service1" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="WcfServiceLibrary1.Service1Behavior">
          <serviceMetadata httpGetEnabled="false" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

</configuration>

C#Codefor service1.cs文件是

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.ServiceModel;
using WcfServiceLibrary1;

namespace WindowsService1
{
    public partial class Service1 : ServiceBase
    {
        internal static ServiceHost myServiceHost = null;
        public WCFServiceHost1()
        {
            InitializeComponent();
        }

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

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

现在我在public WCFServiceHost1()收到 Method必须有返回类型的错误。

我不知道为什么我会遇到这个错误。我现在在WCF,我通过msdn。

中提供的信息做到了这一点

1 个答案:

答案 0 :(得分:3)

我想你想要声明构造函数:

public Service1()
{
    InitializeComponent();
}

但是,您已经声明了应该具有返回类型的方法(它也可能是无效的):

public WCFServiceHost1()
{
    InitializeComponent();
}

总结它是否是构造函数,它应该是public Service1()(与类型名称相同),如果它是一个方法,它应该是public void WCFServiceHost1()