【问题标题】:WCF service WebInvoke attribute and support some behaviorsWCF 服务 WebInvoke 属性并支持一些行为
【发布时间】:2012-05-28 13:56:14
【问题描述】:

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

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

【问题讨论】:

    标签: asp.net json wcf


    【解决方案1】:

    如果您想使用 [WebGet] / [WebInvoke] 属性,您需要使用带有 webHttpBinding 的端点和使用 &lt;webHttp/&gt; 端点行为的行为 - 请参阅下面的配置编辑。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-21
      • 1970-01-01
      相关资源
      最近更新 更多