domenica, febbraio 06, 2011

How to transform a pure Class Library project into a WCF Service Library

WCF Service Library projects are classic Class Library projects plus some very useful features:
  • they auto-host themselves using the utility WCF Service Host
  • they can be called and tested using the utility WCF Test Client
  • they can be discovered and added in other projects of the same solution using "Add Service Reference..."

(WCF Service Host and WCF Test Client are part of Visual Studio 2010).

The differences between the two type of project are in the .csprojct file. So, you can transform a classis Class Library project inot a WCF Service Library project modifying the .csproj file.

Steps:

* close VS2010
* do a complete backup of the whole solution   :)
* edit the "Class Library" .csproj using notepad (or other text editor)
* under <PropertyGroup> add:
    <ProjectTypeGuids>{3D9AD99F-2412-4246-B90B-4EAA41C64699};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
    <StartArguments>/client:"WcfTestClient.exe"</StartArguments>


    * after <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> add:

    <ProjectExtensions>
    <VisualStudio>
    <FlavorProperties GUID="{3D9AD99F-2412-4246-B90B-4EAA41C64699}">
    <WcfProjectProperties>
    <AutoStart>True</AutoStart>
    </WcfProjectProperties>
    </FlavorProperties>
    </VisualStudio>
    </ProjectExtensions>



    The resulting file should looks like this:

    <?xml version="1.0" encoding="utf-8"?>
    <Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    <ProductVersion>8.0.30703</ProductVersion>
    <SchemaVersion>2.0</SchemaVersion>
    <ProjectGuid>{08536F3E-152A-46C0-8C63-C0821FE5DE7C}</ProjectGuid>
    <OutputType>Library</OutputType>
    <AppDesignerFolder>Properties</AppDesignerFolder>
    <RootNamespace>PureClassLib</RootNamespace>
    <AssemblyName>PureClassLib</AssemblyName>
    <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
    <FileAlignment>512</FileAlignment>
    <ProjectTypeGuids>{3D9AD99F-2412-4246-B90B-4EAA41C64699};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
    <StartArguments>/client:"WcfTestClient.exe"</StartArguments>

    </PropertyGroup>
    <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <DebugSymbols>true</DebugSymbols>
    <DebugType>full</DebugType>
    <Optimize>false</Optimize>
    <OutputPath>bin\Debug\</OutputPath>
    <DefineConstants>DEBUG;TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
    </PropertyGroup>
    <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    <DebugType>pdbonly</DebugType>
    <Optimize>true</Optimize>
    <OutputPath>bin\Release\</OutputPath>
    <DefineConstants>TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
    </PropertyGroup>
    <ItemGroup>
    <Reference Include="System" />
    <Reference Include="System.Core" />
    <Reference Include="System.Runtime.Serialization" />
    <Reference Include="System.ServiceModel" />
    <Reference Include="System.Xml.Linq" />
    <Reference Include="System.Data.DataSetExtensions" />
    <Reference Include="Microsoft.CSharp" />
    <Reference Include="System.Data" />
    <Reference Include="System.Xml" />
    </ItemGroup>
    <ItemGroup>
    <Compile Include="AcmeService.cs" />
    <Compile Include="Class1.cs" />
    <Compile Include="IAcmeService.cs" />
    <Compile Include="Properties\AssemblyInfo.cs" />
    </ItemGroup>
    <ItemGroup>
    <None Include="app.config" />
    </ItemGroup>
    <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
    <ProjectExtensions>
    <VisualStudio>
    <FlavorProperties GUID="{3D9AD99F-2412-4246-B90B-4EAA41C64699}">
    <WcfProjectProperties>
    <AutoStart>True</AutoStart>
    </WcfProjectProperties>
    </FlavorProperties>
    </VisualStudio>
    </ProjectExtensions>

    <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
    Other similar extension points exist, see Microsoft.Common.targets.
    <Target Name="BeforeBuild">
    </Target>
    <Target Name="AfterBuild">
    </Target>
    -->
    </Project>




    A useful link to understand Project Types:  http://www.mztools.com/Articles/2008/MZ2008017.aspx



    Nessun commento: