WCF中的有状态服务是否支持会话?
尝试了以下属性,但它不起作用。
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
公共类CalculatorService:ICalculator
[的ServiceContract(SessionMode = SessionMode.Required)]
public interface ICalculator
我需要做哪些更改WCFCommunicationListener来支持会话?
Uri resourceAddress;
if (!Helpers.TryGetUri(pcHost + pcPort + "/api/code/scan", out resourceAddress))
{
rootPage.NotifyUser("Invalid URI.", NotifyType.ErrorMessage);
return;
}
try
{
terminalRef = "1";
//code = "Uc0E17G4nW";
IHttpContent jsonContent = new HttpJsonContent(JsonValue.Parse("{\"code\":\"" + code +
",\"ref\" : \""+ terminalRef +
"\"}"));
HttpResponseMessage response = await httpClient.PostAsync(resourceAddress, jsonContent).AsTask(cts.Token);
Debug.WriteLine(".");
//await Helpers.DisplayTextResultAsync(response, cts.Token);
rootPage.NotifyUser("Completed", NotifyType.StatusMessage);
}
catch (Exception ex)
{
rootPage.NotifyUser("Error: " + ex.Message, NotifyType.ErrorMessage);
String errorMessage = ex.Message.ToString();
}
答案 0 :(得分:0)
我可以使用以下代码在WCF状态服务中实现会话。
protected override IEnumerable<ServiceReplicaListener> CreateServiceReplicaListeners()
{
return new[]
{
new ServiceReplicaListener( (context) =>
{
var wcfCommunicationListener = new WcfCommunicationListener<ICalculator>(context, new CalculatorService(),WcfUtility.CreateTcpListenerBinding(),"WCFServiceEndpoint");
wcfCommunicationListener.ServiceHost.Description.Behaviors.Remove(typeof(ServiceBehaviorAttribute));
wcfCommunicationListener.ServiceHost.Description.Behaviors.Add(new ServiceBehaviorAttribute() { InstanceContextMode = InstanceContextMode.PerSession});
return wcfCommunicationListener;
}
)
};
}