2009-10-25 67 views
0

我是WCF新手,正在開發一個項目 ,我正在構建一個Web項目(ASP.NET 2.03.5/C#),名爲 的WCF庫。從Web項目調用服務Lbrary - 什麼在Web.Config?

我在這裏有幾件事情。

1)我爲我的JSON Web方法爲我的 SOAP合同(ISOAP)提供了一個恭維合同(IJSON)。 2)我有一個合同,IFileTransferService,處理 文件流。

能否請你看看我的app.config和WEB.CONFIG 文件,並告訴我,如果他們看起來是正確的,或者如果我再生 認爲這是在我的App.Config中在我的web.config不必要的? 如果我使用服務庫,我真的需要做兩次事情(聲明服務,綁定等)嗎?這看起來是 對不對?

此外,任何其他文體/建設性意見有關 什麼我一邊system.serviceModel總是讚賞。

謝謝你的時間。

WEB.CONFIG

<system.serviceModel> 
    <client> 
     <endpoint name="MySOAPAuthEP" 
       address="http://localhost:1241/WebProj/auth.svc/soap" 
       binding="basicHttpBinding" 
       bindingConfiguration="soapWeb" 
       contract="Project.WebAPI.Authentication.ISOAPAuthService" /> 
     <endpoint name="MySOAPTradeEP" 
       address="http://localhost:1241/WebProj/trade.svc/soap" 
       binding="basicHttpBinding" 
       bindingConfiguration="soapWeb" 
       contract="Project.WebAPI.Trade.ISOAPTradeService" /> 
     <endpoint name="MySOAPFileTransferEP" 
       address="http://localhost:1241/WebProj/filetransfer.svc/soap" 
       binding="basicHttpBinding" 
       bindingConfiguration="httpLargeDataStream" 
       contract="Project.WebAPI.FileTransfer.ISOAPFileTransferService" /> 

     <endpoint name="MyJSONAuthEP" 
       binding="webHttpBinding" 
       bindingConfiguration="jsonWeb" 
       contract="Project.WebAPI.Authentication.IJSONAuthService" /> 
     <endpoint name="MyJSONTradeEP" 
       binding="webHttpBinding" 
       bindingConfiguration="jsonWeb" 
       contract="Project.WebAPI.Trade.IJSONTradeService" /> 
     <endpoint name="MyJSONFileTransferEP" 
       binding="webHttpBinding" 
       bindingConfiguration="jsonWeb" 
       contract="Project.WebAPI.FileTransfer.IJSONFileTransferService" /> 
    </client> 
    <bindings> 
     <basicHttpBinding> 
     <binding name="soapWeb" /> 
     <binding name="httpLargeDataStream" closeTimeout="00:01:00" openTimeout="00:01:00" 
      receiveTimeout="00:10:00" sendTimeout="00:01:00" maxBufferSize="65536" 
      maxReceivedMessageSize="2147483647" messageEncoding="Mtom" transferMode="Streamed"> 
      <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" 
      maxBytesPerRead="4096" maxNameTableCharCount="16384" /> 
      <security mode="None"> 
      <transport clientCredentialType="None" /> 
      </security> 
     </binding> 
     </basicHttpBinding> 
     <webHttpBinding> 
     <binding name="jsonWeb" maxBufferSize="1500000" maxBufferPoolSize="1500000" 
      maxReceivedMessageSize="1500000"> 
      <readerQuotas maxDepth="32" maxStringContentLength="656000" maxArrayLength="656000" 
      maxBytesPerRead="656000" maxNameTableCharCount="656000" /> 
     </binding> 
     </webHttpBinding> 
    </bindings> 
    <behaviors> 
     <endpointBehaviors> 
     <behavior name="WebHttpEPBehavior"> 
      <webHttp /> 
     </behavior> 
     <behavior name="BasicHttpEPBehavior" /> 
     </endpointBehaviors> 
     <serviceBehaviors> 
     <behavior name="Default"> 
      <serviceMetadata httpGetEnabled="true" /> 
      <serviceDebug includeExceptionDetailInFaults="true" /> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/> 
    <services> 
     <service behaviorConfiguration="Default" name="Project.WebAPI.Trade.TradeService"> 
     <endpoint address="json" behaviorConfiguration="WebHttpEPBehavior" 
      binding="webHttpBinding" name="MyJSONTradeEP" contract="Project.WebAPI.Trade.IJSONTradeService" /> 
     <endpoint address="soap" binding="basicHttpBinding" name="MySOAPTradeEP" 
      contract="Project.WebAPI.Trade.ISOAPTradeService" /> 
     <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> 
     <host> 
      <baseAddresses> 
      <add baseAddress="http://localhost:1243/WebProj/trade.svc" /> 
      </baseAddresses> 
     </host> 
     </service> 
     <service behaviorConfiguration="Default" name="Project.WebAPI.Authentication.AuthService"> 
     <endpoint address="json" behaviorConfiguration="WebHttpEPBehavior" 
      binding="webHttpBinding" name="MyJSONAuthEP" contract="Project.WebAPI.Authentication.IJSONAuthService" /> 
     <endpoint address="soap" binding="basicHttpBinding" name="MySOAPAuthEP" 
      contract="Project.WebAPI.Authentication.ISOAPAuthService" /> 
     <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> 
     <host> 
      <baseAddresses> 
      <add baseAddress="http://localhost:1242/WebProj/auth.svc" /> 
      </baseAddresses> 
     </host> 
     </service> 
     <service behaviorConfiguration="Default" name="Project.WebAPI.FileTransfer.FileTransferService"> 

     <endpoint address="json" behaviorConfiguration="WebHttpEPBehavior" 
        binding="webHttpBinding" name="MyJSONFileTransferEP" contract="Project.WebAPI.FileTransfer.IJSONFileTransferService" /> 

     <endpoint address="soap" 
        binding="basicHttpBinding" 
        name="MySOAPFileTransferEP" 
        bindingConfiguration="httpLargeDataStream" 
        contract="Project.WebAPI.FileTransfer.ISOAPFileTransferService" /> 

     <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> 
     <host> 
      <baseAddresses> 
      <add baseAddress="http://localhost:1244/WebProj/filetransfer.svc" /> 
      </baseAddresses> 
     </host> 
     </service> 
    </services> 
    </system.serviceModel> 

的app.config

<system.serviceModel> 
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" /> 
    <bindings> 
     <basicHttpBinding> 
     <binding name="soapWeb" /> 
     <binding name="httpLargeDataStream" 
       maxReceivedMessageSize="2147483647" 
       closeTimeout="00:01:00" 
       openTimeout="00:01:00" 
       receiveTimeout="00:10:00" 
       sendTimeout="00:01:00" 
       transferMode="Streamed" 
       messageEncoding="Mtom" 
       maxBufferSize="65536"> 
      <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" 
       maxBytesPerRead="4096" maxNameTableCharCount="16384" /> 
      <security mode="None"> 
      <transport clientCredentialType="None"/> 
      </security> 
     </binding> 
     </basicHttpBinding> 
     <webHttpBinding> 
     <binding name="jsonWeb" maxBufferSize="1500000" maxBufferPoolSize="1500000" 
      maxReceivedMessageSize="1500000"> 
      <readerQuotas maxDepth="32" maxStringContentLength="656000" maxArrayLength="656000" 
      maxBytesPerRead="656000" maxNameTableCharCount="656000" /> 
     </binding> 
     </webHttpBinding> 
    </bindings> 
    <services> 
     <service behaviorConfiguration="Default" name="Project.WebAPI.Trade.TradeService"> 
     <endpoint address="json" behaviorConfiguration="WebHttpEPBehavior" 
      binding="webHttpBinding" name="MyJSONTradeEP" contract="Project.WebAPI.Trade.IJSONTradeService" /> 
     <endpoint address="soap" binding="basicHttpBinding" name="MySOAPTradeEP" 
      contract="Project.WebAPI.Trade.ISOAPTradeService" /> 
     <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> 
     <host> 
      <baseAddresses> 
      <add baseAddress="http://localhost:1243/WebProj/trade.svc" /> 
      </baseAddresses> 
     </host> 
     </service> 
     <service behaviorConfiguration="Default" name="Project.WebAPI.Authentication.AuthService"> 
     <endpoint address="json" behaviorConfiguration="WebHttpEPBehavior" 
      binding="webHttpBinding" name="MyJSONAuthEP" contract="Project.WebAPI.Authentication.IJSONAuthService" /> 
     <endpoint address="soap" binding="basicHttpBinding" name="MySOAPAuthEP" 
      contract="Project.WebAPI.Authentication.ISOAPAuthService" /> 
     <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> 
     <host> 
      <baseAddresses> 
      <add baseAddress="http://localhost:1242/WebProj/auth.svc" /> 
      </baseAddresses> 
     </host> 
     </service> 
     <service behaviorConfiguration="Default" name="Project.WebAPI.FileTransfer.FileTransferService"> 
     <endpoint address="json" behaviorConfiguration="WebHttpEPBehavior" 
        binding="webHttpBinding" name="MyJSONFileTransferEP" contract="Project.WebAPI.FileTransfer.IJSONFileTransferService" /> 

     <endpoint address="soap" binding="basicHttpBinding" name="MySOAPFileTransferEP" 
        contract="Project.WebAPI.FileTransfer.ISOAPFileTransferService" /> 

     <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> 
     <host> 
      <baseAddresses> 
      <add baseAddress="http://localhost:1244/WebProj/filetransfer.svc" /> 
      </baseAddresses> 
     </host> 
     </service> 
    </services> 
    <behaviors> 
     <endpointBehaviors> 
     <behavior name="WebHttpEPBehavior"> 
      <webHttp /> 
     </behavior> 
     <behavior name="BasicHttpEPBehavior" /> 
     </endpointBehaviors> 
     <serviceBehaviors> 
     <behavior name="Default"> 
      <serviceMetadata httpGetEnabled="true" /> 
      <serviceDebug includeExceptionDetailInFaults="true" /> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    </system.serviceModel> 

