Creating custom activity design without reference to Design DLL in Windows Workflow Foundation

[亡魂溺海] 提交于 2019-12-12 17:23:22

问题


I am using Windows Workflow Foundation with custom Activities, and I would like to create custom Design for these activities in my workflow.

I am able to make the design project, and the designer xaml. Also I am able to see the custom design for them in the workflow, if I directly refer the Design project in my Workflow project.

This is something that I would not like to do, because the Designer DLL should not be deployed to production environment. I would only like to have the custom design in Visual Studio workflow editor.

I was able to get things working by adding following:

[Designer("namespace,dll")]
public class CustomActivity : NativeActivity<string>

and after this copying the dll to visual studio path. This is again something that I would not like to do, because every developer should do this and making the build so that the dll is copied some fixed visual studio path is not very good.

I used these two examples, but it seems that both of these directly refer the DLL:

  • http://geekswithblogs.net/jkurtz/archive/2010/01/26/137639.aspx

  • http://code.msdn.microsoft.com/Windows-Workflow-9e867448

I would assume this kind of feature would somehow be supported by the Visual Studio/Workflow Foundation.

Do you have any ideas how to resolve this? Thanks!


回答1:


The Designer attribute (using "magic" string) is not something very reliable. If you change your class name or namespace, you'll not have a compilation error. There is another (better imho) way to do it using IRegisterMetadata implementation:

  1. Your Design assembly must reference your activity assembly, but this usually can't be avoided.
  2. Add a partial class (.cs) to your XAML designer
  3. This class must inherit from System.Activities.Presentation.Metadata.IRegisterMetadata. this interface only define one method to implement.

Here is a implementation sample:

public void Register()
{
    AttributeTableBuilder builder = new AttributeTableBuilder();
    builder.AddCustomAttributes(
        typeof(MyActivity),
        new DesignerAttribute(typeof(MyActivityDesigner)));
    MetadataStore.AddAttributeTable(builder.CreateTable());
}

Next, you'll want to have the custom designer used in Visual Studio. Visual Studio have strict rules to automatically load designer assemblies. You need:

  1. Your designer project must have the same name than your activity project, with ".Design" added at the end. Example:
    • Activiy project: MyApp.Activities.dll
    • Designer project: MyApp.Activities.Design.dll
  2. The .Design dll must be in the same directory than the activity dll. You can automate this with a post build event in the designer project.

Important Edit:

I see now that your links already present this method but you say that it directly reference the DLL. Yes, the DESIGN dll reference the ACTIVITY dll. But you asked the opposite: the activity dll shouldn't reference the design dll. Using the IRegisterMetadata method, your DESIGN dll can reference the ACTIVITY dll, it's not a problem: you can remove the design dll from the released package, the activity dll will work fine.



来源:https://stackoverflow.com/questions/21888523/creating-custom-activity-design-without-reference-to-design-dll-in-windows-workf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!