有关WCF的问题IService.cs:using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;namespace Services
{
    [ServiceContract]
    public interface IServiceCallBack
    {
        [OperationContract(IsOneWay = true)]
        void ReceiveMessage(string msg);
    }    [ServiceContract(CallbackContract = typeof(IServiceCallBack))]
    public interface IService
    {
        [OperationContract(IsOneWay = true)]
        void GetMessage();
    } 
}
Service.cs:using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
using System.Threading;namespace Services
{
    [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)]
    public class Service : IService
    {
        IServiceCallBack callback;
        Timer heartTimer;          public void GetMessage()
        {
            callback = OperationContext.Current.GetCallbackChannel<IServiceCallBack>();
            heartTimer = new Timer(new TimerCallback(heartTimer_Elapsed), null, 5000, Timeout.Infinite);
        }        private void heartTimer_Elapsed(object data)
        {
            if (((ICommunicationObject)callback).State == CommunicationState.Opened)
            {
                callback.ReceiveMessage("当前时间:" + DateTime.Now.ToString());
            }
            else
            {
                heartTimer.Change(Timeout.Infinite, 0);
            }
        }
    }
}宿主:using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Services; //导入WCF服务库项目命名空间
using System.ServiceModel;//导入WCF服务命名空间namespace Host
{
    public class Program 
    {
        public static void Main(string[] args)
        {
            try
            {
                ServiceHost host = new ServiceHost(typeof(Service));
                host.Open();
                Console.WriteLine("服务已启动......");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.ReadLine();
            //host.Close();
        }
    }
}宿主App.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="NewBehavior">
          <serviceMetadata />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service behaviorConfiguration="NewBehavior" name="Services.Service">
        <clear />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:1041/Service" />
          </baseAddresses>
        </host>
        <endpoint address="" binding="wsDualHttpBinding" contract="Services.IService"
            listenUriMode="Explicit">
        </endpoint>
        <endpoint address="net.tcp://localhost:1042/Service" binding="netTcpBinding"
            contract="Services.IService" listenUriMode="Explicit">
        </endpoint>
        <endpoint address="mex" binding="basicHttpBinding" bindingConfiguration=""
            contract="IMetadataExchange">
        </endpoint>
      </service>
    </services>
  </system.serviceModel>
</configuration>

解决方案 »

  1.   

    这个在本地可以正常运行但放上服务器后,就有问题:提示
    Service 'Services.Service' 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 config
    uration file, 
    or because no endpoints were defined in the service element.