简体   繁体   中英

For use WCF and create .svc is necessary a web application or in simple ASP.NET website run the .svc

Because in website i retrieve from .svc

The type 'TaskService', provided as the Service attribute value in the ServiceHost directive could not be found.

And not in web application

[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;
    }
}

And the config file:

<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>

It doesn't matter if it is a Web or a WCF app. The important thing is that you reference System.ServiceModel , that is the core assembly of WCF. First of all, you need to create an interface to be your service specification:

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

And then, implement it:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM