2009-08-04 4 views
1

Ich habe einige Probleme mit diesem seit ein paar Tagen jetzt ... Ich habe eine WCF-Klassenbibliothek erstellt - WebAPILibrary. Es ist unter meiner Lösung, die auch mein Webprojekt enthält.Brauchen Sie Hilfe mit AJAX-Enabled WCF-Dienste (Bitte schauen @ System.ServiceModel in Web.Config)

Ich versuche jetzt, eine AJAX/JSON-Antwort auf eine Suchanfrage zu aktivieren.

In den Client-Code-Beispiele, ich sehe etwas in der Art:

function btnsrch_onclick() { 

       var query = document.getElementById('q'); 
       var type = document.getElementById('type'); 

       $(document).ready(function(){ 
        $.getJSON("http://api.domain.com/services/trade.svc?q=" + query.value + "&type=" + type.value + "&format=json", 
         function(data){ 
          $.each(data.items, function(i,item){ 
          $("<img/>").attr("src", item.media.m).appendTo("#images"); 
           if (i == 3) return false; 
         }); 
        }); 
       }); 

       } 

Mein Problem ist, die bis zu diesem Punkt, ich habe meine WCF-Klassenbibliothek in meinem Webprojekt glücklich worden verweist. Aber jetzt scheint es, ich muss eine SVC-Datei erstellen und verwenden, um meine Javascript AJAX/JSON-Aufrufe auszuführen.

meine Frage:

1) Wie kann ich die SVC-Datei in Zusammenarbeit mit meiner WCF-Klassenbibliothek erstellen? 2) Ich bin mir nicht sicher, was ich unter System.ServiceModel für die Web.config meines Webprojekts setzen muss. Könnte eine freundliche Seele sich ansehen, was ich gerade habe und mir unter mitteilen, was ich tun muss?

Webprojekt System.ServiceModel (web.config):

<system.serviceModel> 
    <bindings> 
     <wsHttpBinding> 
     <binding name="WSHttpBinding_ITradeService" closeTimeout="00:01:00" 
      openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" 
      bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard" 
      maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text" 
      textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false"> 
      <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" 
      maxBytesPerRead="4096" maxNameTableCharCount="16384" /> 
      <reliableSession ordered="true" inactivityTimeout="00:10:00" 
      enabled="false" /> 
      <security mode="Message"> 
      <transport clientCredentialType="Windows" proxyCredentialType="None" 
       realm="" /> 
      <message clientCredentialType="Windows" negotiateServiceCredential="true" 
       algorithmSuite="Default" establishSecurityContext="true" /> 
      </security> 
     </binding> 
     <binding name="WSHttpBinding_IAuthService" closeTimeout="00:01:00" 
      openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" 
      bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard" 
      maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text" 
      textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false"> 
      <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" 
      maxBytesPerRead="4096" maxNameTableCharCount="16384" /> 
      <reliableSession ordered="true" inactivityTimeout="00:10:00" 
      enabled="false" /> 
      <security mode="Message"> 
      <transport clientCredentialType="Windows" proxyCredentialType="None" 
       realm="" /> 
      <message clientCredentialType="Windows" negotiateServiceCredential="true" 
       algorithmSuite="Default" establishSecurityContext="true" /> 
      </security> 
     </binding> 
     <binding name="WSHttpBinding_IAuthService1" closeTimeout="00:01:00" 
      openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" 
      bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard" 
      maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text" 
      textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false"> 
      <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" 
      maxBytesPerRead="4096" maxNameTableCharCount="16384" /> 
      <reliableSession ordered="true" inactivityTimeout="00:10:00" 
      enabled="false" /> 
      <security mode="Message"> 
      <transport clientCredentialType="Windows" proxyCredentialType="None" 
       realm="" /> 
      <message clientCredentialType="Windows" negotiateServiceCredential="true" 
       algorithmSuite="Default" establishSecurityContext="true" /> 
      </security> 
     </binding> 
     <binding name="WSHttpBinding_ITradeService1" closeTimeout="00:01:00" 
      openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" 
      bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard" 
      maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text" 
      textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false"> 
      <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" 
      maxBytesPerRead="4096" maxNameTableCharCount="16384" /> 
      <reliableSession ordered="true" inactivityTimeout="00:10:00" 
      enabled="false" /> 
      <security mode="Message"> 
      <transport clientCredentialType="Windows" proxyCredentialType="None" 
       realm="" /> 
      <message clientCredentialType="Windows" negotiateServiceCredential="true" 
       algorithmSuite="Default" establishSecurityContext="true" /> 
      </security> 
     </binding> 
     </wsHttpBinding> 
    </bindings> 
    <client> 
     <endpoint address="http://localhost:8731/Design_Time_Addresses/Trade/" 
     binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_ITradeService" 
     contract="ITradeService" name="WSHttpBinding_ITradeService"> 
     <identity> 
      <dns value="localhost:8731" /> 
     </identity> 
     </endpoint> 
     <endpoint address="http://localhost:8731/Design_Time_Addresses/Authentication/" 
     binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IAuthService" 
     contract="IAuthService" name="WSHttpBinding_IAuthService"> 
     <identity> 
      <dns value="localhost:8731" /> 
     </identity> 
     </endpoint> 
     <endpoint address="http://localhost:8731/Design_Time_Addresses/Authentication/" 
     binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IAuthService1" 
     contract="Trezoro.WebAPI.Authentication.IAuthService" name="WSHttpBinding_IAuthService1"> 
     <identity> 
      <dns value="localhost:8731" /> 
     </identity> 
     </endpoint> 
     <endpoint address="http://localhost:8731/Design_Time_Addresses/Trade/" 
     binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_ITradeService1" 
     contract="Trezoro.WebAPI.Trade.ITradeService" name="WSHttpBinding_ITradeService1"> 
     <identity> 
      <dns value="localhost:8731" /> 
     </identity> 
     </endpoint> 
    </client> 
    </system.serviceModel> 

