WCF服务WebInvoke属性并支持一些行为

时间:2012-05-28 13:56:14

标签: asp.net json wcf

Hello目前我在FW3.5下有传统的WCF服务。使用一些SilverLigt应用程序, 我需要扩展此WCF服务以支持JSON格式并将其调用为JavaScript应用程序, 目前的服务看起来像

[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Alcatraz
{
    [OperationContract]
    [FaultContract(typeof(SomeError))]
    public SomeClass DoSomething(SomeClass data)
    {
    }
}

据我所知,支持WCF服务的JSON格式我需要将WebInvoke属性添加到方法

[WebInvoke(Method = "GET",
           RequestFormat = WebMessageFormat.Json,
           ResponseFormat = WebMessageFormat.Json,
           UriTemplate = "players")]

[DataContract]添加到 SomeClass 类。但我不确定如果我要添加这个,所有应用程序都会以同样的方式工作吗?

<system.serviceModel>
  <extensions>
    <behaviorExtensions>
      <add name="silverlightFaults" type="myNS.WCF.SilverlightFaultBehavior, SilverlightFaultBehavior, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
    </behaviorExtensions>
  </extensions>
  <bindings>
    <customBinding>
      <binding name="binaryHttpBinding" allowCookies="true" receiveTimeout="00:02:00" sendTimeout="00:02:00" openTimeout="00:02:00" closeTimeout="00:02:00">
        <binaryMessageEncoding/>
        <httpTransport maxReceivedMessageSize="655360" maxBufferSize="655360"/>
      </binding>
    </customBinding>
  </bindings>
  <behaviors>
    <endpointBehaviors>
      <behavior name="SilverlightFaultBehavior">
        <silverlightFaults/>
      </behavior>
    </endpointBehaviors>
    <serviceBehaviors>
      <behavior name="myNS.WCF.MyServiceBehavior">
        <serviceMetadata httpGetEnabled="true"/>
        <serviceDebug includeExceptionDetailInFaults="true"/>
      </behavior>
    </serviceBehaviors>
  </behaviors>
  <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
  <services>
    <service behaviorConfiguration="myNS.WCF.MyServiceBehavior" name="myNS.WCF.MyService">
      <endpoint address="" binding="customBinding" bindingConfiguration="binaryHttpBinding" contract="myNS.WCF.MyService" behaviorConfiguration="SilverlightFaultBehavior"/>
      <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
    </service>
  </services>
</system.serviceModel>

为了与其他客户兼容,我需要做些什么?

1 个答案:

答案 0 :(得分:1)

如果要使用[WebGet] / [WebInvoke]属性,则需要使用具有webHttpBinding的端点并使用<webHttp/>端点行为的行为 - 请参阅对配置的编辑下方。

<system.serviceModel>
  <extensions>
    <behaviorExtensions>
      <add name="silverlightFaults" 
           type="myNS.WCF.SilverlightFaultBehavior, SilverlightFaultBehavior, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
    </behaviorExtensions>
  </extensions>
  <bindings>
    <customBinding>
      <binding name="binaryHttpBinding" 
               allowCookies="true" 
               receiveTimeout="00:02:00" 
               sendTimeout="00:02:00" 
               openTimeout="00:02:00" 
               closeTimeout="00:02:00">
        <binaryMessageEncoding/>
        <httpTransport maxReceivedMessageSize="655360" maxBufferSize="655360"/>
      </binding>
    </customBinding>
  </bindings>
  <behaviors>
    <endpointBehaviors>
      <behavior name="SilverlightFaultBehavior">
        <silverlightFaults/>
      </behavior>
      <behavior name="Web">
        <webHttp/>
      </behavior>
    </endpointBehaviors>
    <serviceBehaviors>
      <behavior name="myNS.WCF.MyServiceBehavior">
        <serviceMetadata httpGetEnabled="true"/>
        <serviceDebug includeExceptionDetailInFaults="true"/>
      </behavior>
    </serviceBehaviors>
  </behaviors>
  <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
  <services>
    <service behaviorConfiguration="myNS.WCF.MyServiceBehavior" name="myNS.WCF.MyService">
      <endpoint address="" 
                binding="customBinding" 
                bindingConfiguration="binaryHttpBinding" 
                contract="myNS.WCF.MyService" 
                behaviorConfiguration="SilverlightFaultBehavior"/>
      <endpoint address="web" 
                binding="webHttpBinding" 
                contract="myNS.WCF.MyService" 
                behaviorConfiguration="Web"/>
      <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
    </service>
  </services>
</system.serviceModel>