开发者

WCF Callback implementation: Service is consistently returning a null operation context

开发者 https://www.devze.com 2022-12-11 08:57 出处:网络
I\'m currently attemping to display the progress of an SSIS package that\'s running on the server by implementing a callback in WCF. This is working to a point; basically the OperationContext.Current

I'm currently attemping to display the progress of an SSIS package that's running on the server by implementing a callback in WCF. This is working to a point; basically the OperationContext.Current is returning null after a few listener events have already happened (roughly 30 seconds after the initial SSIS package execute call). Here's some of the code that I'm using:

WCF Service method:

var dts = new Microsoft.SqlServer.Dts.Runtime.Application();

PackageEventListener listener = new PackageEventListener();
listener.OnPackageProgress += new PackageEventListener.PackageProgressChangedHandler(listener_OnPackageProgress);
...
if (package.Execute(null, null, listener, null, null) == DTSExecResult.Failure)
...

private static void listener_OnPackageProgress(object package, EventArgs packageinfo)
{
    var context = System.ServiceModel.OperationContext.Current;
    if (context == null) return;

    IXLoadMarriageCallback callback = context.GetCallbackChannel<IXLoadMarriageCallback>();
    if (callback != null)
    {
        PackageProgressEventArgs eventArgs = (PackageProgressEventArgs)packageinfo;
        callback.OnProgressCallback(eventArgs.ProgressDescription);
    }
}

public interface IXLoadMarriageCallback
{
    [OperationContract(IsOneWay = true)]
    void OnProgressCallback(string message);
}

(PackageEventListener is a class that inherits from Microsoft.SqlServer.Dts.Runtime.DefaultEvents)

The service is marked up as follows:

[ServiceBehavior(
    InstanceContextMode = InstanceContextMode.PerSession,
    ConcurrencyMode = ConcurrencyMode.Reentrant
)]
public class IntegrationServicesService : IIntegrationServicesService

And the service interface is marked up as follows:

[ServiceContract(
    CallbackContract = typeof(Business.IXLoadMarriageCallback),
    SessionMode = SessionMode.Required
)]
public interface IIntegrationServicesService

I'm using net tcp bindings, service-side config as follows:

<bindings>
    <netTcpBinding>
        <binding name="LongReliableTCPBinding"
            closeTimeout="10:01:00" openTimeout="10:01:01" receiveTimeout="08:00:01" sendTimeout="08:00:02"
            transactionFlow="false" transferMode="Buffered" transactionProtocol="OleTransactions"
            hostNameComparisonMode="StrongWildcard" listenBacklog="10" maxBufferPoolSize="4194304" maxBufferSize="4194304" maxConnections="10" maxReceivedMessageSize="4194304">
            <readerQuotas maxDepth="32" maxStringContentLength="4194304" maxArrayLength="4194304" maxBytesPerRead="4194304" maxNameTableCharCount="4194304" />
            <reliableSession ordered="true" inactivityTimeout="08:00:03" enabled="true" />
            <security mode="Transport">
                <transport clientCredentialType="Windows" protectionL开发者_如何转开发evel="EncryptAndSign" />
                <message clientCredentialType="Windows" />
            </security>
        </binding>
    </netTcpBinding>
</bindings>

I can't see any exceptions being thrown. Apologies for the rather incomplete code sample as there's quite a lot going on (just to get an indication of progress!).

As requested, here's some of the client code that calls the service method (it's a presenter):

public class XLoadPresenter : IIntegrationServicesServiceCallback, IDisposable
{
    private IXLoadView view;
    private readonly IIntegrationServicesServiceClient integrationServicesServiceClient;
    private readonly IXLoadServiceClient xloadServiceClient;

    public XLoadPresenter(IXLoadServiceClient xloadServiceClient)
    {
        var context = new System.ServiceModel.InstanceContext(this);
        this.integrationServicesServiceClient = new IntegrationServicesServiceClient(context);

        this.xloadServiceClient = xloadServiceClient;
    }

    ...

    private void InvokeXLoadMarriages()
    {
        integrationServicesServiceClient.BeginXLoadMarriage(view.Database, OnEndXLoadMarriages, null);
    }
    ...
}


Have figured out what was happening. The problem lies in my implementation of the DefaultEvents class:

public class PackageEventListener : DefaultEvents
{
    public PackageProgressChangedHandler OnPackageProgress;
    public PackagePostExecuteHandler OnPackagePostExecute;

    public delegate void PackageProgressChangedHandler(object package, EventArgs packageInfo);
    public delegate void PackagePostExecuteHandler(object package, EventArgs packageInfo);

    public override void OnProgress(TaskHost taskHost, string progressDescription, int percentComplete, int progressCountLow, int progressCountHigh, string subComponent, ref bool fireAgain)
    {
        OnPackageProgress(this, new PackageProgressEventArgs(taskHost, progressDescription, subComponent));

        base.OnProgress(taskHost, progressDescription, percentComplete, progressCountLow, progressCountHigh, subComponent, ref fireAgain);
    }

    public override void OnPostExecute(Executable exec, ref bool fireAgain)
    {
        OnPackagePostExecute(this, new PackagePostExecuteEventArgs(exec));

        base.OnPostExecute(exec, ref fireAgain);
    }
}

I was overriding OnPostExecute but hadn't attached a handler to it, i.e. in the code sample listed in the question I had:

listener.OnPackageProgress += new PackageEventListener.PackageProgressChangedHandler(listener_OnPackageProgress);

but no:

listener.OnPackagePostExecute += PackageEventListener.PackagePostExecuteHandler(listener_OnPackagePostExecute);

So the overridden OnPostExecute was throwing a 'hidden' null reference exception (in the sense that no exception was being reported and the OperationContext.Current was being set to null).

I've fixed it by simply defaulting an empty delegate, i.e.:

public PackageProgressChangedHandler OnPackageProgress = delegate { };
public PackagePostExecuteHandler OnPackagePostExecute = delegate { };
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号