I created an Azure web application with an ASP.NET website that also has some WCF JSON services. I really donβt know enough about WCF service models to be sure that I am doing it right, does it look right? Are there other configurations of service models that are better suited for scalability, more maximum concurrent connections, etc.?
<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> <servicePointManager expect100Continue="false" /> </settings> </system.net>
This works, but sometimes I get unexpected connections (timeouts) without HTTP error codes in my development environment that bother me.
Update @ 24. November 2011
web.config
<system.net> <connectionManagement> <add address="*" maxconnection="48" /> </connectionManagement> </system.net>
I suspect that the Visual Studio web server may call Ajax calls to get timeouts, after a few minutes the service will start accepting requests again. Here is my complete setup, can you understand what the problem is? I have only one Ajax call for the service.
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); } }
Example markup 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 call from client
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); } ); }
Update
I think I know what the problem is right now; WCF services seem to be single by default (source: 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 ). This explains why my Ajax calls get timeouts blocked by another thread. This code should work much better:
ExampleService.svc.cs
[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple, InstanceContextMode = InstanceContextMode.PerSession, IncludeExceptionDetailInFaults = false, MaxItemsInObjectGraph = Int32.MaxValue)]
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" %>
Update @ 9. Oct. 2011
I think I got the answer that I need here Lock with ConcurrencyMode.Multiple and InstanceContextMode.PerCall
aspNetCompatibilityEnabled="false" means inability to access HttpContext , ASP.NET sessions, etc. in my WCF code.