c#创建与SQL服务器连接的wcf服务,并在metro风格的javascript / html5应用程序中调用它

时间:2012-06-21 08:52:29

标签: sql-server windows-8 microsoft-metro

由于windows 8 metro风格的javascript / html5应用程序无法直接与数据库通信,我想创建一个与SQL服务器连接的c#wcf服务,并在metro风格的javascript / html5应用程序中调用它进行数据存储和检索。

我在Visual Studio 2012 RC中创建了一个c#wcf服务应用程序,并托管在本地Windows 8发布预览系统上。但我无法将wcf服务与我的地铁应用程序连接。

以下是我的wcf服务应用程序代码:

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

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



}
}

并且

<?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>

以下是我在metro app中调用wcf的代码:

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

我的Metro应用程序退出时出现以下错误:

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

请告诉我这里我做错了什么...... 或者有没有办法与sql server数据库通信metro javascript / html5应用程序。

提前致谢...

1 个答案:

答案 0 :(得分:1)

我是通过创建JSON-Rest-WCF Service,将其与sql server连接并通过以下方法从metro app调用此服务来完成的。

WinJS.xhr({ url: url})