回答

0

若干意見,以幫助簡化房間您的配置文件,使他們更具可讀性:

  • 你有兩個<services>爲以及web.config和app.config中的<client>部分。爲什麼?這通常不是必需的 - 服務器端(您的服務器代碼運行時)需要<services>,而調用服務器的客戶端只需要<client>部分
  • 看起來您已經使用Visual Studio或svcutil.exe來創建你的配置,因爲你的綁定配置也列出了所有的綁定默認值(這是你擁有的大量設置)。您可以減少這種情況,只留下針對特定場景實際更改的設置,從而使配置變得更小,更易於理解
  • 您真的需要<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>設置嗎?這基本上「僞造」了WCF服務中的HttpContext,使它們的外觀和行爲非常像ASMX webservices - 你需要這個嗎?
  • 這裏該設置<serviceDebug includeExceptionDetailInFaults="true" />可能是一個開發/測試環境有益的和好,但我肯定會推薦用於生產
  • 刪除它是你的自我託管,或在IIS託管?如果您使用IIS來託管,則不會使用定義的<baseAddress>條目;在IIS託管環境中,由於* .svc文件(它所在的虛擬目錄)的位置是您的基址,因此您無法覆蓋該基址,因此基址將被忽略。

就是這樣乍一看:-)

Marc

+0

謝謝Marc。我遵循你的建議,只是離開部分在我的web.config,但現在似乎無法讓它運行。我收到以下錯誤:服務'AuthService'具有零應用程序(非基礎設施)端點。這可能是因爲沒有爲您的應用程序找到配置文件,或者因爲在配置文件中找不到匹配服務名稱的服務元素,或者因爲服務元素中沒有定義端點。我記得當我開始並通過在Web.config中轉儲服務和綁定來解決它時,我遇到了這個錯誤。想法?再次感謝。 – 2009-10-26 07:10:34

+0

你在哪裏舉辦你的服務課程? IIS?然後,您需要在訪問該服務的客戶端app.config中的web.config和您的''(但不包括「services」) – 2009-10-26 08:40:52

+0

再次感謝。不,我現在正在自主發展。 – 2009-10-26 15:13:49