بررسی متد های یک طرفه در WCF
نویسنده: مسعود پاکدل
تاریخ: ۱۳۹۲/۰۸/۱۶ ۹:۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
[ServiceContract]
public interface ISampleService
{
[OperationContract]
void Wait();
} public class SampleService : ISampleService
{
public void Wait()
{
Thread.Sleep( new TimeSpan( 0, 1, 0 ) );
}
} class Program
{
static void Main( string[] args )
{
SampleService.SampleServiceClient client = new SampleService.SampleServiceClient();
Console.WriteLine( DateTime.Now );
client.Wait();
Console.WriteLine( DateTime.Now );
Console.ReadKey();
}
}
<basicHttpBinding>
<binding name="BasicHttpBinding_ISampleService" sendTimeout="0:2:0"/>
</basicHttpBinding>
[ServiceContract]
public interface ISampleService
{
[OperationContract( IsOneWay = true )]
void Wait();
}
private class OperationContainerExtension : IExtension<OperationContext>
{
public OperationContainerExtension( IDatabaseContext dbContext, string contextKey )
{
this.CurrentDbContext = dbContext;
this.ContextKey = contextKey;
}
public IDatabaseContext CurrentDbContext
{
get;
private set;
}
public string ContextKey
{
get;
private set;
}
public void Attach( OperationContext owner )
{
}
public void Detach( OperationContext owner )
{
}
} if ( OperationContext.Current != null )
{
OperationContext.Current.Extensions.Add( new OperationContainerExtension( dbContext , CONTEXTKEY ) );
OperationContext.Current.OperationCompleted += CurrentOperationContext_OperationCompleted;
OperationContext.Current.Channel.Faulted += Channel_Faulted;
} void Channel_Faulted( object sender, EventArgs e )
{
IDatabaseContext dbContext = GetDbContext();
if ( dbContext != null )
{
dbContext.Dispose();
GC.Collect();
}
}
private void CurrentOperationContext_OperationCompleted( object sender, EventArgs e )
{
IDatabaseContext dbContext = GetDbContext();
if ( dbContext != null )
{
dbContext.Dispose();
GC.Collect();
}
} protected override IDatabaseContext GetDbContext()
{
if ( OperationContext.Current != null )
{
var operationContainerExtension = OperationContext.Current.Extensions.OfType<OperationContainerExtension>().FirstOrDefault( e => e.ContextKey == CONTEXTKEY );
if ( operationContainerExtension != null )
{
return operationContainerExtension.CurrentDbContext;
}
return staticDbContext;
}
else
return staticDbContext;
} private string CONTEXTKEY = Guid.NewGuid().ToString();