当前位置: 代码迷 >> Web Service >> WCF入门初学者有关问题
  详细解决方案

WCF入门初学者有关问题

热度:388   发布时间:2016-05-02 02:47:51.0
WCF入门菜鸟问题
我最近在学WCF,碰到一个问题,提示“ServiceHost 仅支持类服务类型。”错误,请问如何解决?

using System.ServiceModel;
namespace TestServer
{
  [ServiceContract]
  public interface ITestSevice
  {
  [OperationContract]
  int Getsum(int num1, int num2);  
  }  
}

namespace TestServer
{
  public class TestService : ITestSevice
  {
  public int Getsum(int num1, int num2)
  {
  return num1+num2;
  }
  }
}

宿主app.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
  <behaviors>
  <serviceBehaviors>
  <behavior name="sumServiceBehavior">
  <serviceMetadata httpGetEnabled="true"/>
  </behavior>
  </serviceBehaviors>
  </behaviors>
  <services>
  <service name="TestServer.TestService" behaviorConfiguration="sumServiceBehavior">
  <host>
  <baseAddresses>
  <add baseAddress="http://localhost:8089/MySumService"/>
  </baseAddresses>
  </host>
  <endpoint address="" binding="wsHttpBinding" contract="TestServer.ITestService" />
  </service>
  </services>
  </system.serviceModel>
</configuration>

宿主执行服务:
private void button1_Click(object sender, EventArgs e)
  {  
  try
  {
  ServiceHost host = new ServiceHost(typeof(TestServer.ITestSevice));//=>这句报错:ServiceHost 仅支持类服务类型。”
  host.Open();
  richTextBox1.AppendText("WCF服务已经成功启动");
  }
  catch (Exception ex)
  {
  richTextBox1.AppendText("WCF服务启动中错误:" + ex.Message);

  }
  }

执行到ServiceHost host=new ....这句报错“ServiceHost 仅支持类服务类型。”请问如何解决?

------解决方案--------------------
ServiceHost host = new ServiceHost(typeof(TestServer.TestServer));
  相关解决方案