IIS上托管的WCF服务无法正常工作

我想构建一个公开basicHTTP端点和webHTTP端点的服务。 如果我在运行模式下用VS2010测试以下项目,一切都很好; 但我想在IIS(本地或远程)和测试通过主机服务。

Service.svc:

 

我将我的网站托管到本地IIS。 当我尝试: http://localhost/ContactLibrary2.0/Service.svc我得到:

无法找到“ContactLibrary.ContactLibraryService”类型,作为ServiceHost指令中的Service属性值提供,或者在配置元素system.serviceModel / serviceHostingEnvironment / serviceActivations中提供。

Web.config看起来像:

                                       

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 GetAllContacts(string start, string end); } 

在您的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 GetAllContacts(string start, string end); } 

然后将配置中的服务元素更改为:

                                    

这样做会暴露您的接口IContact可通过SOAP和REST访问。

唯一的变化是REST端点url,它将是http://localhost/virtualDirectoryname/ContactLibrarySOAPService.svc/rest/resourcename

注意:更改实现IContact的类的名称,以使其成为通用而不是使用SOAP或REST一词来避免混淆。