本文整理匯總了C#中System.ServiceModel.InstanceContext.GetServiceInstance方法的典型用法代碼示例。如果您正苦於以下問題:C# InstanceContext.GetServiceInstance方法的具體用法?C# InstanceContext.GetServiceInstance怎麽用?C# InstanceContext.GetServiceInstance使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.ServiceModel.InstanceContext
的用法示例。
在下文中一共展示了InstanceContext.GetServiceInstance方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: BeforeInvoke
public object BeforeInvoke(
InstanceContext instanceContext,
IClientChannel channel,
Message message)
{
ComPlusServerSecurity serverSecurity = null;
WindowsImpersonationContext impersonateContext = null;
bool errorTraced = false;
WindowsIdentity identity = null;
Uri from = null;
object instance = null;
int instanceID = 0;
string action = null;
TransactionProxy proxy = null;
Transaction tx = null;
Guid incomingTransactionID = Guid.Empty;
// The outer try block is to comply with FXCOP's WrapVulnerableFinallyClausesInOuterTry rule.
try
{
try
{
identity = MessageUtil.GetMessageIdentity(message);
if (message.Headers.From != null)
from = message.Headers.From.Uri;
instance = instanceContext.GetServiceInstance(message);
instanceID = instance.GetHashCode();
action = message.Headers.Action;
ComPlusMethodCallTrace.Trace(TraceEventType.Verbose, TraceCode.ComIntegrationInvokingMethod,
SR.TraceCodeComIntegrationInvokingMethod, this.info, from, action, identity.Name, iid, instanceID, false);
// Security
//
if (this.info.CheckRoles)
{
if (!this.comAuth.IsAuthorizedForOperation(identity))
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(Error.CallAccessDenied());
}
}
if (this.info.HostingMode != HostingMode.WebHostOutOfProcess)
{
// NOTE: This has the side effect of setting up
// the COM server security thing, so be sure
// to clear it with Dispose() eventually.
//
serverSecurity = new ComPlusServerSecurity(identity,
this.info.CheckRoles);
}
// Transactions
//
proxy = instanceContext.Extensions.Find<TransactionProxy>();
if (proxy != null)
{
// This makes the Tx header Understood.
tx = MessageUtil.GetMessageTransaction(message);
if (tx != null)
{
incomingTransactionID = tx.TransactionInformation.DistributedIdentifier;
}
try
{
if (tx != null)
{
proxy.SetTransaction(tx);
}
else
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(Error.TransactionMismatch());
}
ComPlusMethodCallTrace.Trace(TraceEventType.Verbose, TraceCode.ComIntegrationInvokingMethodNewTransaction,
SR.TraceCodeComIntegrationInvokingMethodNewTransaction, this.info, from, action, identity.Name, iid, instanceID, incomingTransactionID);
}
catch (FaultException e)
{
Transaction txProxy = proxy.CurrentTransaction;
Guid currentTransactionID = Guid.Empty;
if (txProxy != null)
currentTransactionID = txProxy.TransactionInformation.DistributedIdentifier;
string identityName = String.Empty;
if (null != identity)
identityName = identity.Name;
DiagnosticUtility.EventLog.LogEvent(TraceEventType.Error,
(ushort)System.Runtime.Diagnostics.EventLogCategory.ComPlus,
(uint)System.Runtime.Diagnostics.EventLogEventId.ComPlusInvokingMethodFailedMismatchedTransactions,
incomingTransactionID.ToString("B").ToUpperInvariant(),
currentTransactionID.ToString("B").ToUpperInvariant(),
from.ToString(),
//.........這裏部分代碼省略.........
示例2: BeforeInvoke
public object BeforeInvoke(InstanceContext instanceContext, IClientChannel channel, Message message)
{
ComPlusServerSecurity serverSecurity = null;
WindowsImpersonationContext context = null;
object obj3;
bool flag = false;
WindowsIdentity clientIdentity = null;
Uri from = null;
int instanceID = 0;
string action = null;
TransactionProxy proxy = null;
Transaction messageTransaction = null;
Guid empty = Guid.Empty;
try
{
try
{
clientIdentity = MessageUtil.GetMessageIdentity(message);
if (message.Headers.From != null)
{
from = message.Headers.From.Uri;
}
instanceID = instanceContext.GetServiceInstance(message).GetHashCode();
action = message.Headers.Action;
ComPlusMethodCallTrace.Trace(TraceEventType.Verbose, 0x50018, "TraceCodeComIntegrationInvokingMethod", this.info, from, action, clientIdentity.Name, this.iid, instanceID, false);
if (this.info.CheckRoles && !this.comAuth.IsAuthorizedForOperation(clientIdentity))
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(Error.CallAccessDenied());
}
if (this.info.HostingMode != HostingMode.WebHostOutOfProcess)
{
serverSecurity = new ComPlusServerSecurity(clientIdentity, this.info.CheckRoles);
}
proxy = instanceContext.Extensions.Find<TransactionProxy>();
if (proxy != null)
{
messageTransaction = MessageUtil.GetMessageTransaction(message);
if (messageTransaction != null)
{
empty = messageTransaction.TransactionInformation.DistributedIdentifier;
}
try
{
if (messageTransaction == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(Error.TransactionMismatch());
}
proxy.SetTransaction(messageTransaction);
ComPlusMethodCallTrace.Trace(TraceEventType.Verbose, 0x5001a, "TraceCodeComIntegrationInvokingMethodNewTransaction", this.info, from, action, clientIdentity.Name, this.iid, instanceID, empty);
goto Label_02DC;
}
catch (FaultException exception)
{
Transaction currentTransaction = proxy.CurrentTransaction;
Guid distributedIdentifier = Guid.Empty;
if (currentTransaction != null)
{
distributedIdentifier = currentTransaction.TransactionInformation.DistributedIdentifier;
}
string name = string.Empty;
if (clientIdentity != null)
{
name = clientIdentity.Name;
}
DiagnosticUtility.EventLog.LogEvent(TraceEventType.Error, EventLogCategory.ComPlus, (EventLogEventId) (-1073610725), new string[] { empty.ToString("B").ToUpperInvariant(), distributedIdentifier.ToString("B").ToUpperInvariant(), from.ToString(), this.info.AppID.ToString("B").ToUpperInvariant(), this.info.Clsid.ToString("B").ToUpperInvariant(), this.iid.ToString(), action, instanceID.ToString(CultureInfo.InvariantCulture), Thread.CurrentThread.ManagedThreadId.ToString(CultureInfo.InvariantCulture), System.ServiceModel.ComIntegration.SafeNativeMethods.GetCurrentThreadId().ToString(CultureInfo.InvariantCulture), name, exception.ToString() });
flag = true;
throw;
}
}
ComPlusMethodCallTrace.Trace(TraceEventType.Verbose, 0x5001b, "TraceCodeComIntegrationInvokingMethodContextTransaction", this.info, from, action, clientIdentity.Name, this.iid, instanceID, true);
Label_02DC:
if (this.info.HostingMode == HostingMode.WebHostOutOfProcess)
{
context = clientIdentity.Impersonate();
}
CorrelationState state = new CorrelationState(context, serverSecurity, from, action, clientIdentity.Name, instanceID);
context = null;
serverSecurity = null;
obj3 = state;
}
finally
{
if (context != null)
{
context.Undo();
}
if (serverSecurity != null)
{
((IDisposable) serverSecurity).Dispose();
}
}
}
catch (Exception exception2)
{
if (!flag && DiagnosticUtility.ShouldTraceError)
{
DiagnosticUtility.EventLog.LogEvent(TraceEventType.Error, EventLogCategory.ComPlus, (EventLogEventId) (-1073610727), new string[] { (from == null) ? string.Empty : from.ToString(), this.info.AppID.ToString("B").ToUpperInvariant(), this.info.Clsid.ToString("B").ToUpperInvariant(), this.iid.ToString("B").ToUpperInvariant(), action, instanceID.ToString(CultureInfo.InvariantCulture), Thread.CurrentThread.ManagedThreadId.ToString(CultureInfo.InvariantCulture), System.ServiceModel.ComIntegration.SafeNativeMethods.GetCurrentThreadId().ToString(CultureInfo.InvariantCulture), clientIdentity.Name, exception2.ToString() });
}
throw;
}
//.........這裏部分代碼省略.........