WCF-Klassenbibliothek System.ServiceModel (app.config):

<system.serviceModel> 
     <serviceHostingEnvironment aspNetCompatibilityEnabled="true" /> 
     <services> 
      <service behaviorConfiguration="WebAPILibrary.WebAPIBehavior" 
      name="Company.WebAPI.Trade.TradeService"> 
      <endpoint address="" binding="wsHttpBinding" contract="Company.WebAPI.Trade.ITradeService"> 
       <identity> 
       <dns value="localhost:8731" /> 
       </identity> 
      </endpoint> 
      <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> 
      <host> 
       <baseAddresses> 
       <add baseAddress="http://localhost:8731/Design_Time_Addresses/Trade/" /> 
       </baseAddresses> 
      </host> 
      </service> 
      <service behaviorConfiguration="WebAPILibrary.WebAPIBehavior" 
      name="Company.WebAPI.Authentication.AuthService"> 
      <endpoint address="" binding="wsHttpBinding" contract="Company.WebAPI.Authentication.IAuthService"> 
       <identity> 
       <dns value="localhost:8731" /> 
       </identity> 
      </endpoint> 
      <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> 
      <host> 
       <baseAddresses> 
       <add baseAddress="http://localhost:8731/Design_Time_Addresses/Authentication/" /> 
       </baseAddresses> 
      </host> 
      </service> 
     </services> 
     <behaviors> 
      <endpointBehaviors> 
      <behavior name="WebAPILibrary.WebAPIBehavior"> 
       <enableWebScript/> 
      </behavior> 
      </endpointBehaviors> 
      <serviceBehaviors> 
      <behavior name="WebAPILibrary.WebAPIBehavior"> 
       <!-- 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="True" /> 
      </behavior> 
      </serviceBehaviors> 
     </behaviors> 
     </system.serviceModel> 

Antwort

1

Sie sollten nie verweisen Ihre WCF-Bibliothek in einem anderen Projekt. Es ist als Service gedacht, nicht als Klassenbibliothek.

+0

Danke für die Antwort. Vielleicht habe ich mich schlecht erklärt? Ich glaube nicht, dass ich diesen Teil falsch gemacht habe. Ich habe die WCF-Bibliothek entworfen und dann in meinem Webprojekt die Endpunkte mithilfe der Auswahl "Service-Referenz" aufgerufen. Dementsprechend habe ich meine beiden Endpunkte - AuthService und TradeService - eingebunden. Ich habe die Methoden jedes Dienstes in meinem Projekt verwendet, indem ich AuthServiceClient und TradeServiceClient instanziiert habe. Ist das falsch? Wenn ja, können Sie bitte erklären, warum und was ich tun muss, das ist richtig ... Vielen Dank im Voraus. –

+0

Sie sagten: "MEIN PROBLEM IST DAS, bis zu diesem Punkt habe ich meine WCF-Klassenbibliothek in meinem Webprojekt gerne referenziert. Aber jetzt scheint es, dass ich eine SVC-Datei erstellen muss". Wie haben Sie eine Service-Referenz ohne eine .svc-Datei erstellt? Was haben Sie in das URL-Textfeld eingegeben? –

+0

OK, danke dafür. Wenn ich "Service-Referenz hinzufügen" im Web-Projekt, zeigt das Fenster eine Schaltfläche "entdecken". Wenn ich darauf klicke, zeigt es auf die MEX-Endpunkte meiner beiden Dienste. Für "Authentifizierung" habe ich zum Beispiel folgendes im Feld "Adresse": http: // localhost: 8731/Design_Time_Addresses/Authentifizierung/mex Dies fügt den Dienst zu meinem Web-Projekt (unter Web-Services) und dann ich kann auf die Service-Methoden zugreifen. Was mache ich falsch? Sollte ich meinem Webprojekt eine SVC-Datei hinzufügen und Code schreiben, der als Proxy für alle Methoden dient, die ich in der Servicebibliothek erstellt habe? Danke. –