В PerSession, как мне получить Dispose() в службе для запуска? В коде ниже Dispose() не вызывается. Ни когда я вызываю .Close(), ни когда я позволяю сеансу тайм-аута.WCF Dispose() не вызывается с InstanceContectMode = PerSession
Если я меняю службу на PerCall Dispose() вызывается (при каждом вызове метода). С PerSession я получаю сеанс (тестируется с помощью serviceStartTime).
Сервис
[ServiceBehavior (InstanceContextMode=InstanceContextMode.PerSession)]
public class MagicEightBallService : IEightBall, IDisposable
{
private DateTime serviceStartTime;
public void Dispose()
{
Console.WriteLine("Eightball dispose ... " + OperationContext.Current.SessionId.ToString());
}
public MagicEightBallService()
{
serviceStartTime = DateTime.Now;
Console.WriteLine("Eightball awaits your question " + OperationContext.Current.SessionId.ToString() + " " + serviceStartTime.ToLongTimeString());
}
public string ObtainAnswerToQuestion(string userQuestion)
{
return "maybe " + OperationContext.Current.SessionId.ToString() + " " + serviceStartTime.ToLongTimeString();
}
Client
using (EightBallClient ball = new EightBallClient())
{
while (true)
{
Console.Write("Your question: ");
string question = Console.ReadLine();
if (string.IsNullOrEmpty(question)) break;
try
{
string answer = ball.ObtainAnswerToQuestion(question);
Console.WriteLine("8-ball says: {0}", answer);
}
catch (Exception Ex)
{
Console.WriteLine("ball.ObtainAnswerToQuestion exception " + Ex.Message);
}
}
ball.Close();
}
Сервис-Контракт
[ServiceContract (SessionMode = SessionMode.Required)]
public interface IEightBall
{
[OperationContract]
string ObtainAnswerToQuestion(string userQuestion);
[OperationContract]
sDoc GetSdoc(int sID);
DateTime CurDateTime();
}
Хост
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="WSHttpBinding_ISampleService"
closeTimeout="00:01:00" openTimeout="00:01:00"
receiveTimeout="00:10:00" sendTimeout="00:10:00">
<security mode="Message" />
<reliableSession ordered="true"
inactivityTimeout="00:10:00"
enabled="true" />
</binding>
</wsHttpBinding>
</bindings>
<services>
<service name="MajicEightBallServiceLib.MagicEightBallService"
behaviorConfiguration="EightBallServiceMEXBehavior" >
<endpoint address=""
binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_ISampleService"
contract="MajicEightBallServiceLib.IEightBall">
</endpoint>
<endpoint address="mex"
binding ="mexHttpBinding"
contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8000/MagicEightBallService"/>
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="EightBallServiceMEXBehavior">
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
namespace MagicEightBallServiceHost
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("**** Console Based WCF Host *****");
using (ServiceHost serviceHost = new ServiceHost(typeof(MagicEightBallService)))
{
serviceHost.Open();
Console.WriteLine("The service is running");
Console.ReadLine();
}
}
}
}
Ваш контракт на обслуживание, также указанный с 'Session = true'? Я думаю, что это единственный способ, которым клиент знает сервер, когда это делается с сеансом. –
@MichaelEdenfield Где бы я поставил Session = true? Я пробовал SessionMode = SessionMode.Required, но это не помогло. – Paparazzi
'[ServiceContract (Session = True)]' (Я не знаю, что это был ответ, который я просто знаю, я всегда делаю это с помощью своих служб на основе сеанса.) –