0

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

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

以下是我的 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 应用程序中调用 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}

请告诉我我在这里做错了什么......或者有什么方法可以将 Metro javascript/html5 应用程序与 sql server 数据库通信。

提前致谢...

4

1 回答 1

1

我通过创建JSON-Rest-WCF 服务来完成它,将其与 sql server 连接并通过以下方法从 Metro 应用程序调用此服务。

WinJS.xhr({ url: url})
于 2012-06-22T08:14:18.617 回答