views:

478

answers:

1

Hi.

I have been having some problems with this for a few days now... I have designed a WCF Class Library - WebAPILibrary. It is under my solution which also contains my web project.

I now am attempting to enable a AJAX/JSON response to a search query.

In the client code examples, I see something to the effect:

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;
                      });
                  });
              });

                }

MY PROBLEM IS that, up until this point, I have been happily referencing my WCF class library in my Web Project. But, now it seems I need to create a SVC file and use it to execute my javascript AJAX/JSON calls.

MY QUESTION IS:

1) How do I create the SVC file in cooperation with my WCF class library? 2) I am not quite sure what I need to put under system.ServiceModel for my web project's web.config. Could some kind soul kindly look at what I currently have and advise me on what I need to be doing?

WEB PROJECT 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 CLASS LIBRARY 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>
+1  A: 

You should never reference your WCF library in any other project. It is meant to be used as a service, not as a class library.

John Saunders
Thanks for the response. Maybe I have explained myself poorly? I don't think I have done that part incorrectly. I have designed the WCF library and then, in my Web Project, called the endpoints by using the "Service Reference" selection. Accordingly, I have incorporated my two endpoints - AuthService and TradeService. I have been using each service's methods in my project by instantiating AuthServiceClient and TradeServiceClient. Is this incorrect? If so, can you kindly explain why and what I need to be doing that is correct... Thanks in advance.
Code Sherpa
You said "MY PROBLEM IS that, up until this point, I have been happily referencing my WCF class library in my Web Project. But, now it seems I need to create a SVC file". How did you create a service reference without a .svc file? What did you type into the URL textbox?
John Saunders
OK, thanks for that. When I "Add Service Reference" in the Web Project the window displays a "discover" button. When I click on it, it points to the MEX endpoints of my two services. For example, for "Authentication" I have the following in the "Address" box:http://localhost:8731/Design_Time_Addresses/Authentication/mexThis adds the service to my Web Project (under web services) and then I can access the service methods. What am I doing wrong? Should I add an SVC file to my web project and write code that acts as a proxy to all the methods I have created in the Service Library? Thanks.
Code Sherpa
Ok, thanks. Those are only for test purposes, specifically, those are for the use of the WCF Test Client. Do not use those in a client of your own. You need to create .svc files, or else self-host.
John Saunders
OK, thanks. I need just a bit more clarification... To create the SVC file, do I simply place it and the *.svc.cs file in the Web Project directory? And, how do I use the SVC file to offer all the methods I created in the Service Class Library?
Code Sherpa
You don't need a .svc.cs file. The .svc file will refer to the service class you created in your service class library.
John Saunders
OK, I think this is what I needed to know. Could you kindly show me what my SVC file should look like if it is pointing to my class library (which is part of the same solution, but in a different project than the SVC file). Here is what I currently have in my web project:<%@ ServiceHost Language="C#" Debug="true" Service="MyService" CodeBehind="~/App_Code/Service.cs" %>
Code Sherpa
And here is one more question... If I am supposed to be pointing to my assembly in my SVC file, don't i need to be referencing my WCF class library in my Web Project? I know you said that I shouldn't be doing this but, then, how does my SVC file know it exists?
Code Sherpa
I believe what you should do is create a WCF Service Project. Reference your WCF Class Library from the new project (this is the only project that should reference it). Create a .svc file, and change the `<%@ ServiceHost%>` statement to point to your service from your library. I don't have the syntax for `<%@ ServiceHost%>` handy. Please look it up in MSDN.
John Saunders
OK, this is all sorted out now. This comment:"You don't need a .svc.cs file. The .svc file will refer to the service class you created in your service class library" set me down the right path. Thanks for your help.
Code Sherpa