[英]WCF Service hosted on IIS is not working
I want to build a service that exposes a basicHTTP endpoint and a webHTTP Endpoint. 我想构建一个公开basicHTTP端点和webHTTP端点的服务。 If i test the following project with VS2010 in running mode everything is fine;
如果我在运行模式下用VS2010测试以下项目,一切都很好; but i want to host the service in IIS (local or remotely) and the tests to pass.
但我想在IIS(本地或远程)和测试通过主机服务。
Service.svc: Service.svc:
<%@ ServiceHost Language="C#" Debug="true" Service="ContactLibrary.ContactLibraryService"%>
I host my web site into local IIS. 我将我的网站托管到本地IIS。 When i try : http://localhost/ContactLibrary2.0/Service.svc i get :
当我尝试: http://localhost/ContactLibrary2.0/Service.svc我得到:
The type 'ContactLibrary.ContactLibraryService', provided as the Service attribute value in the ServiceHost directive, or provided in the configuration element system.serviceModel/serviceHostingEnvironment/serviceActivations could not be found.
无法找到“ContactLibrary.ContactLibraryService”类型,它作为ServiceHost指令中的Service属性值提供,或者在配置元素system.serviceModel / serviceHostingEnvironment / serviceActivations中提供。
Web.config looks like : 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 looks like : 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);
}
In your svc file you need to associate the code behind as well as shown below: 在您的svc文件中,您需要关联后面的代码以及如下所示:
<%@ ServiceHost Language="C#" Debug="true" Service="ContactLibraryNamespace.ContactLibrarySOAPService" CodeBehind="ContactLibrarySOAPService.svc.cs" %>
You dont need to have seperate classes for using BasicHttpBinding and webHttpBinding. 您不需要使用单独的类来使用BasicHttpBinding和webHttpBinding。
Just change your IContact interface to the below: 只需将您的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);
}
Then change your service element 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>
Doing so would expose your interface IContact accessible via SOAP and REST. 这样做会暴露您的接口IContact可通过SOAP和REST访问。
The only change would be to the REST endpoint url which would be http://localhost/virtualDirectoryname/ContactLibrarySOAPService.svc/rest/resourcename
唯一的变化是REST端点url,它将是
http://localhost/virtualDirectoryname/ContactLibrarySOAPService.svc/rest/resourcename
NOTE: Change the name of the class that implements IContact so as to make it generic rather than having the word SOAP or REST to avoid confusion. 注意:更改实现IContact的类的名称,以使其通用,而不是使用SOAP或REST一词来避免混淆。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.