WCF service hosted on IIS not running

I want to create a service that provides basicHTTP endpoint and webHTTP endpoint. If I test the following project with VS2010 in live mode, everything is fine; but I want to host the service in IIS (locally or remotely) and pass the tests.

Service.svc:

<%@ ServiceHost Language="C#" Debug="true" Service="ContactLibrary.ContactLibraryService"%>

      

I am hosting my website in local IIS. When I try: http: //localhost/ContactLibrary2.0/Service.svc I get:

The type 'ContactLibrary.ContactLibraryService' supplied as the value for the Service attribute in the ServiceHost directive or supplied in the system.serviceModel / serviceHostingEnvironment / serviceActivations configuration element was not found.

The web.config looks like this:

<?xml version="1.0"?>
<configuration>    
  <system.web>
    <compilation debug="false" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <services>
      <service name="ContactLibraryNamespace.ContactLibraryService">
        <endpoint address="" binding="basicHttpBinding" bindingConfiguration=""
          name="soap" contract="ContactLibraryNamespace.IContact" />
        <endpoint address="mex" binding="mexHttpBinding" bindingConfiguration=""
          name="mex" contract="IMetadataExchange" />
        <endpoint address="rest" behaviorConfiguration="web" binding="webHttpBinding"
          bindingConfiguration="" name="rest" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost/ContactLibrary2.0" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <endpointBehaviors>
        <behavior name="web">
          <webHttp />
          <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>      
</configuration>

      

IContact looks like this:

[ServiceContract]
    public interface IContact
    {
        [OperationContract]
        [WebInvoke(Method = "GET", UriTemplate = "GetContact/{idContact}", ResponseFormat = WebMessageFormat.Json)]
        Contact GetContact(string idContact);
        [OperationContract]
        [WebInvoke(Method = "POST", UriTemplate = "AddContact", RequestFormat = WebMessageFormat.Json)]
        string AddContact(Contact contact);
        [OperationContract]
        [WebInvoke(Method = "POST", UriTemplate = "EditContact", RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
        string EditContact(string idContact, Contact Contact);
        [OperationContract]
        [WebInvoke(Method = "POST", UriTemplate = "DeleteContact", RequestFormat = WebMessageFormat.Json)]
        string DeleteContact(string idContact);
        [OperationContract]
        [WebInvoke(Method = "GET", UriTemplate = "GetAllContacts/{start}/{end}", RequestFormat = WebMessageFormat.Json)]
        List<Contact> GetAllContacts(string start, string end);        
    }

      

+3


source to share


1 answer


In your svc file, you need to link the code as shown below:

<%@ ServiceHost Language="C#" Debug="true" Service="ContactLibraryNamespace.ContactLibrarySOAPService" CodeBehind="ContactLibrarySOAPService.svc.cs" %>

      

You don't need to have separate classes to use BasicHttpBinding and webHttpBinding.

Just change your IContact interface to below:

[ServiceContract]
    public interface IContact
    {
        [OperationContract]
        [WebInvoke(Method="GET", UriTemplate = "GetContact/{idContact}", ResponseFormat=WebMessageFormat.Json)]
        Contact GetContact(string idContact);
        [OperationContract]
        [WebInvoke(Method = "POST", UriTemplate = "AddContact", RequestFormat = WebMessageFormat.Json)]
        string AddContact(Contact contact);
        [OperationContract]
        [WebInvoke(Method = "POST", UriTemplate = "EditContact", RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
        string EditContact(string idContact, Contact Contact);
        [OperationContract]
        [WebInvoke(Method = "POST", UriTemplate = "DeleteContact", RequestFormat = WebMessageFormat.Json)]
        string DeleteContact(string idContact);
        [OperationContract]
        [WebInvoke(Method = "GET", UriTemplate = "GetAllContacts/{start}/{end}", RequestFormat = WebMessageFormat.Json)]
        List<Contact> GetAllContacts(string start, string end);        
    }

      



Then change your service item in your config to:

<system.serviceModel>
    <services>
      <service name="ContactLibraryNamespace.ContactLibrarySOAPService">
        <endpoint address="" binding="basicHttpBinding" bindingConfiguration=""
         contract="ContactLibraryNamespace.IContact" />
        <endpoint address="rest" behaviorConfiguration="web" binding="webHttpBinding" bindingConfiguration="" contract="ContactLibraryNamespace.IContact" />
        <endpoint address="mex" binding="mexHttpBinding" bindingConfiguration=""
         contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost/ContactLibrary2.0" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
       <endpointBehaviors>
        <behavior name="web">
          <webHttp />
          <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
        </behavior>
        <behavior name="json">
          <enableWebScript />
          <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>

      

This will provide access to your IContact interface via SOAP and REST.

The only change will have the REST endpoint url, which will be http://localhost/virtualDirectoryname/ContactLibrarySOAPService.svc/rest/resourcename

NOTE. Change the name of the class that implements IContact to make it generic and not have the word SOAP or REST to avoid confusion.

+2


source







All Articles