IIS上承载的WCF服务不工作
本文关键字:服务 工作 WCF IIS | 更新日期: 2023-09-27 18:29:17
我想构建一个公开基本HTTP端点和webHTTP端点的服务。如果我在运行模式下用VS2010测试以下项目,一切都很好;但我想在IIS(本地或远程)中托管服务,并通过测试。
Service.svc:
<%@ ServiceHost Language="C#" Debug="true" Service="ContactLibrary.ContactLibraryService"%>
我将我的网站托管到本地IIS中。当我尝试:http://localhost/ContactLibrary2.0/Service.svc我得到:
找不到类型"ContactLibrary.ContactLibraryService",该类型在ServiceHost指令中作为Service属性值提供,或在配置元素system.serviceModel/serviceHostingEnvironment/serviceActivations中提供。
Web.config看起来像:
<?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看起来像:
[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);
}
在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.访问
唯一的更改是REST端点url,它将是http://localhost/virtualDirectoryname/ContactLibrarySOAPService.svc/rest/resourcename
注意:更改实现IContact的类的名称,使其通用,而不是使用SOAP或REST来避免混淆。