aspNetCompatibilityEnabled = “真”

时间:2011-09-10 23:17:42

标签: wcf asp.net-ajax azure

我创建了一个Azure Web应用程序,其中包含一个ASP.NET Web,其中还包含一些JSON WCF服务。我真的不太了解WCF服务模型,以确保我做得对,这看起来对你来说是否正确?是否有其他服务模型配置更好的可扩展性,更多的最大并发连接等?

      <system.serviceModel>
        <behaviors>
          <serviceBehaviors>
            <behavior name="">
              <serviceMetadata httpGetEnabled="true" />
              <serviceDebug includeExceptionDetailInFaults="true" />
            </behavior>
          </serviceBehaviors>
        </behaviors>
        <serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true" />
      </system.serviceModel>
  <system.net>
    <settings>
      <!-- See http://social.msdn.microsoft.com/Forums/en-US/windowsazuredata/thread/d84ba34b-b0e0-4961-a167-bbe7618beb83 -->
      <servicePointManager expect100Continue="false" />
    </settings>
  </system.net>  

这样可行但我偶尔会在我的开发环境中遇到意外的连接丢失(超时)而没有HTTP错误代码让我担心。

2011年11月24日更新

的web.config

  <system.net>
    <connectionManagement>
      <!-- See http://social.msdn.microsoft.com/Forums/en-US/windowsazuredata/thread/d84ba34b-b0e0-4961-a167-bbe7618beb83 -->
      <add address="*" maxconnection="48" />
    </connectionManagement>
  </system.net>  

我怀疑可能是Visual Studio Web服务器导致Ajax调用超时,几分钟后服务开始再次接受请求。这是我的完整设置,你能看出问题是什么吗?我只对该服务进行了一次Ajax调用。

Inferface

IExample.cs:

using System.ServiceModel;
using System.ServiceModel.Web;

namespace WebPages.Interfaces
{
    [ServiceContract]
    public interface IExample
    {
        [OperationContract]
        [WebInvoke(Method = "GET",
            ResponseFormat = WebMessageFormat.Json)]
        string GetSomething(string id);
    }
}

ExampleService.svc.cs标记

<%@ ServiceHost Language="C#" Debug="true" Service="WebPages.Interfaces.ExampleService" CodeBehind="ExampleService.svc.cs" Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>

ExampleService.svc.cs codebehind

namespace WebPages.Interfaces
{
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class ExampleService : IExample
    {
        string JsonSerializeSomething(Something something)
        {
            var serializer = new DataContractJsonSerializer(something.GetType());
            var memoryStream = new MemoryStream();

            serializer.WriteObject(memoryStream, something);

            return Encoding.Default.GetString(memoryStream.ToArray());
        }

        public string GetSomething(string id)
        {
            var something = DoSomeBusinessLogic(id);

            return JsonSerializeSomething(something);
        }
    }
}

来自客户端的jQuery调用

function _callServiceInterface(id, delegate) {
    var restApiCall = "Interfaces/ExampleService.svc/GetSomething?id="
            + escape(id);

    $.getJSON(restApiCall, delegate);
}

function _getSomethingFromService() {
    _callServiceInterface('123',
        function (result) {
            var parsedResult = $.parseJSON(result);
            $('#info').html(result.SomethingReturnedFromServiceCall);
        }
    );
}

更新

我想我知道现在的问题是什么; WCF服务似乎是默认的单一威胁(来源:http://msdn.microsoft.com/query/dev10.query?appId=Dev10IDEF1&l=EN-US&k=k(SYSTEM.SERVICEMODEL.SERVICEBEHAVIORATTRIBUTE.CONCURRENCYMODE);k(TargetFrameworkMoniker-%22.NETFRAMEWORK%2cVERSION%3dV4.0%22);k(DevLang-CSHARP)&rd=true)。这解释了为什么我的Ajax调用获得超时,被另一个线程阻止。这段代码应该可以更好地工作:

ExampleService.svc.cs

[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple, InstanceContextMode = InstanceContextMode.PerSession,
         IncludeExceptionDetailInFaults = false, MaxItemsInObjectGraph = Int32.MaxValue)]
    //[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class ExampleService : IExample

的web.config

 <system.serviceModel>
    <protocolMapping>
      <add scheme="http" binding="webHttpBinding" bindingConfiguration="" />
    </protocolMapping>
    <behaviors>
      <endpointBehaviors>
        <behavior name="">
          <webHttp defaultOutgoingResponseFormat="Json" />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>  

ExampleService.svc

<%@ ServiceHost Language="C#" Debug="true" Service="WebPages.Interfaces.TagService" CodeBehind="TagService.svc.cs" %>

2011年10月9日更新

我想我得到了我需要的答案Locking with ConcurrencyMode.Multiple and InstanceContextMode.PerCall

aspNetCompatibilityEnabled="false"表示无法在我的WCF代码中访问HttpContext,ASP.NET会话等。

1 个答案:

答案 0 :(得分:3)

我想我得到了我需要的答案Locking with ConcurrencyMode.Multiple and InstanceContextMode.PerCall

aspNetCompatibilityEnabled="false"表示无法在我的WCF代码中访问HttpContext,ASP.NET会话等。