0

您好,目前我在 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>

我需要做什么才能与其他客户端兼容?

4

1 回答 1

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>
于 2012-05-28T16:18:09.570 回答