2012-03-23 76 views
4

我想創建一個返回json的wcf服務。我的配置文件有一些問題,我也不知道如何測試它。WCF和JSON綁定

<?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 = "[email protected]", 
     street = "Bizusa 8" 
    }; 
} 

我的服務我的局域網的另一臺計算機上運行。基地址如下:http://192.168.1.xxx/ContactLibraryServiceContactLibraryService由IIS託管並轉換爲應用程序。

回答

4

這是不好的,你有服務合同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

+0

我修改了名稱並更新了我的帖子。 – 2012-03-23 16:00:12

+1

'JSONEndpointBehavior'應該不是Service但是Endpoint行爲 – paramosh 2012-03-23 16:38:13

5

您需要的<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> 

和服務合同。

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

我更新了我的帖子。 – 2012-03-23 15:58:39

+0

您仍然沒有終端行爲。 – carlosfigueira 2012-03-24 14:49:19

+0

我將在星期一編輯服務,因爲這是一項工作任務。如果我成功了,或者我得到新的錯誤,我會再次發佈。 – 2012-03-24 21:21:42

2

我想你錯過綁定在這裏你必須在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> 
+0

請根據您的要求更改參數,並保持您已添加的其他標籤。 – 2013-04-03 13:59:24