簡體   English   中英

IIS上托管的WCF服務無法正常工作

[英]WCF Service hosted on IIS is not working

我想構建一個公開basicHTTP端點和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一詞來避免混淆。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM