Add behaviorattribute to a WorkflowServiceHost

2019-04-13 14:45发布

Hi all i have a problem while adding a custom behavior to a WorkflowServiceHost.

Here is my WorflowServiceHostFactory:

public class ScoringWorkflowServiceHostFactory : WorkflowServiceHostFactory, IServiceHost<IKernel>
{
    private static IKernel _InjectionInstance;
    public IKernel InjectionInstance
    {
        get { return _InjectionInstance ?? (_InjectionInstance = new StandardKernel(new ScoringWorkflowServicesNinjectModule(Scope))); }
    }

    public object Scope
    {
       get { return Guid.NewGuid(); }      
    }

    public override ServiceHostBase CreateServiceHost(string constructorString, Uri[] baseAddresses)
    {

        String fullFilePath = Path.Combine(HostingEnvironment.ApplicationPhysicalPath, constructorString);

        WorkflowService wf = CSharpExpressionCompiler.Compile(fullFilePath);

        System.ServiceModel.Activities.WorkflowServiceHost host = base.CreateWorkflowServiceHost(wf, baseAddresses);


        NinjectBehaviorAttributeWF behavior = new NinjectBehaviorAttributeWF(wf);

        host.Description.Behaviors.Add(behavior);

        host.AddNinjectResolverExtension(InjectionInstance, Scope);

        TypeAdapterFactory.SetCurrent(new SvcMapperAdapterFactory());

        LoggerFactory.SetCurrent(new EntLibLoggerFactory());

        return host;
    }

}

Here is my behavior:

public class NinjectBehaviorAttributeWF : Attribute, IServiceBehavior
{
private System.ServiceModel.Activities.WorkflowService host;

public NinjectBehaviorAttributeWF(System.ServiceModel.Activities.WorkflowService host)
{

    this.host = host;
}
    public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters)
    {
    }

    public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
    {

        foreach (ChannelDispatcher dispatcher in serviceHostBase.ChannelDispatchers)
        {
            foreach (EndpointDispatcher endpointDispatcher in dispatcher.Endpoints)
            {
                DispatchRuntime dispatchRuntime = endpointDispatcher.DispatchRuntime;

                dispatchRuntime.InstanceContextProvider = new PerCallInstanceContextProvider(dispatchRuntime);
            }
        }
    }

    public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
    {
    }
}

In this way, i have an error while loading my service(xamlx): The service type provided could not be loaded as a service because it does not have a default (parameter-less) constructor. To fix the problem, add a default constructor to the type, or pass an instance of the type to the host.

I don't know neither it's possible, nor how can create the default constructor for a workflowservice, (because the real implementation is the xamlx and not a simple class)

So, I've tried with a custom Provider:

 dispatchRuntime.InstanceProvider = new CustomInstanceProvider(host.Body);

where CustomInstanceProvider is:

public class CustomInstanceProvider : IInstanceProvider
{
string message;
private System.Activities.Activity activity;

public CustomInstanceProvider(string msg)
{
Console.WriteLine("The non-default constructor has been called.");
this.message = msg;

}

public CustomInstanceProvider(System.Activities.Activity activity)
{
  this.activity = activity;
}

public object GetInstance(InstanceContext instanceContext,   System.ServiceModel.Channels.Message message)
{
Console.WriteLine("GetInstance is called:");
return this.activity;
}

public object GetInstance(InstanceContext instanceContext)
{
  Console.WriteLine("GetInstance is called:");
  return this.activity;
}

public void ReleaseInstance(InstanceContext instanceContext, object instance)
{
Console.WriteLine("ReleaseInstance is called");
}
}

But i have this error:

System.InvalidCastException: Unable to cast object of type 'System.ServiceModel.Activities.WorkflowService' to type 'IHttpGetMetadata'.

How can I resolve my problem? Thanks a lot

0条回答
登录 后发表回答