如何使用WCF服务来支持Web应用程序和移动应用程序?

时间:2016-09-02 09:08:20

标签: c# android .net wcf

我想开发一个带有WCF服务的Web应用程序,该服务不仅支持我的Web应用程序,还支持使用JSON进行所有操作的移动应用程序。

那么有人可以为它建议最好的架构以及如何实现它吗?

更新

这是我到目前为止所尝试的内容

1)这是我的应用程序的结构

enter image description here

2)IHelloService.cs

enter image description here

3)HelloService.svc

enter image description here

4)工厂中的业务逻辑 - ServiceProfile.cs

enter image description here

4)实体 - Profile.cs

enter image description here

5)在页面上调用服务

enter image description here

这样可以正常使用!!但当我通过Android应用程序访问此服务,必须返回JSON。那是我被困的地方。如何使用可以双方使用的相同服务。

为了测试这项服务,我使用了如下的ajax调用。

 $.ajax({
            url: "Service/HelloService.svc/TestService",
            type: 'POST',
            //dataType: 'json',
            //contentType: "application/json; charset=utf-8",
            data: JSON.stringify({ "_Profile": [{ "Name": "Aijaz from JSON" }] }),
            success: function (data) {
                $('[Id$="Label1"]').text(data);
            },
            error: function (e) {

            },
            complete: function () { }
        });

2 个答案:

答案 0 :(得分:0)

创建一个宁静的WCF服务。此服务将提供JSON格式的数据,您可以在Web应用程序/移动应用程序中使用。

更多信息:如何创建此类服务。 http://www.compilemode.com/2015/09/creating-wcf-rest-service-to-get-json.html

我希望这就是你想要的。

答案 1 :(得分:0)

我认为你错过了webHttpBinding的绑定细节。 :)