我在建立一个WCF服务时,遇到个怪问题,我基本是照网上的例子做的,可运行时总报错,请高手指点一下。主要的程序有:
1、接口类:    [ServiceContract]
    public interface IStarServiceInterface
    {
        [OperationContract]
        double MyAdd(double n1, double n2);
    }
2、实现类:   public class StarService : IStarServiceInterface
    {
        public double MyAdd(double n1, double n2)
        {
            return n1 + n2;
        }
    }
3.建立服务的代码:        ServiceHost oServiceHost = new ServiceHost(typeof(StarService));        public MainForm()
        {
            InitializeComponent();            // Open the ServiceHost to create listeners         // and start listening for messages.
            try
            {
                System.ServiceModel.NetTcpBinding oBinding = new NetTcpBinding();
                oBinding.Name = "tcpbinding";
                oBinding.CloseTimeout = new TimeSpan(600);
                oBinding.OpenTimeout = new TimeSpan(600);
                oBinding.ReceiveTimeout = new TimeSpan(600);
                oBinding.SendTimeout = new TimeSpan(600);
                oBinding.TransactionFlow = false;   //是否支持分布式事务
                oBinding.TransferMode = TransferMode.Buffered;
                oBinding.TransactionProtocol = TransactionProtocol.OleTransactions;
                oBinding.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
                oBinding.ListenBacklog = 10;
                oBinding.MaxBufferPoolSize = 524288;
                oBinding.MaxBufferSize = 65536;
                oBinding.MaxConnections = 10;
                oBinding.MaxReceivedMessageSize = 65536;
                oBinding.Security.Mode = SecurityMode.None; //不需要证书
                oBinding.Security.Transport.ClientCredentialType = TcpClientCredentialType.Certificate;
                oBinding.ReliableSession.Ordered = true;
                oBinding.ReliableSession.InactivityTimeout = new TimeSpan(6000);
                oBinding.ReliableSession.Enabled = false;                System.Xml.XmlDictionaryReaderQuotas oReaderQuotas = new XmlDictionaryReaderQuotas();
                oReaderQuotas.MaxArrayLength = int.MaxValue;
                oReaderQuotas.MaxStringContentLength = int.MaxValue;
                oReaderQuotas.MaxBytesPerRead = int.MaxValue;                oBinding.ReaderQuotas = oReaderQuotas;                oServiceHost.AddServiceEndpoint(typeof(StarService), oBinding, "net.tcp://localhost:8999/StarService");                oServiceHost.Open();                SystemMessage.AppendText("服务建立成功...\r\n");
            }
            catch (Exception ex)
            {
                SystemMessage.AppendText("无法建立服务...\r\n");
            }
        }
问题是,运行到语句
oServiceHost.AddServiceEndpoint(typeof(StarService), oBinding, "net.tcp://localhost:8999/StarService");
时,总报错,
协定类型 Star.WinFormServer.StarService 不具有 ServiceContractAttribute 特性。若要定义有效协定,指定的类型(协定接口或服务类)必须具有 ServiceContractAttribute请问此问题该如何解决。

解决方案 »

  1.   

    http://www.cnblogs.com/artech/archive/2007/09/15/893838.html参考
      

  2.   

    typeof(StarService)改为typeof(IStarServiceInterface)
      

  3.   

    回复楼上的,改了不行,报错:
    ServiceHost 仅支持类服务类型。
      

  4.   

    oServiceHost.AddServiceEndpoint(typeof(StarService),  
    这个地方改不是让你改这里  ServiceHost oServiceHost = new ServiceHost(typeof(StarService));
      

  5.   

    多谢楼上的,这样改的话,服务的确建立了。但又有了新的问题,客户端调用时报错:
    打开操作没有在分配的超时 00:00:00.0600000 内完成。分配给此操作的时间可能已经是更长超时的一部分。客户端的代码如下:
    1、接口:   [ServiceContract]
        public interface ServiceInterface
        {
            [OperationContract]
            double MyAdd(double n1, double n2);
        }
    2、建立频道:private ServiceInterface ssProxy;
    ....
          private void ResetProxy()
            {
                NetTcpBinding oBinding = new NetTcpBinding();
                oBinding.Name = "tcpbinding";
                oBinding.CloseTimeout = new TimeSpan(600000);
                oBinding.OpenTimeout = new TimeSpan(600000);
                oBinding.ReceiveTimeout = new TimeSpan(600000);
                oBinding.SendTimeout = new TimeSpan(600000);
                oBinding.TransactionFlow = false;   //是否支持分布式事务
                oBinding.TransferMode = TransferMode.Buffered;
                oBinding.TransactionProtocol = TransactionProtocol.OleTransactions;
                oBinding.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
                oBinding.ListenBacklog = 10;
                oBinding.MaxBufferPoolSize = 524288;
                oBinding.MaxBufferSize = 2147483647;
                oBinding.MaxConnections = 10;
                oBinding.MaxReceivedMessageSize = 2147483647;
                oBinding.Security.Mode = SecurityMode.None; //不需要证书
                oBinding.Security.Transport.ClientCredentialType = TcpClientCredentialType.Certificate;
                oBinding.ReliableSession.Ordered = true;
                oBinding.ReliableSession.InactivityTimeout = new TimeSpan(6000);
                oBinding.ReliableSession.Enabled = false;            System.Xml.XmlDictionaryReaderQuotas oReaderQuotas = new XmlDictionaryReaderQuotas();
                oReaderQuotas.MaxArrayLength = int.MaxValue;
                oReaderQuotas.MaxStringContentLength = int.MaxValue;
                oReaderQuotas.MaxBytesPerRead = int.MaxValue;            oBinding.ReaderQuotas = oReaderQuotas;            ServiceEndpoint nettcpEndpoint = new ServiceEndpoint(ContractDescription.GetContract(typeof(ServiceInterface)), oBinding, new EndpointAddress("net.tcp://localhost:8999/StarService"));
                ChannelFactory<ServiceInterface> factory = new ChannelFactory<ServiceInterface>(nettcpEndpoint);
                ssProxy = factory.CreateChannel();
            }这是成功的,没什么问题,3、调用时报以上错误,代码如下:textBox3.Text = ssProxy.MyAdd(Convert.ToDouble(textBox1.Text), Convert.ToDouble(textBox2.Text)).ToString();
    请高手们再指点一下。
      

  6.   

    oServiceHost  要 Open(); 才算真正运行。