wcf - aspNet兼容性启用="true"

标签 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 调用。

接口(interface)

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 代码隐藏

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 session 等。

最佳答案

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

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

关于wcf - aspNet兼容性启用="true",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7375311/

相关文章:

c# - 使用 CORS 通过 IIS 访问 WCF REST 服务在本地工作,但不能从连接的设备工作

ruby-on-rails - 带有自定义绑定(bind)的 Savon

javascript - 使用 ASP.Net ajax 库进行跨浏览器 Xml 操作

asp.net - 同时进行多个异步回发 - ASP.NET

c# - 为 Web 服务创建磁盘缓存的最佳方法

c# - 我应该按什么顺序增加我的 ASP.NET 知识?

azure - 如何仅获取 Azure key 保管库的部分 Powershell 输出?

Azure Function - 挂起队列触发器

c# - 如何从 Azure Function (HTTP) 返回数据流

c# - 将 Entity Framework 模型转换为数据契约