By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,213 Members | 2,108 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,213 IT Pros & Developers. It's quick & easy.

Connecting to a WCF Service hosted in a Windows Service

P: n/a
I have written a simple WCF service hosted in a Windows console application
and a simple WCF client console application that connects successfully to
that service and retrieves data. I then ported the console application to
WinForms and also got that to work properly.

My next move was to host the WCF service in a Windows service application. I
believe I have that working correctly, but I can't for the life of me get a
client to connect to the service. I did not use svcutil to generate proxy
code for the client, but instead relied on using a ChannelFactory to
instantiate my client connection to the service.

Here's the client code:

namespace MyClient
{
public partial class MainForm : Form
{
private IMyService service;

public MainForm()
{
InitializeComponent();

try
{
ChannelFactory<IMyServicefactory = new
ChannelFactory<IMyService>("MyService");
service = factory.CreateChannel();
string myData = service.GetMyData();
MessageBox.Show(myData);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + ex.StackTrace);
}
}
}
}

Here's the client app.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<client>
<endpoint address="net.tcp://localhost:12345/MyService/service"
binding="netTcpBinding"
contract="Contracts.IMyService"
name="MyService">
</endpoint>
</client>
</system.serviceModel>
</configuration>

Here's the server app.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<service name="MyService">
<host>
<baseAddresses>
<add
baseAddress="net.tcp://localhost:12345/MyService/service"/>
</baseAddresses>
</host>
<endpoint address=""
binding="netTcpBinding"
contract="Contracts.IMyService">
</endpoint>
<endpoint address="mex"
binding="mexTcpBinding"
contract="IMetadataExchange">
</endpoint>
</service>
</services>
</system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="metadataSupport">
<serviceMetadata>
</serviceMetadata>
</behavior>
</serviceBehaviors>
</behaviors>
</configuration>

Here's the contract code:

namespace Contracts
{
[ServiceContract]
public interface IMyService
{
[OperationContract]
string GetMyData();
}
}

I get the following exception when invoking the service.GetMyData() method:

Could not connect to net.tcp://localhost:12345/MyService/service. The
connection attempt lasted for a time span of 00:00:00.9843813. TCP error code
10061: No connection could be made because the target machine actively
refused it.
Server stack trace:
at System.ServiceModel.Channels.SocketConnectionIniti ator.Connect(Uri
uri, TimeSpan timeout)
at System.ServiceModel.Channels.BufferedConnectionIni tiator.Connect(Uri
uri, TimeSpan timeout)
at
System.ServiceModel.Channels.ConnectionPoolHelper. EstablishConnection(TimeSpan timeout)
at
System.ServiceModel.Channels.ClientFramingDuplexSe ssionChannel.OnOpen(TimeSpan timeout)
at System.ServiceModel.Channels.CommunicationObject.O pen(TimeSpan timeout)
at System.ServiceModel.Channels.ServiceChannel.OnOpen (TimeSpan timeout)
at System.ServiceModel.Channels.CommunicationObject.O pen(TimeSpan timeout)
at
System.ServiceModel.Channels.ServiceChannel.CallOp enOnce.System.ServiceModel.Channels.ServiceChannel .ICallOnce.Call(ServiceChannel
channel, TimeSpan timeout)
at
System.ServiceModel.Channels.ServiceChannel.CallOn ceManager.CallOnce(TimeSpan
timeout, CallOnceManager cascade)
at System.ServiceModel.Channels.ServiceChannel.Ensure Opened(TimeSpan
timeout)
at System.ServiceModel.Channels.ServiceChannel.Call(S tring action,
Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs,
TimeSpan timeout)
at System.ServiceModel.Channels.ServiceChannel.Call(S tring action,
Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs)
at
System.ServiceModel.Channels.ServiceChannelProxy.I nvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
at System.ServiceModel.Channels.ServiceChannelProxy.I nvoke(IMessage
message)

Exception rethrown at [0]:
at System.Runtime.Remoting.Proxies.RealProxy.HandleRe turnMessage(IMessage
reqMsg, IMessage retMsg)
at System.Runtime.Remoting.Proxies.RealProxy.PrivateI nvoke(MessageData&
msgData, Int32 type)
at Contracts.IMyService.GetMyData()
at WinClient.MainForm.GetMyData() in C:\Documents and Settings\adrian\My
Documents\Visual Studio 2005\Projects\WCFTest\WinClient\MainForm.cs:line 54

This is my first attempt at using WCF. Any help would be greatly appreciated.
Mar 19 '07 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.