【问题标题】:Method must have a return type WCF Service方法必须具有返回类型 WCF 服务
【发布时间】:2013-10-16 10:43:10
【问题描述】:

我正在使用 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>

而 service1.cs 文件的 C# 代码是

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() 中遇到错误,方法必须有返回类型

我不明白为什么会出现此错误。我现在在 WCF 中,我通过 msdn 中提供的信息做到了这一点。

【问题讨论】:

    标签: c# wcf tcp windows-services


    【解决方案1】:

    我想你想声明构造函数:

    public Service1()
    {
        InitializeComponent();
    }
    

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

    public WCFServiceHost1()
    {
        InitializeComponent();
    }
    

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

    【讨论】:

      猜你喜欢
      • 2015-05-16
      • 2012-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多