在使用路由时,如何在WCF的配置中正确设置服务行为? [英] How do I properly set up service behaviors in configuration for WCF when using routing?

查看:45
本文介绍了在使用路由时,如何在WCF的配置中正确设置服务行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个基于WCF的既定项目。它使用路由来公开REST-ful API。最近我不得不改变其中一个包含的服务的行为,以便返回更大的JSON序列化响应而不会崩溃。使用ServiceBehaviorAttribute进行更改是非常简单的(如声明ServiceController类时所示),但我们希望解决方案基于配置,而不是基于代码。



注意:该属性实际上不存在于代码中。它只是用来说明如何使用它。

I'm working on an established project that's based on WCF. It uses routing in order to expose a REST-ful API. Recently I had to change the behavior of one of the included services in order to return larger JSON-serialized responses without crashing. The change was trivial to make using a ServiceBehaviorAttribute (as shown when declaring the ServiceController class), but we wanted the solution to be configuration-based, not code-based.

NOTE: that attribute isn't actually present in the code. It's just there to illustrate how it would be used.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Net;

namespace Company.Project.Service.Services.Interfaces
{
    [ServiceContract]
    public interface IServiceController
    {
        [OperationContract]
        [Description("Gets stuff")]
        [WebGet(UriTemplate = "/?stuffIds={stuffIds}", ResponseFormat = WebMessageFormat.Json)]
        IEnumerable<Stuff> GetStuff(string stuffIds);
    }
}

namespace Company.Project.Service.Services
{   
    [ServiceBehavior(MaxItemsInObjectGraph = 2147483647)]
    public class ServiceController : BaseController, IServiceController
    {
        public IEnumerable<Stuff> GetStuff(string stuffIds)
        {
            ///
        }
    }   
}



这是(大部分)设置的代码路由:


Here is (most of) the code that sets up routing:

using System;
using System.ServiceModel.Activation;
using System.Web.Routing;

using Company.Project.Service.Services;

namespace Company.Project.Service.Host
{
    public class Global : System.Web.HttpApplication
    {
        protected void Application_Start(object sender, EventArgs e)
        {
            RegisterRoutes();
        }

        private void RegisterRoutes()
        {
            ///

            RouteTable.Routes.Add(new ServiceRoute("service", new WebServiceHostFactory(), typeof(Services.ServiceController)));
        }
    }
}



服务配置如下:


The service configuration is as follows:

<services>
  <service name="Company.Project.Service.Services.ServiceControllerZZTop"

       behaviorConfiguration="secureBehavior">

    <endpoint name="service"

              contract="Company.Project.Service.Services.Interfaces.IServiceController"

              binding="basicHttpBinding"

              bindingConfiguration="AsmxConfiguration"

              address="ws"

              />
  </service>
</services>

<bindings>
  <basicHttpBinding>
    <binding name="AsmxConfiguration"

             receiveTimeout="00:03:00"

             sendTimeout="00:03:00"

             maxReceivedMessageSize="2147483647"

             maxBufferPoolSize="2147483647">
      <security mode="None"></security>
      <readerQuotas maxDepth="2147483647"

                    maxStringContentLength="2147483647"

                    maxArrayLength="2147483647"

                    maxBytesPerRead="2147483647"

                    maxNameTableCharCount="2147483647" />
    </binding>

    <binding name="SecureBasic"

             receiveTimeout="00:03:00"

             sendTimeout="00:03:00"

             maxReceivedMessageSize="2147483647"

             maxBufferPoolSize="2147483647">
      <security mode="Transport">
        <transport clientCredentialType="None" />
      </security>
      <readerQuotas maxDepth="2147483647"

                    maxStringContentLength="2147483647"

                    maxArrayLength="2147483647"

                    maxBytesPerRead="2147483647"

                    maxNameTableCharCount="2147483647" />
    </binding>
  </basicHttpBinding>
</bindings>

