Frage

This question is a followon from my previous where I have since discovered that it is not working 100% on my computer: WCF service not running on non-dev machine

I'm working through this example:

http://msdn.microsoft.com/en-us/library/ff649818.aspx

It turns out that the InstallUtil step isn't really working here. I've discovered that if VS2010 has the project open, and you go to add a service reference like in step 8 of the tutorial, VS2010 actually starts up the service host and therefore a reference is created.

Here's how i've debugged so far:

  • Install the service as per InstallUtil, close down VS2010 solution; then open a completely different solution (TESTWCF) Try and add a service reference and it fails - cannot find at the specified address

  • Open WCFServiceLibrary1 project again as a separate instance of VS2010. Try and add a service reference to TESTWCF and it fails.

  • Within WCFServiceLibrary1, attempt step 8 - add a service reference. This causes the service host to start and the service is found.

  • With service host still running, in TESTWCF I then try and add service and it works.

  • Close down the service host and try and add reference in TESTWCF and it doesn't work again.

This all seems to be totally independant of the service running or not running as installed by InstallUtil.

I've also verified this through the creation of a new virtual server from scratch and loading things on one by one. And only when VS2010 was installed did it start to work - when I observed above.

Any ideas ?

War es hilfreich?

Lösung 2

Success ! After like 4 days of effort on this, the MSDN tutorial has a fatal flaw.

In the first step of the tutorial you create a wcf service library and by default it names the service Service1. In step 2.6 of the tutorial you are asked to specify the base address:

net.tcp://localhost:8523/Service1 

Step 3 you are asked to create a new windows service, and by default this is also called Service1.

In step 5.2 you are asked to make a reference to System.ServiceModel and to WcfServiceLibrary1.

In step 5.6 you replace the Onstart Method to start the service and, Step 8 shows the final code as being:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.ServiceModel;
using WcfServiceLibrary1;

namespace WindowsService1
{
public partial class Service1: ServiceBase
{
    internal static ServiceHost myServiceHost = null; 

    public WCFServiceHost1()
    {
        InitializeComponent();
    }
    protected override void OnStart(string[] args)
    {
        if (myServiceHost != null)
        {
            myServiceHost.Close();
        }
        myServiceHost = new ServiceHost(typeof(Service1));
        myServiceHost.Open();
    }
    protected override void OnStop()
    {
        if (myServiceHost != null)
        {
            myServiceHost.Close();
            myServiceHost = null;
        }
    }
}
}

The crucial line of code which is wrong is:

        myServiceHost = new ServiceHost(typeof(Service1));

Well it might behave differently in VS2008 or 2005 or maybe it's a config in VS2010 however, my VS2010 interprets Service1 to be that of the containing class ie:

WindowsService1.Service1

Whereas it should in fact be:

WcfServiceLibrary1.Service1

I noticed that 4 days ago but figured I didn't know enough about WCF and I was wrong somehow - esp when it appeared to work 'cause of VS2010 starting it up itself.

Andere Tipps

WCF services can be self-hosted in an application (such as a console or a Windows Forms application)

I think you are over complicating it, you don't have to even install it with InstallUtil. InstallUtil installs it to run as windows service, and you can make console application which will be serving as WCF service.

You have to import:

System.ServiceModel
System.ServiceModel.Web
System.Web.Services

I think those with web will be needed if you want to use it as web service with get and post.

Then you need to specify contract for client and server.

[ServiceContract(Name = "SomeService", Namespace = "http://some.domain.com/some/someservice",SessionMode=SessionMode.Required)]
    public interface ISomeService
    {
        [OperationContract]
        string Execute(string expression);
    }

You have contract and now you have to implement it in service. nothing special in there just use this interface.

What is very important is app.config, you have to specify it well for client and for service. In config you have all stuff that points to service.

In client you have to add service as reference, it should find it as in point 8 but only if you have configs ok!

In client just do something in code like that:

 using (ChannelFactory<ISomeService> channel = new ChannelFactory<ISomeService>("SomeService"))
            {
                ISomeService svc = channel.CreateChannel();
                svc.Execute("my expression to evaluate by the service");
             }

Try to make it easiest possible way without InstallUtil and such, it doesn't have to be windows service to serve stuff over network.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top