将行为属性添加到WorkflowServiceHost

大家好我在向WorkflowServiceHost添加自定义行为时遇到问题。

这是我的WorflowServiceHostFactory:

public class ScoringWorkflowServiceHostFactory : WorkflowServiceHostFactory, IServiceHost { 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; } } 

这是我的行为:

 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 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) { } } 

这样,我在加载服务时出错(xamlx):提供的服务类型无法作为服务加载,因为它没有默认(无参数)构造函数。 要解决此问题,请为该类型添加默认构造函数,或将该类型的实例传递给主机。

我不知道它是否可能,也不知道如何为工作流服务创建默认构造函数(因为真正的实现是xamlx而不是简单的类)

所以,我尝试过使用自定义提供程序:

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

CustomInstanceProvider的位置是:

 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"); } } 

但我有这个错误:

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

我该如何解决我的问题? 非常感谢