Sunday, September 12, 2010

Wcf Data Service

Hi
I have created a basic WCF Dataservices using ADO.net entity framework in silverlight. When i about to create method and access it i m getting error as bad request,Error in query syntax.

I have created Wcf Dataservice as
public static void InitializeService(IDataServiceConfiguration config)
{
config.SetEntitySetAccessRule("*", EntitySetRights.All);
config.SetServiceOperationAccessRule("*", ServiceOperationRights.All);

config.UseVerboseErrors = true;
}

[WebGet]
public string GetUsers(string userId)
{
return "my data";
}

my browser uri is
http://localhost:50449/DataService.svc/GetUsers?userId='1'


And here is the Code from Client:
public void LoadData(string userId)
{
var qry = ctx.CreateQuery("GetUsers").AddQueryOption("UserId", string.Format(@"'{0}'", userId));

qry.BeginExecute(new AsyncCallback(OnLoadComplete), qry);

}
When i execute via browser uri i m getting the result. Not by accessing via method in SL.
I found in SL it adds () in browser URI as like this
http://localhost:50449/DataService.svc/GetUsers()?userId='1'

After a long search we can execute the method as mentioned below which solve me the problem.

ctx.BeginExecute(new Uri(string.Format("{0}GetUsers?UserId={1}", smileEntites.BaseUri, "'1' "), UriKind.RelativeOrAbsolute), OnLoadComplete, null);

void OnLoadComplete(IAsyncResult result)
{

try
{
// Get the results and add them to the collection
IQueryable queryResults = smileEntites.EndExecute(result).AsQueryable();
foreach (var coursw in queryResults)
{
}


}
catch (Exception ex)
{
if (HtmlPage.IsEnabled)
{
HtmlPage.Window.Alert("Failed to retrieve data: " + ex.ToString());
}
}
}

No comments: