0

As windows 8 metro style javascript/html5 apps cannot communicate directly with database, I want to create a c# wcf service connected with SQL server and calling it in metro style javascript/html5 app for data storing and retrieving.

I have created a c# wcf service application in Visual Studio 2012 RC and hosted on local windows 8 release preview system. But i am unable to connect wcf service with my metro app.

Below is my wcf service application code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace WcfService1
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
[ServiceContract]
public interface IService2
{
    [OperationContract]
    [WebGet(UriTemplate = "/Add/{Number1}/{Number2}", RequestFormat = WebMessageFormat.Json,
   ResponseFormat = WebMessageFormat.Json
                                                    )]
    int Add(string Number1, string Number2);
}
}

And

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace WcfService1
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
// NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging.
public class Service1 : IService2
{
    public int Add(string Number1, string Number2)
    {
        int num1 = Convert.ToInt32(Number1);
        int num2 = Convert.ToInt32(Number2);
        return num1 + num2;
    }      



}
}

And

<?xml version="1.0"?>
<configuration>

<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5" />
</system.web>
<system.serviceModel>
<behaviors>
  <serviceBehaviors>
    <behavior>

      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>

      <serviceDebug includeExceptionDetailInFaults="false"/>
    </behavior>
  </serviceBehaviors>     
</behaviors>    
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
    multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>

<directoryBrowse enabled="true"/>
</system.webServer>

</configuration>

Below is my code of calling wcf in metro app:

var Number1  = document.getElementById('TextboxFirstNumber');
var Number2  = document.getElementById('TextboxSecondNumber');
var ResultSpan  = document.getElementById('SpanResult');
var ButtonToAdd  = document.getElementById('ButtonAdd');
ButtonToAdd.addEventListener('click',  function  ()  {
    var baseURl = "http://localhost:52653/Service1.svc/Add/";
    var url  = baseURl+Number1.value+"/"+Number2.value;  
    WinJS.xhr({ url: url  }).then(function  (r)  {
        var result  = JSON.parse(r.responseText);
        ResultSpan.innerHTML  = result; 
    });
}); 

My metro app exits with following error:

{"exception":null,"error":{},"promise":{"_oncancel":null,"_nextState":null,"_state":{"name":"error","done":null,"then":null},"_listeners":null,"_value":{},"_isException":false,"_errorId":2},"id":2}

Please tell me what i am doing wrong here... or is there any way to communicate metro javascript/html5 app with sql server database.

Thanks in advance...

Kate Gregory
  • 18,808
  • 8
  • 56
  • 85
Babur Saqib
  • 11
  • 1
  • 4
  • `As windows 8 metro style javascript/html5 apps cannot communicate directly with database` - What has `metro style` to do with communication ? I thought its plain old JS and HTML5. One can communicate with database - like this - http://www.w3schools.com/ajax/ajax_database.asp, Sorry if I am missing something ? – Angshuman Agarwal Jun 21 '12 at 08:58
  • @AngshumanAgarwal thx for your reply, but it does not work in metro app because it is client side programing – Babur Saqib Jun 21 '12 at 09:13
  • Yea we all know that `Javascript and HTML` are meant for `client only` since ages. But, AJAX does work - http://stackoverflow.com/a/10366701/763026. may be you have to read more about the the framework and syntaxes. `Another good website here` - http://stephenwalther.com/archive/category/metro. `Read about declarative data binding here` - http://stephenwalther.com/archive/2012/02/26/metro-declarative-data-binding.aspx – Angshuman Agarwal Jun 21 '12 at 09:18
  • You are talking about web app, i am talking about metro style desktop app(not having any hosting web server). In your links arrays or IndexedDb is used for data storage. I want to user sql server express localdb comes with windows 8 release preview. – Babur Saqib Jun 21 '12 at 09:54
  • But, you have mentioned `javascript/html5 apps` & I am just telling that `it is possible to make Db calls from Javascript code` and one `does not require any kind of hosting for that` - http://msdn.microsoft.com/en-us/library/windows/apps/hh700358.aspx. Moreover, for WCF services you may have to do a hosting on IIS if you are not doing self-hosting. May be then you have to provide more information in your question in terms of what actually you are trying to achieve. – Angshuman Agarwal Jun 21 '12 at 10:14
  • You are right that "it is possible to make Db calls from Javascript code" and i have done it and the code runs in web browser but the same code does not run in metro environment. Secondly i have hosted WCF service on local IIS server and have tested it with WCF Test Client. It is working fine with test client – Babur Saqib Jun 21 '12 at 10:27
  • I have done it by creating a JSON-Rest-WCF Service connected with sql server and call this service from metro app – Babur Saqib Jun 21 '12 at 14:40
  • cool !! Good to know you have solved it. If possible, upload this as a sample on `github` and share the link for everybody to learn. – Angshuman Agarwal Jun 21 '12 at 15:38

1 Answers1

1

I have done it by creating a JSON-Rest-WCF Service, connect it with sql server and call this service from metro app through below method.

WinJS.xhr({ url: url})
Babur Saqib
  • 11
  • 1
  • 4