对于使用WCF和create .svc是必要的Web应用程序或简单的ASP.NET网站运行.svc

时间:2010-06-08 05:23:17

标签: c# asp.net asp.net-mvc wcf silverlight

因为我在网站上从.svc

中检索

无法找到作为ServiceHost指令中的Service属性值提供的类型'TaskService'。

而不是在网络应用程序中

[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class TaskService
{
    [OperationContract]
    public List<int> GetTasks(int id, int type)
    {
        List<int> nodes = new List<int>();
            return nodes;
    }
}

配置文件:

<system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="TaskServiceBehavior">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
    <services>
      <service behaviorConfiguration="TaskServiceBehavior" name="TaskService">
        <endpoint address="" binding="basicHttpBinding" contract="TaskService"/>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
    </services>
  </system.serviceModel>

1 个答案:

答案 0 :(得分:0)

无论是Web还是WCF应用程序都无关紧要。重要的是你引用了 System.ServiceModel ,这是WCF的核心程序集。首先,您需要创建一个接口作为您的服务规范:

[ServiceContract(Namespace = "")]    
public interface ITaskService
{
    [OperationContract]
    List<int> GetTasks(int id, int type);
}

然后,实施它:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class TaskService : ITaskService
{
    public List<int> GetTasks(int id, int type)
    {
        List<int> nodes = new List<int>();
            return nodes;
    }
}