В вашем файле SVC вам нужно связать код, как показано ниже:
<%@ ServiceHost Language="C#" Debug="true" Service="ContactLibraryNamespace.ContactLibrarySOAPService" CodeBehind="ContactLibrarySOAPService.svc.cs" %>
Вам не нужно иметь отдельные классы для использования BasicHttpBinding и webHttpBinding.
Просто изменитеваш интерфейс IContact приведен ниже:
[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);
}
Затем измените ваш элемент службы в вашей конфигурации на:
<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>
Это сделает ваш интерфейс IContact доступным через SOAP и REST.
Единственным изменением будет URL-адрес конечной точки REST, который будет http://localhost/virtualDirectoryname/ContactLibrarySOAPService.svc/rest/resourcename
ПРИМЕЧАНИЕ. Измените имя класса, реализующего IContact, чтобы сделать его универсальным, а не иметь слово SOAP илиОТДЫХ, чтобы избежать путаницы.