Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What steps do I need to take to convert from a class library to a WCF?

I created a project as a Class Library. Now I need to make it into a WCF. I can create a WCF project, but I would like to avoid all that fuss with TFS. I've done the App.config and added the /client:"wcfTestClient.exe" line to the Command line arguments. But there seems to be something else missing from it launching the Hosting.

like image 811
Yuriy Faktorovich Avatar asked Nov 18 '09 21:11

Yuriy Faktorovich


People also ask

How do I create a WCF library project?

Open Visual Studio. On the start window, choose Create a new project. Type wcf service library in the search box on the Create a new project page. Select either the C# or Visual Basic template for WCF Service Library, and then click Next.

How do I import a class library into Visual Studio?

Right-click on the solution in Solution Explorer and select Add > New Project. On the Add a new project page, enter library in the search box. Choose C# or Visual Basic from the Language list, and then choose All platforms from the Platform list. Choose the Class Library template, and then choose Next.

Is WCF a library?

A WCF service library is a dynamic-link library (DLL). As such, it cannot be executed on its own. It needs to be deployed into a hosting environment. For more information about this process, see Hosting and Consuming WCF Services.

What is the difference between WCF Service library and WCF Service application?

The major difference is that the WCF Service Application has a . svc file, whereas the WCF Service Library does not have a . svc file. Suppose we want to host this service application in IIS, then you need to specify for IIS the execution runtime environment requirements.


2 Answers

I discovered the following doing the opposite to what you are trying to achieve, i.e. changing a service library to a console application..

some of the settings in the csproj files cannot be edited from the settings screen from within VS to convert an class library to a WCF Service Library you need to add the following to your project file

Add the following to the first PropertyGroup [these are the guids for a C# WCF Project]

<ProjectTypeGuids>{3D9AD99F-2412-4246-B90B-4EAA41C64699};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>

See here for further information on ProjectTypeGuids

You may also need to add the following line immediately below:

<StartArguments>/client:"WcfTestClient.exe"</StartArguments>

But ultimately it's the PropertyTypeGuids that you need to manually insert to get VS to recognise the project as a WCF Service Library Project.

like image 159
Dog Ears Avatar answered Oct 31 '22 17:10

Dog Ears


This is what I had to do to convert my class library to WCF REST application.

1) Modify the .csproj file and add the below two lines to the first PropertyGroup element in .csproj file.

<ProjectTypeGuids>{349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}</ProjectTypeGuids>
<UseIISExpress>false</UseIISExpress>

2) Add the following line to below <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> to import Microsoft.WebApplication.targets file

<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" />

3) Add the following code to the end of the file before the </Project> tag.

<ProjectExtensions>
<VisualStudio>
  <FlavorProperties GUID="{349c5851-65df-11da-9384-00065b846f21}">
    <WebProjectProperties>
      <UseIIS>False</UseIIS>
      <AutoAssignPort>True</AutoAssignPort>
      <DevelopmentServerPort>50178</DevelopmentServerPort>
      <DevelopmentServerVPath>/</DevelopmentServerVPath>
      <IISUrl>
      </IISUrl>
      <NTLMAuthentication>False</NTLMAuthentication>
      <UseCustomServer>False</UseCustomServer>
      <CustomServerUrl>
      </CustomServerUrl>
      <SaveServerSettingsInUserFile>False</SaveServerSettingsInUserFile>
    </WebProjectProperties>
  </FlavorProperties>
</VisualStudio>

4) Save the .csproj file and Reload the project.

5) Add a Web.Config file to the project and add the below bare minimal code. You can add more later per your requirement.

    <?xml version="1.0"?>
<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>

  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
      <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
    </modules>
  </system.webServer>

  <system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
    <standardEndpoints>
      <webHttpEndpoint>
        <!-- 
            Configure the WCF REST service base address via the global.asax.cs file and the default endpoint 
            via the attributes on the <standardEndpoint> element below
        -->
        <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true"/>
      </webHttpEndpoint>
    </standardEndpoints>
  </system.serviceModel>

</configuration>

6) Add a Global.asax file. Below is a sample file.

    public class Global : HttpApplication
{
    void Application_Start(object sender, EventArgs e)
    {
        RegisterRoutes();
    }

    private void RegisterRoutes()
    {
        // Edit the base address of Service1 by replacing the "Service1" string below
        RouteTable.Routes.Add(new ServiceRoute("YourService", new WebServiceHostFactory(), typeof(YourServiceClass)));
    }
}

7) Finally in the project's properties, under Build tab, if the output path is set to bin\Debug modify it to bin\.

like image 31
muruge Avatar answered Oct 31 '22 18:10

muruge