How to reference two versions of an API?

不羁岁月 提交于 2019-12-18 03:39:34

问题


I have a need to reference two different versions of the Sharepoint API dll. I have a webservice that needs to run under both Sharepoint 2 and Sharepoint 3, but also needs to work with new features provided by the Sharepoint 3 API (Checkout and Content Approval)

What is the best way to acheive this - I'm currently leaning towards having two projects, with the code in a single file shared between the two with various sections of the code compiled in using conditional compilation.

Is there a better way ?

Thanks

Matt


回答1:


This is how I spit out .NET 1.1 versions compiled against WSSv2 API and .NET 2.0 compiled against WSSv3 assembly. It will work for VS 2005 and 2008.

You will need to use MSBEE http://www.codeplex.com/Wiki/View.aspx?ProjectName=MSBee

Working with .NET 1.1 with Visual Studio 2008

Some tips

Open up *.csproj and find out where the SharePoint dll is referenced and change to something like this which changes the referenced assembly depending upon your target (FX1_1 means you are targeting .NET1.1 and therefore WSSv2)

<Reference Include="Microsoft.SharePoint">
  <HintPath Condition="'$(TargetFX1_1)'!='true'">pathto\WSS3\Microsoft.SharePoint.dll</HintPath>
  <HintPath Condition="'$(TargetFX1_1)'=='true'">pathto\WSS2\Microsoft.SharePoint.dll</HintPath>
</Reference>

Use conditional compilation for differences where necessary

#if FX1_1  
    // WSSv2 specific code  
#else  
    // WSSv3 specific code  
#endif

If you get a compiler error but the code looks right it may be that the error is only for .NET1.1 / WSSv2 and compiles fine in .NET2/WSSv3. Check the output tab to see for which target the error occurred

You will also need to master some MSBUILD ninja moves to keep a 1 step build process and keep yourself sane http://brennan.offwhite.net/blog/2006/11/30/7-steps-to-msbuild/ using MSBUILD you can get VS to compile both versions at the same time without resorting to the command line.

This will run the .NET1.1 compilation after .NET has finished and output some messages to the Output window to help you work out where errors occurred.

<Target Name="BeforeBuild">
    <Message Text="--- Building for .NET 1.1 ---" Importance="high" Condition="'$(TargetFX1_1)'=='true'" />
    <Message Text="--- Building for .NET 2.0 ---" Importance="high" Condition="'$(TargetFX1_1)'!='true'" />
</Target>
<Target Name="AfterBuild" Condition="'$(TargetFX1_1)'!='true'">
    <MSBuild Projects="$(MSBuildProjectFile)" Properties="TargetFX1_1=true;" />
</Target>



回答2:


You could give an "extern alias" a go.

This is one of those times when the VB late binding (option strict off) approach works well. Roll on C# 4.0 and dynamic.

You might try writing an interface for the bits you need (in a base library), and write 2 dlls: one referencing each version of the sharepoint dll. For both projects, implement the interface (throwing NotSupportedException for the bits you can't do), and load the appropriate dll at runtime? (factory approach)

Just try it with a single method before you get too absorbed... don't do the whole thing until you know it works for the simplest of simple methods.



来源:https://stackoverflow.com/questions/286826/how-to-reference-two-versions-of-an-api

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