WCF和JSON绑定 [英] WCF and JSON binding

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

问题描述

我正在尝试创建一个返回json的wcf服务.我的配置文件有问题,我也不知道如何测试.

I am trying to create a wcf service that returns json. I have some problems with my config file and i also don't know how to test it.

<?xml version="1.0"?>
<configuration>
  <system.web>
    <compilation debug="false" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <services>
      <service name="ContactLibraryJSON.ContactLibrary">
        <endpoint address="" binding="webHttpBinding" bindingConfiguration="JSONEndpointBehavior"
          contract="ContactLibraryJSON.IContactServiceJSON" />
        <endpoint address="mex" binding="mexHttpBinding" bindingConfiguration=""
          contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="http://192.168.1.31/ContactLibraryJSON" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="JSONEndpointBehavior">
          <webHttp/>
        </behavior>
        <behavior>
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
</configuration>

我还是

无法将行为扩展webhttp添加到服务行为 之所以命名为JSONEndpointBehavior,是因为基础行为类型确实 没有实现IServiceBehaviorInterface"

"Cannot add the behavior extension webhttp to the service behavior names JSONEndpointBehavior because the underlying behavior type does not implement the IServiceBehaviorInterface"

联系人的定义如下:

[DataContract(Name="Contact")]
public class Contact
{        
    [DataMember(Name="FirstName")]
    public string firstName=null;
    [DataMember(Name="LastName")]
    public string lastName=null;
    [DataMember(Name="Email")]
    public string email=null;
    [DataMember(Name = "Age")]
    public int age = 0;
    [DataMember(Name = "Street")]
    public string street=null;
    [DataMember(Name = "City")]
    public string city=null;
    [DataMember(Name = "Country")]
    public string country=null;
}

IContactService的定义如下:

IContactService is defined like :

[ServiceContract]
public interface IContactServiceJSON
{
    [OperationContract]
    [WebGet(BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json, UriTemplate = "")]
    Contact GetContact();        
}

GetContact的实现:

public Contact GetContact()
{
    return new Contact()
    {
        firstName = "primulNume",
        lastName = "alDoileaNume",
        age = 33,
        city = "Cluj",
        country = "Romania",
        email = "ceva@mail.com",
        street = "Bizusa 8"
    };
}

我的服务在局域网中的另一台计算机上运行.基址类似于: http://192.168.1.xxx/ContactLibraryService . ContactLibraryService由IIS托管,并转换为应用程序.

My service runs on another computer in my lan. Base address is like: http://192.168.1.xxx/ContactLibraryService. ContactLibraryService is hosted by IIS and is converted to an application.

推荐答案

拥有服务合同IContact和数据联系人Contact不好.重命名服务合同,例如IContactService.

It's not good that you have Service Contract IContact and Data Contact Contact. Rename Service Contract like IContactService.

<services>
  <service name="ContactLibrary.ContactService">
    <endpoint address="" binding="webHttpBinding" contract="ContactLibrary.IContactService"  behaviorConfiguration="JsonBehavior" />
    <host>
      <baseAddresses>
        <add baseAddress="http://localhost:8732"/>
      </baseAddresses>
    </host>
  </service>
</services>

<behaviors>
  <endpointBehaviors>
    <behavior name="JsonBehavior">
      <webHttp />
    </behavior>
  </endpointBehaviors>
</behaviors>

在调试期间(看起来您具有WCF库),服务地址将为 http://localhost:8732/contact

During Debug time (it looks you have WCF library), service address will be http://localhost:8732/contact

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

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