<behaviors>
  <serviceBehaviors>
    <behavior name="secureBehavior">
      <serviceDebug includeExceptionDetailInFaults="false" />
      <serviceMetadata httpsGetEnabled="true" />
      <dataContractSerializer maxItemsInObjectGraph="2147483647" />
    </behavior>
  </serviceBehaviors>
</behaviors>



在服务配置中,您会注意到该名称与服务控制器类的名称不完全匹配。这是故意的 - 我整天都在脑子里戴着廉价太阳镜 - 显然有必要让整个事情发挥作用。我稍后会进一步解释。否则,看起来很正常:服务引用了一个行为,它将序列化程序的maxItemsInObjectGraph属性设置得足够高以修复原始问题,而< endpoint /> 在引用中设置了basicHttpBinding所有其他必要的属性。



我们曾经使用 SecureBasic 绑定,但是去了<$当我们开始公开端点非安全时,c $ c> AsmxConfiguration 绑定。 (HTTP而不是SSL)它们是相同的,除了一个具有传输级别的安全性。



运行我显示的代码实际上有效,并且没有失败时返回非常大的数据集。但是,如果我从服务配置中的服务类名称中删除ZZ Top,则服务会响应404错误页面。


In the service configuration you'll notice that the name doesn't quite match the name of the service controller class. That is intentional -- I'd had "Cheap Sunglasses" in my head all day -- and apparently necessary to make the whole thing work. I'll explain further in a bit. Otherwise, looks normal: service references a behavior that sets the serializer's maxItemsInObjectGraph attribute high enough to fix the original problem, and the <endpoint/> within references a basicHttpBinding that sets all of the other necessary properties.

We used to use the SecureBasic binding, but went to the AsmxConfiguration binding when we started exposing endpoints "non-secure". (HTTP instead of SSL) They're identical except that one has transport-level security.

Running the code as I've shown actually works, and without failing when returning very large data sets. However, if I remove ZZ Top from the service class name in the service configuration, the service responds with a 404 error page.

Server Error in '/v2' Application. The resource cannot be found. Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.

Requested URL: /service/

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.18034



所以,问题:


So, the questions:



  1. 为什么?我在这里反对哪些不可抗拒的力量?
  2. 为路由服务编写WCF配置时,如何设置端点配置?我做得对吗?删除地址属性没有帮助。它应该是其他价值吗? 基本上:当我使用路由时,确保我的WCF配置正确的最佳方法是什么?

推荐答案

您好,



请参阅Rest-Ful服务的示例Web.Config文件详细信息。



两个主要部分负责。



Hi,

Please find the sample Web.Config File details for Rest-Ful service.

The two main parts are responsible.

1.Services Part
    i.Service Endpoint details
2.Behaviours Part
    i.Service Behaviour
   ii.Endpoint Behaviour





示例Web.config文件:



Sample Web.config File:

<configuration>
  <system.web>
    <compilation debug="true" targetframework="4.0" />
  </system.web>
  <system.servicemodel>
    <services>
      <service name="WcfService3.RestImplService" behaviorconfiguration="MyServiceBehavior">
        <endpoint address="">
                  binding="webHttpBinding"
                  contract="WcfService3.IRestImplService"
                      behaviorConfiguration="MyWeb"              />
      </endpoint></service>
    </services>

    <behaviors>
      <servicebehaviors>
        <behavior name="MyServiceBehavior">
          <!-- 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>
      <endpointbehaviors>
        <behavior name="MyWeb">
          <webhttp />
        </behavior>
      </endpointbehaviors>
    </behaviors>

    <servicehostingenvironment multiplesitebindingsenabled="true" />
  </system.servicemodel>
  <system.webserver>
    <modules runallmanagedmodulesforallrequests="true" />
  </system.webserver>
</configuration>









Thanks!





Thanks!


这篇关于在使用路由时,如何在WCF的配置中正确设置服务行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