views:

334

answers:

2

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.

+1  A: 

WCF is not dot net. To create a WCF application you have to do four things

  1. Define a service contract
  2. Implement the contract on the server side
  3. Host your implemented service
  4. Create a Client that also can use the service contract

take a look at this tutorial

This is a complete example of a service and its host

using System.ServiceModel;
using System.ServiceModel.Description;
using System.Runtime.Serialization;
using System;

[ServiceContract]
public interface  AddStuff
{
    [OperationContract]
    int Add(int X,int Y);
}

public class opAddStuff : AddStuff
{
    public int Add(int X, int Y)
    {
        return X + Y;
    }
}

public class Pgm
{
    static void Main(string[] args)
    {
        string httpAddr = "http://127.0.0.1:6001/AddStuff";
        string netAddr= "net.tcp://127.0.0.1:5001/AddStuff";

        System.ServiceModel.ServiceHost SH = new ServiceHost(typeof(opAddStuff),new Uri(httpAddr));

        BasicHttpBinding B = new BasicHttpBinding();
        NetTcpBinding NB = new NetTcpBinding();

        SH.AddServiceEndpoint(typeof(AddStuff), B, httpAddr);
        SH.AddServiceEndpoint(typeof(AddStuff), NB, netAddr);



        System.ServiceModel.Description.ServiceMetadataBehavior smb = SH.Description.Behaviors.Find<ServiceMetadataBehavior>();
        // If not, add one
        if (smb == null)
            smb = new ServiceMetadataBehavior();

        smb.HttpGetEnabled = true;
        smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;

        SH.Description.Behaviors.Add(smb);
        SH.AddServiceEndpoint(  ServiceMetadataBehavior.MexContractName,  MetadataExchangeBindings.CreateMexHttpBinding(),  "mex");

        SH.Open();

        Console.WriteLine("Service at your service");
        string crap = Console.ReadLine();



    }
}

You also have to run this command

netsh http add urlacl url=http://+:6001/AddStuff user=DOMAIN\USER

some of this comes from here

rerun
I've created the service contract. Implemented it. I'm having a problem at the Host point. When you create a WCF application it does it for you. I'd like to modify a Class Library Application to do that.
Yuriy Faktorovich
You are still launching an exe for the service and instantiating the service. Thats my problem, I need Visual Studio to figure that out by itself like it does in a WCF Service Library.
Yuriy Faktorovich
+1  A: 

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.

Dog Ears