This is a migrated thread and some comments may be shown as answers.

Pulling From a WCF Service Project

4 Answers 121 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Joey
Top achievements
Rank 1
Joey asked on 25 Jan 2011, 05:19 PM
Hey all,

I have a RadComboBox that I want to have pull data from a separate WCF service project, and I've followed the guide for linking a RadComboBox to a WCF service (http://www.telerik.com/help/aspnet-ajax/combobox-loading-items-from-wcf-service.html), but it will not work when the service is in a WCF project separate from the web project.  I first tried configuring the RadComboBox to use a service within the same web project and that worked great, but once I moved the service to a separate project, it started giving the error "The server method GetData failed".

So, I have two projects in my solution.  A WCF service project and a web project, and the web project has a service reference to the WCF service project.  The web project has the following RadComboBox on its default page:
<telerik:RadComboBox ID="RadComboBox1"
   EnableLoadOnDemand="true"
   Width="250"
   runat="server">
    <WebServiceSettings Method="GetData" Path="http://localhost:4835/Service.svc" />         
</telerik:RadComboBox>

The WCF service project has the following ajax-enabled service:
namespace WcfService
{
    [ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class Service
    {
        [OperationContract]
        [WebGet(ResponseFormat=WebMessageFormat.Xml)]
        public RadComboBoxData GetDataTelerik(RadComboBoxContext context)
        {
            RadComboBoxData data = new RadComboBoxData();
 
            //todo: call db..
 
            RadComboBoxItemData item = new RadComboBoxItemData();
            item.Text = "rawr";
            item.Value = "teh value";
 
            data.Items = new RadComboBoxItemData[] { item };
            return data;
        }
    }
}

And the service project has the following web.config file:
<?xml version="1.0"?>
<configuration>
 
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
 
  <system.serviceModel>
      <behaviors>
          <endpointBehaviors>
              <behavior name="WcfService.ServiceAspNetAjaxBehavior">
                  <enableWebScript />
              </behavior>
          </endpointBehaviors>
          <serviceBehaviors>
            <behavior>
              <serviceMetadata httpGetEnabled="true"/>
            </behavior>
          </serviceBehaviors>
      </behaviors>
      <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
          multipleSiteBindingsEnabled="true" />
      <services>
          <service name="WcfService.Service">
              <endpoint address="" behaviorConfiguration="WcfService.ServiceAspNetAjaxBehavior"
                  binding="webHttpBinding" contract="WcfService.Service" />
          </service>
      </services>
  </system.serviceModel>
   
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
 
</configuration>

When the service was in the web project and working properly, the same serviceModel section of the above config file was in the web project's web.config.  When I moved the service to its own project, I pulled that section out and put it in the service project's web.config.  The only difference is I set httpGetEnabled to true (so that the service could be referenced and the wsdl could be viewed).

But when the RadComboBox tries to pull data from the service, it gives the error message "The server method GetData failed".  What am I missing?  Thanks!

-Joey

4 Answers, 1 is accepted

Sort by
0
Dimitar Terziev
Telerik team
answered on 28 Jan 2011, 03:44 PM
Hi Joey,

In order to call a web service which is not in your project , it's better to use server-side proxy. In general when using web service and ajax control, the XMLHttpRequest object doesn't allow calls to be made from code in one domain to a web service in another. That's why you can't load the content of your combobox.

Greetings,
Dimitar Terziev
the Telerik team
Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
0
sitefinitysteve
Top achievements
Rank 2
Iron
Veteran
answered on 01 Feb 2011, 09:11 PM
I just encountered this problem now as well

Same deal, worked okay in the website project, but external IIS hosted site fails with the exact same error...firebug shows the data is coming down...
0
Joey
Top achievements
Rank 1
answered on 01 Feb 2011, 09:20 PM
I was going about this problem the wrong way.  To make it work, I ended up setting up the WCF service the way you normally would when it's in a separate project and used the WCF asynchronous methods generated by the service's reference in my web app.  And then in the End method of the async call, I manually bound the results to the RadComboBox.  This way I can utilize the power of the autocomplete feature in the RadComboBox while being able to use whatever parameters and return types I want in my WCF service.

So the end method of my web service async call looks like this:
public partial class AutocompleteSearchTextbox : Telerik.Web.UI.RadComboBox
{
    public AutocompleteSearchTextbox() : base()
    {
        _searchTermType = "";
        Filters = new FilterCollection();
        EnableLoadOnDemand = true;
    }
 
    protected override void OnItemsRequested(RadComboBoxItemsRequestedEventArgs args)
    {
        base.OnItemsRequested(args);
 
        string searchText = args.Context["Text"].ToString();
 
        // If the search is already cached and the cached results are not too old, use them
        if (Page.Cache[searchText] != null && Page.Cache[searchText] is CachedSearchResult &&
            DateTime.Now.Subtract(((CachedSearchResult)Page.Cache[searchText]).CachedTime) < TimeSpan.FromSeconds(CachedSearchResult.CACHED_RESULT_SECONDS_TTL))
        {
            BindResults((CachedSearchResult)Page.Cache[searchText]);
            return;
        }
 
        // Query the web service for the search results
        PageAsyncTask asyncTask = new PageAsyncTask(BeginGetDataAsync, EndGetDataAsync, null, searchText);
        Page.RegisterAsyncTask(asyncTask);
    }
 
    void EndGetDataAsync(IAsyncResult ar)
    {
        SearchResult[] data = _service.EndGetData(ar);
        string searchText = ar.AsyncState.ToString();
         
        // Store the results in the cache
        if (Page.Cache[ar.AsyncState.ToString()] != null)
            ((CachedSearchResult)Page.Cache[ar.AsyncState.ToString()]).Result = data;
        else
        {
            FilterCollection filters = new FilterCollection();
            filters.Filters.Add(new Filter(SearchTermType, searchText));
            Page.Cache[searchText] = new CachedSearchResult(filters, data);
        }
 
        BindResults((CachedSearchResult)Page.Cache[searchText]);
    }
 
    private void BindResults(CachedSearchResult result)
    {
        DataTextField = "Result";
        DataSource = result.Result;
        DataBind();
    }

Hope this helps someone.
0
sitefinitysteve
Top achievements
Rank 2
Iron
Veteran
answered on 01 Feb 2011, 09:31 PM
Ack, well that's a pain in the ass :)

I just wanted a simple external webservice to bind my controls against lol...didn't want to force an IIS reset on the main site with a service update.
Tags
ComboBox
Asked by
Joey
Top achievements
Rank 1
Answers by
Dimitar Terziev
Telerik team
sitefinitysteve
Top achievements
Rank 2
Iron
Veteran
Joey
Top achievements
Rank 1
Share this question
or