启用WCF服务使用JSON使用 [英] Enable WCF Service to use with JSON

查看:156
本文介绍了启用WCF服务使用JSON使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个WCF服务。这是工作正常,当我使用简单的.NET通过添加作为一个Web服务。但我想使它能够使用iPhone应用程序为JSON调用。为了测试我已经用它在.NET中使用JSON,但它不工作。

I have created a wcf service. That is working fine when i am using simply in .net by adding as a webservice. But i want to make it able to use for iPhone app as JSON call. For testing i have used it in .net with JSON but its not working.

我知道这样的问题之前问,我特地为这对我来说不能找到解决方案。

i know this kind of question is asked before, i have looked in for this cant find solution for me.

我的配置:

<system.serviceModel>
<behaviors>
  <serviceBehaviors>
    <behavior name="servicebehavior">
      <serviceMetadata httpsGetEnabled="true" />
    </behavior>
  </serviceBehaviors>
  <endpointBehaviors>
    <behavior name="endpointBehavior">
      <enableWebScript />
      <webHttp defaultBodyStyle="Wrapped" defaultOutgoingResponseFormat="Json" />
    </behavior>
  </endpointBehaviors>

</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
<services>
  <service name="MyService" behaviorConfiguration="servicebehavior">
    <endpoint address=""
              behaviorConfiguration="endpointBehavior"
              binding="webHttpBinding"
              contract="IMyService" />
  </service>
</services>

接口code:

[ServiceContract]
public interface IGolfPyramidService
{



    [WebInvoke(UriTemplate = "/Test", Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
    [OperationContract]
    string Test();

}

Myservice.cs code:

Myservice.cs code:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class MyService : IMyService
{        
    public string Test()
    {
        return "success";
    }
}

我想有可能使用URL格式像调用方法: http://example.com/MyService.svc/test

i want to make it possible to call the method using url format like : http://example.com/MyService.svc/test

推荐答案

如果你是初学者,那么这将引导您创建一个可以通过iOS和Android消耗JSON和XML的web服务。
<一href="http://www.$c$cproject.com/Articles/105273/Create-RESTful-WCF-Service-API-Step-By-Step-Guide">http://www.$c$cproject.com/Articles/105273/Create-RESTful-WCF-Service-API-Step-By-Step-Guide

if you are beginner then this will guide you create json and xml enabled web service which can be consumed by IOS and android.
http://www.codeproject.com/Articles/105273/Create-RESTful-WCF-Service-API-Step-By-Step-Guide

这篇关于启用WCF服务使用JSON使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