json - WCF 和 JSON 绑定(bind)

标签 json wcf wcf-binding wcf-rest

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

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

我还是明白了

"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 的定义如下:

[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/ContactLibraryServiceContactLibraryService 由 IIS 托管并转换为应用程序。

最佳答案

您需要添加<webHttp/>端点行为列表。此外,端点需要使用 webHttpBinding 。最后,回复GET HTTP请求,需要使用WebGet属性(而不是 WebInvoke(Method="GET")

  <system.serviceModel> 
    <services> 
      <service name="ContactLibrary.ContactLibrary"> 
        <endpoint address=""
                  binding="webHttpBinding"
                  behaviorConfiguration="JSONEndpointBehavior" 
                  contract="ContactLibrary.IContact"/>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> 
        <endpoint address="ws"
                  binding="wsHttpBinding"
                  bindingConfiguration="" 
                  contract="ContactLibrary.IContact" /> 
        <host> 
          <baseAddresses> 
            <add baseAddress="http://localhost/ContactLibrary" /> 
          </baseAddresses> 
        </host> 
      </service> 
    </services> 
    <behaviors> 
      <endpointBehaviors> 
        <behavior name="JSONEndpointBehavior"> 
          <webHttp/> 
        </behavior> 
      </endpointBehaviors> 
      <serviceBehaviors> 
        <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> 
  </system.serviceModel> 

以及服务契约(Contract):

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

关于json - WCF 和 JSON 绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9838090/

相关文章:

iOS Swift - Google Place API(Web)以奇怪的格式返回 JSON

c# - 根据拦截和返回值自动重试客户端 WCF 调用

C# 2 维 byte[][] 到 JSON.NET 到 C# byte[][] 或 Javascript 等效项失败

WCF 跟踪日志分析 - 帮助

c# - 收到错误 : Contract requires Duplex, 但绑定(bind) 'BasicHttpBinding' 不支持它或未正确配置以支持它

json - 在 Ballerina 中将字符串转换为 Json

php - 如何对该查询的结果进行 json_encode?

javascript - Knockout.js 将 observable 与单个 json 对象绑定(bind)

java - 使用 C# 客户端调用 WS-Security Java Web 服务

.net - WebInvoke 属性可以被绑定(bind)配置替换吗