【问题标题】:WCF and JSON bindingWCF 和 JSON 绑定
【发布时间】:2012-04-07 22:44:27
【问题描述】:

我正在尝试创建一个返回 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>

我还是明白了

"无法在服务行为中添加行为扩展webhttp 命名JSONEndpointBehavior,因为底层行为类型确实 不实现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 托管并转换为应用程序。

【问题讨论】:

    标签: json wcf wcf-binding wcf-rest


    【解决方案1】:

    您需要将&lt;webHttp/&gt; 添加到端点 行为列表中。此外,端点需要使用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> 
    

    以及服务合同:

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

    【讨论】:

    • 我将在星期一编辑服务,因为这是一项工作任务。如果我成功或收到新错误,我将再次发布。
    【解决方案2】:

    您有服务合同IContact 和数据联系人Contact 并不好。将服务合同重命名为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

    【讨论】:

    • 'JSONEndpointBehavior' 应该不是服务而是端点行为
    【解决方案3】:

    我认为您在这里错过了绑定,您必须在 system.serviceModel 标签的绑定选项卡下添加 webHttpBinding 和 basichttpbing。

        <services>
      <service name="VInfotech.Server.Intranet.IntranetService" behaviorConfiguration="IntranetService.Service1Behavior">
        <!-- Service Endpoints -->
        <endpoint address="" binding="basicHttpBinding" bindingConfiguration="Binding1" contract="VInfotech.Server.Intranet.IIntranet">
          <!-- 
              Upon deployment, the following identity element should be removed or replaced to reflect the 
              identity under which the deployed service runs.  If removed, WCF will infer an appropriate identity 
              automatically.
          -->
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>
      </service>
      <service name="VInfotech.Server.Intranet.MobileServicesController" behaviorConfiguration="ServBehave">
        <endpoint address="RestService" bindingConfiguration="StreamedRequestWebBinding" binding="webHttpBinding" behaviorConfiguration="restPoxBehavior" contract="VInfotech.Server.Intranet.IMobileServices" />
      </service>
    </services>
    <bindings>
      <webHttpBinding>
        <binding name="StreamedRequestWebBinding"
        bypassProxyOnLocal="true"
                 useDefaultWebProxy="false"
                 hostNameComparisonMode="WeakWildcard"
                 sendTimeout="10:15:00"
                 openTimeout="10:15:00"
                 receiveTimeout="10:15:00"
                 maxReceivedMessageSize="9223372036854775807"
                 maxBufferPoolSize="9223372036854775807"
                 maxBufferSize="2147483647"
                 transferMode="StreamedRequest" >
          <readerQuotas maxArrayLength="2147483647" maxStringContentLength="2147483647" />
        </binding>
      </webHttpBinding>
      <basicHttpBinding>
        <binding name="Binding1" closeTimeout="04:01:00" openTimeout="04:01:00" receiveTimeout="04:10:00" sendTimeout="04:01:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="1073741824" maxBufferPoolSize="1073741824" maxReceivedMessageSize="1073741824" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true">
          <readerQuotas maxDepth="1073741824" maxStringContentLength="1073741824" maxArrayLength="1073741824" maxBytesPerRead="1073741824" maxNameTableCharCount="1073741824"/>
        </binding>
        <!-- For Cyber Source bindings-->
        <binding name="ITransactionProcessor" closeTimeout="04:01:00" openTimeout="04:01:00" receiveTimeout="04:10:00" sendTimeout="04:01:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="1073741824" maxBufferPoolSize="1073741824" maxReceivedMessageSize="1073741824" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true">
          <readerQuotas maxDepth="1073741824" maxStringContentLength="1073741824" maxArrayLength="1073741824" maxBytesPerRead="1073741824" maxNameTableCharCount="1073741824"/>
          <security mode="TransportWithMessageCredential">
            <transport clientCredentialType="None" proxyCredentialType="None" realm=""/>
            <message clientCredentialType="UserName" algorithmSuite="Default"/>
          </security>
        </binding>
      </basicHttpBinding>
      <!--Cyber Source bindings ends here-->
    </bindings>
    

    【讨论】:

    • 请根据您的要求更改参数,并保留您已添加的其他标签。
    猜你喜欢
    • 1970-01-01
    • 2013-08-15
    • 2010-10-04
    • 2010-11-29
    • 1970-01-01
    • 2015-03-19
    • 2012-04-01
    相关资源
    最近更新 更多