2012-03-28 208 views
3

我想構建一個公開基本HTTP端點和WebHTTP端點的服務。如果我在運行模式下用VS2010測試下面的項目,一切都很好;但我想在IIS(本地或遠程)託管服務並通過測試。在IIS上託管的WCF服務不起作用

Service.svc:

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

我主持我的網站到本地IIS。當我嘗試:http://localhost/ContactLibrary2.0/Service.svc我得到:

類型「ContactLibrary.ContactLibraryService」,作爲服務提供了ServiceHost的指令屬性值,或在配置元素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);   
    } 
+0

你可以發佈你的IContact接口嗎? – 2012-03-28 07:59:36

+0

一切看起來不錯。但是我沒有看到ContactLibraryJSONService的端點。你有沒有定義這樣的端點?如果不是,則應添加它。 – paramosh 2012-03-28 08:29:16

+0

我沒有添加一個,因爲我想看到這部分工作。我想看看我的服務對WCFStorm有反應。 – 2012-03-28 08:32:14

回答

2

在您的SVC文件,你需要後面的代碼以及相關聯,如下所示:

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

你不需要單獨使用Basi類cHttpBinding和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);   
    } 

然後在你的配置更改service元素:

<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,以避免混亂的類的名稱。

+0

<行爲名稱= 「JSON」> \t 誰是 「JSON」?我沒有任何代碼,並且我沒有* .svc下的任何cs類。 – 2012-03-28 08:47:03

+0

而不是使用後面的代碼我使用對我的C#項目的引用。這是一個錯誤的/壞的東西? – 2012-03-28 09:10:26

+0

我編輯我的問題與最新的編輯/錯誤到我的項目。 – 2012-03-28 09:34:31