這篇文章http://social.msdn.microsoft.com/Forums/en-US/windowsazuredata/thread/d84ba34b-b0e0-4961-a167-bbe7618beb83涵蓋了Azure的性能問題。
默認情況下,Azure角色只能在單個線程中運行,這在服務器上效率非常低。有一些非常好的設計模式,向您展示如何實現多線程Azure角色,我個人會遵循這一個http://www.31a2ba2a-b718-11dc-8314-0800200c9a66.com/2010/12/running-multiple-threads-on-windows.html。有了這個你的角色可以並行地串行化對象。
我使用JSON作爲交換格式而不是XML,它具有小得多的字節大小,並且在.NET 4中得到很好的支持。我目前使用DataContractJsonSerializer
,但如果是串行化,您也可以查看JavaScriptSerializer
或JSON.NET我會建議你比較這些表現後你的表現。
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)。下面是一個代碼示例,這將使你的問題的REST API的多線程:
ExampleService.svc.cs
[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple, InstanceContextMode = InstanceContextMode.PerCall,
IncludeExceptionDetailInFaults = false, MaxItemsInObjectGraph = Int32.MaxValue)]
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.ExampleService" CodeBehind="ExampleService.svc.cs" %>
另外,ASP.NET默認只允許兩個併發的HTTP連接(源請參閱http://social.msdn.microsoft.com/Forums/en-US/windowsazuredata/thread/d84ba34b-b0e0-4961-a167-bbe7618beb83)。這些設置將允許多達48個併發的HTTP連接:
的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>
如果你的HTTP POST體消息通常是你應該把Nagling的提高性能的小於1460個字節(源http://social.msdn.microsoft.com/Forums/en-US/windowsazuredata/thread/d84ba34b-b0e0-4961-a167-bbe7618beb83)。下面是一些設置,這是否:
的web.config
<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>
定義你的JSON API的是這樣的:
using System.ServiceModel;
using System.ServiceModel.Web;
using Interchange;
namespace WebPages.Interfaces
{
[ServiceContract]
public interface IExample
{
[OperationContract]
[WebInvoke(Method = "POST",
BodyStyle = WebMessageBodyStyle.Bare,
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json)]
string GetUpdates(RequestUpdates name);
[OperationContract]
[WebInvoke(Method = "POST",
BodyStyle = WebMessageBodyStyle.Bare,
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json)]
string PostMessage(PostMessage message);
}
}
可序列化JSON英寸NET 4這樣的:
string SerializeData(object data)
{
var serializer = new DataContractJsonSerializer(data.GetType());
var memoryStream = new MemoryStream();
serializer.WriteObject(memoryStream, data);
return Encoding.Default.GetString(memoryStream.ToArray());
}
一個典型的交換實體可以定義爲正常:
using System.Collections.Generic;
using System.Runtime.Serialization;
namespace Interchange
{
[DataContract]
public class PostMessage
{
[DataMember]
public string Text { get; set; }
[DataMember]
public List<string> Tags { get; set; }
[DataMember]
public string AspNetSessionId { get; set; }
}
}
你可以寫你自己的上游gzip壓縮的HttpModule,但我會嘗試以上第一的東西。
最後,請確保您的表存儲與使用它們的服務位於相同的位置。
你發現什麼是問題/解決方案? – Rory